客户
dao层
@Repository
public class CustomerDaoImpl implements ICustomerDao {
@Autowired
private HibernateTemplate template;//注入模板,方便开发
@Override
public int save(Customer customer) {
return (int) template.save(customer);
}
@Override
public List<Customer> findByExample(Customer example, int pageNum) {
return template.findByExample(example, (pageNum - 1) * Const.PAGE_SIZE, Const.PAGE_SIZE);
}
@Override
public int count(Customer cust) {
List<Customer> list = template.findByExample(cust);
return list.size();
}
}
service层
@Service
public class CustomerServiceImpl implements ICustomerService {
@Autowired
private ICustomerDao customerDao;
public Page<Customer> findByExample(Customer customer, int pageNum) {
//查询客户
List<Customer> list = customerDao.findByExample(customer, pageNum);
// 总条数
int count = customerDao.count(customer);
//封装成page
return new Page<Customer>(list, pageNum, count);
}
}
action层
@Namespace("/") //名称空间
@ResultPath("/jsp/customer") //在所有的地址前面添加前缀
@Component //spring创建action
@Scope("prototype") //多例,action都是多例的
public class CustomerAction implements ModelDriven<Customer> {//模型驱动封装对象
@Autowired
private ICustomerService customerService;
private Customer customer = new Customer();//模型驱动一对要手动创建对象
private int pageNum = 1;//当前页数,默认是首页
//配置action
@Action(value = "list", results = { @Result(name = "success", location = "list.jsp") })
public String list() throws IOException {
Page<Customer> page = customerService.findByExample(customer, pageNum);
ActionContext.getContext().put("page", page);//将结果放到域对象中
return null;
}
//set、get方法
}
applicationContext.xml
<context:component-scan base-package="com.crm" />
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/db.properties" />
<!-- 配置数据 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingLocations" value="classpath:hibernate/*.hbm.xml"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
网友评论