ssh整合三种方式(三)

ssh环境搭建(xml和注解配置结合版)

准备工作:
创建数据库,创建web工程,创建实体类,编写service层和dao层的接口和实现类;

1、搭建Spring+Hibernate开发环境,保证可以正常运行。

(1) 编写Spring配置文件

<?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/aop
				        http://www.springframework.org/schema/aop/spring-aop.xsd
				        http://www.springframework.org/schema/tx
				        http://www.springframework.org/schema/tx/spring-tx.xsd
				        http://www.springframework.org/schema/context
				        http://www.springframework.org/schema/context/spring-context.xsd">
	 
	 <!-- 配置Spring创建容器时要扫描的包 -->
	 <context:component-scan base-package="cn.com"></context:component-scan>
	 
	 <!-- 配置HibernateTemplate -->
	 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
	 	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 
	 <!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 第一部分:连接数据库的  用连接池-->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 第二部分:hibernate的可选配置 -->
		<property name="hibernateProperties">
			<props>
				<prop key=""></prop>
				<!-- 数据库的方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 是否显示hibernate生成的SQL语句 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 是否使用格式化输出sql语句到控制台 -->
				<prop key="hibernate.format_sql">false</prop>
				<!-- 配置hibernate采用何种方式生成DDL语句 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 把session和线程绑定,从而实现一个线程只有一个Session -->
				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
			</props>
		</property>
		<!-- 第三部分:指定实体类所在的包,当创建SessionFactory,会去该包中扫描实体类上的注解,从而生成映射配置-->
		<property name="packagesToScan">
			<array>
				<value>cn.com.pojo</value>
			</array>
		</property>
	</bean>

	<!-- 配置连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/environment_s2s4h5"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 开启Spring对注解事物的支持 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

(2) 在web.xml 中指定Spring配置文件的存放路径,并且配置监听器。

 <!-- 
		配置spring提供的监听器,用于监听servletContext对象创建,同时为我们创建spring的容器 
		默认情况下:它只能加载位置是在WEB-INF目录中的spring配置文件,同时文件名称必须是applicationContext.xml
  -->
  <listener>
  	  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:config/spring/applicationContext.xml</param-value>
  </context-param>

(3) 使用注解配置实体类、Service以及Dao(如下所示)。

/**
 * Customer 实体类
 * @author Administrator
 *
 */
@Entity
@Table(name="cst_customer")
public class Customer implements Serializable {
	
	@Id
	@Column(name="cust_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long custId;
	
	@Column(name="cust_name")
	private String custName;
	
	@Column(name="cust_source")
	private String custSource;
	
	......

===================================================

/**
 * 持久层的实现类
 * @author Administrator
 *
 */
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao<Customer> {

	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;

	@Override
	public List<Customer> findAll() {
		return (List<Customer>) hibernateTemplate.find("from Customer");
	}
}

===================================================

/**
 * 业务层接口的实现类
 * @author Administrator
 *
 */
@Service("customerService")
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements ICustomerService<Customer>{
	
	@Resource(name="customerDao")
	private ICustomerDao<Customer> customerDao;

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

(4) 测试Spring+Hibernate开发环境是否可用

2、搭建Struts2的开发环境(这里使用事先准备好的前端页面)

(1)创建动作类,并配置

  • 配 置 事 项 如下:
  • 配置 继承的包、名称空间、结果视图、动作方法(每个动作方法的值需要和页面中访问的动作方法相一致)
/**
 * 表现层动作类
 * 
 * @author Administrator
 *
 */
@Controller("customerAction")
@ParentPackage("struts-default")
//名称空间
@Namespace("/customer")
//根据返回结果跳到指定的结果视图
@Results({
	@Result(name="findAll",type="dispatcher",location="/jsp/customer/list.jsp")
		})
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

	private Customer customer = new Customer();
	//注入service,方便调用service层方法
	@Resource(name="customerService")
	private ICustomerService<Customer> customerService;
	private List<Customer> customers;
	
	public Customer getModel() {
		return customer;
	}
	
	public List<Customer> getCustomers() {
		return customers;
	}
	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	/**
	 * 动作方法
	 * 查询所有客户
	 * 
	 * @return 跳转到显示页面
	 */
	@Action("findAll")
	public String findAll(){
		customers = customerService.findAllCustomer();
		return "findAll";
	}
}

(2)在web.xml中配置struts2的核心过滤器
代码如下:

 <!-- 配置struts2的核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  	<!-- 配置开启开发者模式 -->
  	<init-param>
  		<param-name>struts.devMode</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

(3)验证Spring+Hibernate+Struts2是否可用
使用浏览器访问页面,然后查看功能是否可用。

ps:此种ssh整合方式在实际开发中比较常用!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值