SSH整合(xml和注解版本)

 spring配置使用注解实现

 第一步:导入spring的必备jar包

之前的环境已经导入。略。


第二步:在spring配置文件中导入context名称空间及约束 







第三步:在spring配置文件中配置要扫描的包


第四步:把action,service和dao都用注解配置

/**
 * 客户的动作类
 */
@Controller("customerAction")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven {
	@Autowired
	private ICustomerService customerService;
	//action中的方法不变
}

/**
 * 客户的业务层实现类
 */
@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
	@Autowired
	private ICustomerDao customerDao;
	//service中的方法不变
}

/**
 * 客户的持久层实现类
 */
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {
	//dao中必须自己定义HibernateTemplate,不能继承HibernateDaoSupport了
	@Autowired
	private HibernateTemplate hibernateTemplate;
	//dao中的方法不变
}

第五步:在spring配置文件中配置HiernateTemplate 




	
	

第六步:在spring配置文件中配置事务管理器 




	
	

第七步:在spring配置文件中开启spring对注解事务的支持




第八步:在客户的业务层实现类上使用@Transactional注解

/**
 * 客户的业务层实现类
 */
@Service("customerService")
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements ICustomerService {
    
    @Autowired
    private ICustomerDao customerDao;

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List findAllCustomer() {
        return customerDao.findAllCustomer();
    }

    @Override
    public void saveCustomer(Customer customer) {
        customerDao.saveCustomer(customer);
    }
}

 

hibernate映射使用注解配置实现

实体类映射注解配置


/**
 * 客户的实体类
 *  JPA规范:java 持久化规范
 *  注解全都是JPA规范的。
 *  导包都需要导入javax.persistence包下的
 *
 */
@Entity
@Table(name="cst_customer")
public class Customer implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="cust_id")
	private Long custId;
	@Column(name="cust_name")
	private String custName;
	@Column(name="cust_source")
	private String custSource;
	@Column(name="cust_industry")
	private String custIndustry;
	@Column(name="cust_level")
	private String custLevel;
	@Column(name="cust_address")
	private String custAddress;
	@Column(name="cust_phone")
	private String custPhone;
	public Long getCustId() {
		return custId;
	}
	public void setCustId(Long custId) {
		this.custId = custId;
	}
	public String getCustName() {
		return custName;
	}
	public void setCustName(String custName) {
		this.custName = custName;
	}
	public String getCustSource() {
		return custSource;
	}
	public void setCustSource(String custSource) {
		this.custSource = custSource;
	}
	public String getCustIndustry() {
		return custIndustry;
	}
	public void setCustIndustry(String custIndustry) {
		this.custIndustry = custIndustry;
	}
	public String getCustLevel() {
		return custLevel;
	}
	public void setCustLevel(String custLevel) {
		this.custLevel = custLevel;
	}
	public String getCustAddress() {
		return custAddress;
	}
	public void setCustAddress(String custAddress) {
		this.custAddress = custAddress;
	}
	public String getCustPhone() {
		return custPhone;
	}
	public void setCustPhone(String custPhone) {
		this.custPhone = custPhone;
	}
	@Override
	public String toString() {
		return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
				+ ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
				+ ", custPhone=" + custPhone + "]";
	}	
}

 

spring中SessionFactory配置修改




	
	
	
	
		
			
			
				org.hibernate.dialect.MySQLDialect
			
			
			true
			
			false
			
			update
			
			
				org.springframework.orm.hibernate5.SpringSessionContext
			
		
	
	
	
		
			com.itheima.domain
		
	

struts2配置使用注解实现


导入struts2注解的jar包

使用注解配置Action


/**
 * 客户的动作类
 */
@Controller("customerAction")
@Scope("prototype")
//-------以下都是struts2的注解-----------
@ParentPackage("struts-default")//指定当前包的父包
@Namespace("/customer")//指定名称空间,访问当前action的所有方法都需要有名称空间
public class CustomerAction extends ActionSupport implements ModelDriven {
	private Customer customer = new Customer();
	
	@Autowired
	private ICustomerService customerService;	
	
	@Override
	public Customer getModel() {
		return customer;
	}
	
	/**
	 * 查询所有客户
	 * @return
	 */
	private List customers;
	//用于配置动作名称
	@Action(value="findAllCustomer",results={
			@Result(name="findAllCustomer",
					type="dispatcher",
					location="/jsp/customer/list.jsp")
	})
	public String findAllCustomer(){
		customers = customerService.findAllCustomer();
		return "findAllCustomer";
	}
	
	/**
	 * 获取添加客户页面
	 * @return
	 */
	@Action(value="addUICustomer",results={
			@Result(name="addUICustomer",
					location="/jsp/customer/add.jsp")
	})
	public String addUICustomer(){
		return "addUICustomer";
	}
	
	/**
	 * 添加客户
	 * @return
	 */
	@Action(value="addCustomer",results={
			@Result(name="addCustomer",
					type="redirect",
					location="/jsp/success.jsp")
	})
	public String addCustomer(){
		customerService.saveCustomer(customer);
		return "addCustomer";
	}
	
	public List getCustomers() {
		return customers;
	}
	public void setCustomers(List customers) {
		this.customers = customers;
	}
}

 

 

你可能感兴趣的:(SSH框架)