本文共 5818 字,大约阅读时间需要 19 分钟。
整体结构:
1.引入相关jar包
2.编写实体类和映射文件
package cn.zqr.domain;public class Customer { private Long cust_id; private String cust_name; private Long cust_user_id; private Long cust_create_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public Long getCust_user_id() { return cust_user_id; } public void setCust_user_id(Long cust_user_id) { this.cust_user_id = cust_user_id; } public Long getCust_create_id() { return cust_create_id; } public void setCust_create_id(Long cust_create_id) { this.cust_create_id = cust_create_id; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id + ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]"; } }
3.配置struts核心过滤器,和Spring文件加载的监听器
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml struts org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts /*
4.编写Action并配置,采用模型驱动的方式对表单进行封装,采用Spring注入的方式获取serice实例,并配置Action
/** * 客户的控制层 */public class CustomerAction extends ActionSupport implements ModelDriven{ private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } //必须手动new private Customer customer=new Customer(); //模型和属性驱动 /** * 保存客户 * @return */ public String add(){ customerService.save(customer); return NONE; } @Override public Object getModel() { return customer; }}
使用Spring管理Action
在struts中引入Action
5.配置service
service层代码:
/** * 客户业务层 */@Transactionalpublic class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } /** * 保存客户信息 * @param customer */ @Override public void save(Customer customer) { customerDao.add(customer); }}
service需要开事务,在Spring中配置事务,开启事务需要使用session,所以首先配置SessionFactory,创建session工厂需要数据源,所以需要优先配置数据源
org.hibernate.dialect.MySQLDialect true true update
Customer.hbm.xml
最后配置事务并开启
6.编写dao层,dao层需要使用Hibernate模板(HibernateTemplate),为了简化开发,可以直接继承HibernateDaoSupport,然后向其注入sessionFactory
/** * 客户dao层 */public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { /** * 保存客户 */ @Override public void add(Customer customer) { System.out.println("持久层Customer customer"+customer); this.getHibernateTemplate().save(customer); }}
转载地址:http://pcukx.baihongyu.com/