SSH_框架整合1

1 WEB环境下配置Spring

    因为是在WEB环境中应用Spring,所以要先配置web.xml:

(1)WebContent-WEB-INF-lib包中,加入Spring包下的required的所有jar包;

  (2)WebContent-WEB-INF下新建web.xml,配置web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置spring配置文件.xml的名称和位置路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

(3)src下新建Source-folder包:conf,新建applicationContext.xml(暂时不需要写)

2 加入Hibernate

  (1)建立持久化类,生成对应的 .hbm.xml 文件, 生成对应的数据表;

  (2)Spring整合Hibernate

  (3) 步骤:

    ①加入Hibernate的jar包,加入MySQL和c3p0驱动;

    ②conf报下配置hibernate.cfg.xml文件

  
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

    ③src下新建实体类:com.atguigu.ssh.entities:Department.java和Employee.java  

Department.java:
  
 package com.atguigu.ssh.entities;

 public class Department {

     private Integer id;
private String departmentName="";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
} }
Employee.java:
  
 package com.atguigu.ssh.entities;

 import java.util.Date;

 public class Employee {
private Integer id; private String lastName="";
private String email; private Date birth;
private Date createTime; private Department department; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} }

     ④生成对应的.hbm.xml持久化类映射文件:Department.hbm.xml,Employee.hbm.xml

    ⑤conf包下新建db.properties文件: 

  
 jdbc.user=root
jdbc.password=920614
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring_ssh jdbc.initPoolSize=5
jdbc.maxPoolSize=10
#...

    ⑥再次配置Spring配置文件:applicationContext.xml

  
 <?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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 导入C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!--配置Hibernate 的sessionFactory的实例 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property>
</bean> <!--配置Spring声明式事务 -->
<!--1 配置hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!--2 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!--3 配置事务切入点 -->
<aop:config>
<aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config> </beans>

SSH_框架整合1整体框架图1:

  SSH_框架整合1

  

上一篇:LevelDB源码分析-编码


下一篇:spring MVC 后台token防重复提交解决方案