SSH整合(xml和注解版本)

 spring配置使用注解实现

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

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


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




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        		http://www.springframework.org/schema/beans/spring-beans.xsd
        		http://www.springframework.org/schema/tx 
        		http://www.springframework.org/schema/tx/spring-tx.xsd
        		http://www.springframework.org/schema/aop 
        		http://www.springframework.org/schema/aop/spring-aop.xsd
        		http://www.springframework.org/schema/context 
        		http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

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

<!-- 配置spring运行要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>

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

/**
 * 客户的动作类
 */
@Controller("customerAction")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	@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 


<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" 
		class="org.springframework.orm.hibernate5.HibernateTemplate">
	<!-- 注入SessionFactory -->
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

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


<!-- 配置事务管理器 -->
<bean id="transactionManager" 
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	<!-- 注入SessionFactory -->
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

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


<!-- 开启spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

第八步:在客户的业务层实现类上使用@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<Customer> 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配置修改


<!-- 配置SessionFactory -->
<bean id="sessionFactory" 
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<!-- 1、连接数据库的 -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 2、hibernate基本配置的 -->
	<property name="hibernateProperties">
		<props>
			<!-- 数据库的方言-->
			<prop key="hibernate.dialect">
				org.hibernate.dialect.MySQLDialect
			</prop>
			<!-- 是否显示sql语句-->
			<prop key="hibernate.show_sql">true</prop>
			<!-- 是否格式化sql语句-->
			<prop key="hibernate.format_sql">false</prop>
			<!-- 采用何种方式生成数据库表结构 -->
			<prop key="hibernate.hbm2ddl.auto">update</prop>
			<!-- 是spring把sesion绑定到当前线程上的配置 -->
			<prop key="hibernate.current_session_context_class">
				org.springframework.orm.hibernate5.SpringSessionContext
			</prop>
		</props>
	</property>
	<!-- 3、指定扫描映射注解的包-->
	<property name="packagesToScan">
		<array>
			<value>com.itheima.domain</value>
		</array>
	</property>
</bean>

struts2配置使用注解实现


导入struts2注解的jar包

使用注解配置Action


/**
 * 客户的动作类
 */
@Controller("customerAction")
@Scope("prototype")
//-------以下都是struts2的注解-----------
@ParentPackage("struts-default")//指定当前包的父包
@Namespace("/customer")//指定名称空间,访问当前action的所有方法都需要有名称空间
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	private Customer customer = new Customer();
	
	@Autowired
	private ICustomerService customerService;	
	
	@Override
	public Customer getModel() {
		return customer;
	}
	
	/**
	 * 查询所有客户
	 * @return
	 */
	private List<Customer> 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<Customer> getCustomers() {
		return customers;
	}
	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值