首先,先要将每一层的作用弄清,
Struts2属于mvc层,主要是将页面和后端的代码连接起来;
Hibernate主要是用来和数据库打交道;
Spring容器主要是用来管理Struts2和Hibernate的,
这样一层一层的将他们连接起来,连接主要使用接口来连接;
先创建一个对象类:
1 package com.gxxy.project.domain; 2 3 public class Student { 4 private Long id; 5 private String name; 6 private String password; 7 private Integer age; 8 private Double fees; 9 public Student() { 10 super(); 11 } 12 public Student(Long id) { 13 super(); 14 this.id = id; 15 } 16 public Student(String name, String password, Integer age, Double fees) { 17 super(); 18 this.name = name; 19 this.password = password; 20 this.age = age; 21 this.fees = fees; 22 } 23 public Student(Long id, String name, String password, Integer age, Double fees) { 24 super(); 25 this.id = id; 26 this.name = name; 27 this.password = password; 28 this.age = age; 29 this.fees = fees; 30 } 31 public Long getId() { 32 return id; 33 } 34 public void setId(Long id) { 35 this.id = id; 36 } 37 public String getName() { 38 return name; 39 } 40 public void setName(String name) { 41 this.name = name; 42 } 43 public String getPassword() { 44 return password; 45 } 46 public void setPassword(String password) { 47 this.password = password; 48 } 49 public Integer getAge() { 50 return age; 51 } 52 public void setAge(Integer age) { 53 this.age = age; 54 } 55 public Double getFees() { 56 return fees; 57 } 58 public void setFees(Double fees) { 59 this.fees = fees; 60 } 61 @Override 62 public String toString() { 63 return "Student [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", fees=" + fees 64 + "]"; 65 } 66 }
这里还要给对象类设置*.hbm.xml配置
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.gxxy.project.domain"> 7 <class name="Student"> 8 <id name="id"> 9 <generator class="native"></generator> 10 </id> 11 <property name="name"></property> 12 <property name="password"></property> 13 <property name="age"></property> 14 <property name="fees"></property> 15 </class> 16 17 </hibernate-mapping>
首先是dao层:里面主要是CRUD(Create Recrive Update Delete),也就是增删改查;
接口:
1 package com.gxxy.project.dao; 2 3 import java.util.List; 4 5 import com.gxxy.project.domain.Student; 6 7 public interface IStudentDao { 8 public void save(Student stu); 9 10 public void delete(Student stu); 11 12 public void update(Student stu); 13 14 public Student get(Student stu); 15 16 public List<Student> list(); 17 }
实现接口的实现类:
1 package com.gxxy.project.dao.impl; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 9 import com.gxxy.project.dao.IStudentDao; 10 import com.gxxy.project.domain.Student; 11 12 public class StudentDaoImpl implements IStudentDao { 13 14 private SessionFactory sessionFactory; 15 16 public void setSessionFactory(SessionFactory sessionFactory) { 17 this.sessionFactory = sessionFactory; 18 } 19 20 @Override 21 public void save(Student stu) { 22 Session session = sessionFactory.getCurrentSession(); 23 session.save(stu); 24 25 } 26 27 @Override 28 public void delete(Student stu) { 29 Session session = sessionFactory.getCurrentSession(); 30 session.delete(stu); 31 32 } 33 34 @Override 35 public void update(Student stu) { 36 Session session = sessionFactory.getCurrentSession(); 37 session.update(stu); 38 39 } 40 41 @Override 42 public Student get(Student stu) { 43 Session session = sessionFactory.getCurrentSession(); 44 return (Student) session.get(Student.class, stu.getId()); 45 } 46 47 @SuppressWarnings("unchecked") 48 @Override 49 public List<Student> list() { 50 Session session = sessionFactory.getCurrentSession(); 51 Query query = session.createQuery("SELECT stu FROM Student stu"); 52 return query.list(); 53 } 54 55 }
设置service层,里面主要是为了给dao层的CRUD设置事物:
接口:
1 package com.gxxy.project.service; 2 3 import java.util.List; 4 5 import com.gxxy.project.domain.Student; 6 7 public interface IStudentService { 8 public void save(Student stu); 9 10 public void delete(Student stu); 11 12 public void update(Student stu); 13 14 public Student get(Student stu); 15 16 public List<Student> list(); 17 }
实现 service接口的实现类:
1 package com.gxxy.project.service.impl; 2 3 import java.util.List; 4 5 import com.gxxy.project.dao.IStudentDao; 6 import com.gxxy.project.domain.Student; 7 import com.gxxy.project.service.IStudentService; 8 9 public class StudentServiceImpl implements IStudentService { 10 11 private IStudentDao dao; 12 13 public void setDao(IStudentDao dao) { 14 this.dao = dao; 15 } 16 17 @Override 18 public void save(Student stu) { 19 dao.save(stu); 20 } 21 22 @Override 23 public void delete(Student stu) { 24 dao.delete(stu); 25 26 } 27 28 @Override 29 public void update(Student stu) { 30 dao.update(stu); 31 32 } 33 34 @Override 35 public Student get(Student stu) { 36 // TODO Auto-generated method stub 37 return dao.get(stu); 38 } 39 40 @Override 41 public List<Student> list() { 42 // TODO Auto-generated method stub 43 return dao.list(); 44 } 45 46 }
下来就是MVC层:
1 package com.gxxy.project.mvc; 2 3 import java.util.List; 4 5 import com.gxxy.project.domain.Student; 6 import com.gxxy.project.service.IStudentService; 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionSupport; 9 10 public class StudentAction extends ActionSupport { 11 12 private static final long serialVersionUID = 1L; 13 private IStudentService service; 14 public void setService(IStudentService service) { 15 this.service = service; 16 } 17 18 public String list() { 19 List<Student> list = service.list(); 20 //获取一个context,来将数据通过struts传到jsp页面 21 ActionContext context = ActionContext.getContext(); 22 context.put("key", list); 23 24 //System.out.println(list); 25 return "list"; 26 } 27 }
在完成这些的时候,需要导入一些相关的jar包:具体就不罗列了;
再下来就是使用XML文件将他们连接起来:ApplocaltionContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd"> 15 16 <!-- 配置连接数据库的连接池 --> 17 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 18 destroy-method="close"> 19 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 20 <property name="url" value="jdbc:mysql:///spring_jdbc"></property> 21 <property name="username" value="root"></property> 22 <property name="password" value="root"></property> 23 </bean> 24 25 <!-- 配置session工厂:SessionFactory --> 26 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > 27 <!-- 配置数据库连接的信息 name里面的属性是class里面的字段,ref(referenced)属性引用的是连接数据库的连接池--> 28 <property name="dataSource" ref="dataSource"></property> 29 30 <!-- 配置Hibernate的常规配置 --> 31 <property name="hibernateProperties"> 32 <!-- 方言、打印hql语句、自动创表 --> 33 <value> 34 hibernate.dialect=org.hibernate.dialect.MySQLDialect 35 hibernate.show_sql=true 36 hibernate.hbm2ddl.auto=none 37 </value> 38 </property> 39 40 <!-- 配置*.hbm.xml的信息 --> 41 <property name="mappingDirectoryLocations" value="classpath:com/gxxy/project/domain"></property> 42 </bean> 43 44 <!-- 配置dao --> 45 <bean id="dao" class="com.gxxy.project.dao.impl.StudentDaoImpl"> 46 <property name="sessionFactory" ref="sessionFactory"></property> 47 </bean> 48 49 <!-- 配置Service --> 50 <bean id="service" class="com.gxxy.project.service.impl.StudentServiceImpl"> 51 <property name="dao" ref="dao"></property> 52 </bean> 53 54 <!-- 配置事物管理器 --> 55 <bean id="txmag" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 56 <property name="sessionFactory" ref="sessionFactory"></property> 57 </bean> 58 59 <!-- 配置事务 --> 60 <aop:config> 61 <aop:pointcut expression="execution(* com.gxxy.project.service.*.*(..))" id="tran"/> 62 <aop:advisor advice-ref="adv" pointcut-ref="tran"/> 63 </aop:config> 64 <tx:advice id="adv" transaction-manager="txmag"> 65 <tx:attributes> 66 <tx:method name="get*" read-only="true"/> 67 <tx:method name="list*" read-only="true"/> 68 <tx:method name="*"/> 69 </tx:attributes> 70 </tx:advice> 71 <!-- 配置Action --> 72 <bean id="stuAction" class="com.gxxy.project.mvc.StudentAction"> 73 <property name="service" ref="service"></property> 74 </bean> 75 76 77 </beans>
Struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name="struts.devMode" value="ture"></constant> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 9 10 <package name="strutsact" extends="struts-default"> 11 <action name="stu_*" class="stuAction" method="{1}"> 12 <result name="list">WEB-INF/views/list.jsp</result> 13 </action> 14 </package> 15 </struts>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 4 id="WebApp_ID" version="3.0"> 5 <!-- 配置一个监听器 --> 6 <listener> 7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 8 </listener> 9 <!-- 引入ApplocaltionContext.xml文件 --> 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:Applcationcontext.xml</param-value> 13 </context-param> 14 <!-- 配置过滤器 --> 15 <filter> 16 <filter-name>struts2</filter-name> 17 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 18 </filter> 19 <filter-mapping> 20 <filter-name>struts2</filter-name> 21 <url-pattern>/*</url-pattern> 22 </filter-mapping> 23 24 </web-app>
最后在创建一个JSP页面,这样就在不使用注解的情况下将三个框架简单的连接起来了;