SSH Struts2+hiberante+Spring整合
Posted 小手撬动地球
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH Struts2+hiberante+Spring整合相关的知识,希望对你有一定的参考价值。
使用SSH框架编写学生信息:
一.新建Java工程:
(1)建立好Java各层级之间的结构:业务处理层dao,数据模型层domain,页面请求处理层(Struts2 MVC层)action,service层.
(2)建立好各层的实现类及接口;
(3)建立一个source folder文件夹,用来存放一些配置问价.
(4)改变字节码生成的位置,改为WEB-INF下面的classes文件夹下.
Java工程层级结构如下图:
二.hibernate整合到Spring容器中
步骤:
1.编写domain,数据模型,此例子为Student类.
package com.xk.ssh.proj.domian; public class Student { private Integer id; private String name; private Integer age; private double fees; private long contact; public Student() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public double getFees() { return fees; } public void setFees(double fees) { this.fees = fees; } public long getContact() { return contact; } public void setContact(long contact) { this.contact = contact; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", fees=" + fees + ", contact=" + contact + "]"; } public Student(Integer id, String name, Integer age, double fees, long contact) { this.id = id; this.name = name; this.age = age; this.fees = fees; this.contact = contact; } public Student( String name, Integer age, double fees, long contact) { this.name = name; this.age = age; this.fees = fees; this.contact = contact; } public Student(Integer id) { this.id = id; } }
2.遍写映射文件,在domain文件夹下编写Student.xml文件.hibernate主要用来管理数据库.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.xk.ssh.proj.domian"> <class name="Student" table="Student"> <id name="id" column="s_id"> <generator class="native" /> </id> <property name="name" column="s_name" /> <property name="age" column="s_age" /> <property name="fees" column="s_fees" /> <property name="contact" column="s_phone" /> </class> </hibernate-mapping>
3.编写Dao接口及Dao的实现类
package com.xk.ssh.proj.dao; import java.util.List; import com.xk.ssh.proj.domian.Student; public interface IStudentDao { public void save(Student stu); public void delete(Student stu); public void update(Student stu); public Student get(Student stu); public List<Student> list(); }
package com.xk.ssh.proj.dao.impl; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.xk.ssh.proj.dao.IStudentDao; import com.xk.ssh.proj.domian.Student; public class StudentDaoImpl implements IStudentDao { // 在Dao中注入SessionFaction private SessionFactory sessionfactory; public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; } @Override public void save(Student stu) { Session session = sessionfactory.getCurrentSession(); session.save(stu); } @Override public void delete(Student stu) { Session session = sessionfactory.getCurrentSession(); session.delete(stu); } @Override public void update(Student stu) { Session session = sessionfactory.getCurrentSession(); session.update(stu); } @Override public Student get(Student stu) { Session session = sessionfactory.getCurrentSession(); // Student.class:Student的字节码文件 return (Student) session.get(Student.class, stu.getId()); } @SuppressWarnings("unchecked") @Override public List<Student> list() { // 查询session.createQuery("sql语句").list Session session = sessionfactory.getCurrentSession(); return session.createQuery("SELECT stu FROM Student stu").list(); } }
4.拷贝架包:数据驱动的hibernate包,Spring的core,test,bean,context等
antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
dom4j-1.6.1.jar
druid-1.0.15.jar
ehcache-core-2.4.3.jar
freemarker-2.3.22.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.2.0.Final.jar
hibernate-ehcache-4.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.0.6.jar
servlet-api.jar
slf4j-api-1.6.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-test-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
struts2-core-2.3.24.jar
struts2-spring-plugin-2.3.24.jar
xwork-core-2.3.24.jar
5.Spring的配置文件:
①添加并配置.properties文件
<context:property-placeholder location="classpath:db.properties" />
②配置DataSource,连接数据库
<!-- 配置DataSource --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 连接数据库 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
④配置SessionFactory:(jdbc/tx/orm架包)
使用LocalSessionFactoryBean 创建SessionFactory
配置dataSource和引用
配置hibernate中的常规配置(方言)
配置domain的映射文件
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置连接数据库的信息 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的dataSource 此处的ref是引用配置DateSource中的id --> <property name="dataSource" ref="dataSource" /> <!-- 配置hibernate常规配置 --> <property name="hibernateProperties"> <!-- dialect方言 --> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=none hibernate.show_sql=true </value> </property> <!-- 配置映射文件,告诉Spring框架*.hbm.xml在哪 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的mappingDirectoryLocations value是自己的对象xml的包的权限定名称 --> <property name="mappingDirectoryLocations" value="classpath:com/xk/ssh/proj/domian" /> </bean>
6.编写Dao
①直接在Dao中注入一个SessionFactory;
private SessionFactory sessionfactory; public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; }
②在Dao中直接使用SessionFactory.getCurrentSession()来得到我们需要的session
Session session = sessionfactory.getCurrentSession();
7.在Spring中配置Dao
①抽象一个baseDao;
<bean id = "baseDao" abstract = "true" />
②Dao继承BaseDao;
<bean id="stuDao" parent="baseDao"/>
Dao类extends jdbcDaoSupport implements 接口
8.编写Service
①编写Service接口和实现
package com.xk.ssh.proj.service; import java.util.List; import com.xk.ssh.proj.domian.Student; public interface IStudentService { public void save(Student stu); public void delete(Student stu); public void update(Student stu); public Student get(Student stu); public List<Student> list(); }
1 package com.xk.ssh.proj.service.impl; 2 import java.util.List; 3 import com.xk.ssh.proj.dao.IStudentDao; 4 import com.xk.ssh.proj.domian.Student; 5 import com.xk.ssh.proj.service.IStudentService; 6 public class StudentServiceImpl implements IStudentService { 7 private IStudentDao student; 8 public void setStudent(IStudentDao student) { 9 this.student = student; 10 } 11 @Override 12 public void save(Student stu) { 13 student.save(stu); 14 } 15 @Override 16 public void delete(Student stu) { 17 student.delete(stu); 18 } 19 @Override 20 public void update(Student stu) { 21 student.update(stu); 22 } 23 @Override 24 public Student get(Student stu) { 25 return student.get(stu); 26 } 27 @Override 28 public List<Student> list() { 29 return student.list(); 30 } 31 }
②在Spring中配置事物servicebean
<!-- 配置service id为唯一标识,class为service的实现类 name为service实现类中注入的dao层对象 ref为引用Dao -->
<bean id="stuService" class="com.xk.ssh.proj.service.impl.StudentServiceImpl">
<property name="student" ref="stuDao" />
</bean>
9.配置事物
<!-- 配置事务管理器 --> <bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事物 事物是配置给service实现的 --> <aop:config> <!-- pointcut:切点 ; Advice:通知 advisor:切点和通知的配置器 下面是配置切点:advice-ref:引用通知的ID --> <aop:pointcut expression="execution(* com.xk.ssh.proj.service.*.*(..))" id="stuTC" /> <aop:advisor advice-ref="StuAdvice" pointcut-ref="stuTC" /> </aop:config> <tx:advice id="StuAdvice" transaction-manager="txMgr"> <tx:attributes> <!-- 过滤方法,将所有以get和list开头的方法标记为只读 --> <tx:method name="get*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice>
三.将Struts2 集成在Spring容器
1.导架包:(spring-web/Struts-spring-plugin...jar)
2.编写Struts.xml(位置与源文件夹同级)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 --> <constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> <package name="hello" extends="struts-default"> <action name="list_*" class="StuAction" method="{1}"> <result name="list">/WEB-INF/views/SSH.jsp</result> </action> </package> </struts>
3.编写web.xml文件(位置在WEB-INF文件夹下面)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 --> <constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> <package name="hello" extends="struts-default"> <action name="list_*" class="StuAction" method="{1}"> <result name="list">/WEB-INF/views/SSH.jsp</result> </action> </package> </struts>
4.编写Action (页面请求)
package com.xk.ssh.proj.mvc; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.xk.ssh.proj.domian.Student; import com.xk.ssh.proj.service.IStudentService; public class StudentAction extends ActionSupport { private static final long serialVersionUID = 1L; private IStudentService studentService; public void setStudentService(IStudentService studentService) { this.studentService = studentService; } public String list() { List<Student> list = studentService.list(); ActionContext context = ActionContext.getContext(); context.put("key", list); System.out.println(list); return "list"; } }
5.在applicationcontext.xml中配置action
<!-- 配置Action --> <bean id="StuAction" class="com.xk.ssh.proj.mvc.StudentAction"> <property name="studentService" ref="stuService"/> </bean>
6.编写Struts的配置文件:在action的class属性中填写的是此action在applicationcontext.xml中对应的bean的id
7.在web.xml中添加listener监听器
8.在web.xml中添加Spring框架启动的加载文件配置路径
<context-param > <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param>
四.附上完整代码:
1 //domain 2 package com.xk.ssh.proj.domian; 3 4 public class Student { 5 private Integer id; 6 private String name; 7 private Integer age; 8 private double fees; 9 private long contact; 10 public Student() { 11 } 12 public Integer getId() { 13 return id; 14 } 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 public double getFees() { 31 return fees; 32 } 33 public void setFees(double fees) { 34 this.fees = fees; 35 } 36 public long getContact() { 37 return contact; 38 } 39 public void setContact(long contact) { 40 this.contact = contact; 41 } 42 @Override 43 public String toString() { 44 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", fees=" + fees + ", contact=" + contact 45 + "]"; 46 } 47 public Student(Integer id, String name, Integer age, double fees, long contact) { 48 this.id = id; 49 this.name = name; 50 this.age = age; 51 this.fees = fees; 52 this.contact = contact; 53 } 54 public Student( String name, Integer age, double fees, long contact) { 55 this.name = name; 56 this.age = age; 57 this.fees = fees; 58 this.contact = contact; 59 } 60 public Student(Integer id) { 61 this.id = id; 62 } 63 } 64 65 //domain的xml 66 <?xml version="1.0"?> 67 <!DOCTYPE hibernate-mapping PUBLIC 68 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 69 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 70 <hibernate-mapping package="com.xk.ssh.proj.domian"> 71 <class name="Student" table="Student"> 72 <id name="id" column="s_id"> 73 <generator class="native" /> 74 </id> 75 <property name="name" column="s_name" /> 76 <property name="age" column="s_age" /> 77 <property name="fees" column="s_fees" /> 78 <property name="contact" column="s_phone" /> 79 </class> 80 </hibernate-mapping> 81 //dao接口 82 package com.xk.ssh.proj.dao; 83 import java.util.List; 84 import com.xk.ssh.proj.domian.Student; 85 public interface IStudentDao { 86 public void save(Student stu); 87 public void delete(Student stu); 88 public void update(Student stu); 89 public Student get(Student stu); 90 public List<Student> list(); 91 } 92 93 //dao 94 package com.xk.ssh.proj.dao.impl; 95 96 import java.util.List; 97 98 import org.hibernate.Session; 99 import org.hibernate.SessionFactory; 100 101 102 import com.xk.ssh.proj.dao.IStudentDao; 103 import com.xk.ssh.proj.domian.Student; 104 105 public class StudentDaoImpl implements IStudentDao { 106 基于SSH的书籍销售共享系统