SSH框架测试

情到浓时终转凉″ 提交于 2019-11-27 19:44:32

SSH测试

1、Articles

1.1、 实体类--Articles

 1 package com.SSHTest.entity;
 2 
 3 import com.yuan.base.entity.BaseEntity;
 4 
 5 public class Articles extends BaseEntity {
 6 
 7     private static final long serialVersionUID = 1L;
 8     private int Id;
 9     private String Title;
10     private String Body;
11     public int getId() {
12         return Id;
13     }
14     public void setId(int id) {
15         Id = id;
16     }
17     public String getTitle() {
18         return Title;
19     }
20     public void setTitle(String title) {
21         Title = title;
22     }
23     public String getBody() {
24         return Body;
25     }
26     public void setBody(String body) {
27         Body = body;
28     }
29     @Override
30     public String toString() {
31         return "Articles [Id=" + Id + ", Title=" + Title + ", Body=" + Body + "]";
32     }
33     public Articles(int id, String title, String body) {
34         super();
35         Id = id;
36         Title = title;
37         Body = body;
38     }
39     public Articles() {
40         super();
41         // TODO Auto-generated constructor stub
42     }
43 
44     
45     
46 
47 }

1.2、实体映射文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 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 <hibernate-mapping>
 6     <class table="t_vue_articles" name="com.SSHTest.entity.Articles">
 7         <id name="Id" type="java.lang.Integer" column="id">
 8             <generator class="increment"></generator>
 9         </id>
10         
11         <property name="Ttitle" type="java.lang.String" column="title"></property>
12         <property name="Body" type="java.lang.String" column="body"></property>
13     </class>
14 </hibernate-mapping>

1.3、biz--AritclesBiz

 1 package com.SSHTest.biz;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.entity.Articles;
 6 
 7 
 8 
 9 public interface ArticlesBiz {
10     public List<Articles> list();
11     public int add(Articles articles);
12     public int edit(Articles articles);
13     public int delete(Articles articles);
14 
15 }

  biz实现类ArticlesBizImpl

 1 package com.SSHTest.biz.impl;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.biz.ArticlesBiz;
 6 import com.SSHTest.dao.ArticlesDao;
 7 import com.SSHTest.entity.Articles;
 8 
 9 
10 
11 public class ArticlesBizImpl implements ArticlesBiz {
12     
13     private ArticlesDao articlesDao ;
14     
15     
16     
17     public ArticlesDao getArticlesDao() {
18         return articlesDao;
19     }
20 
21     public void setArticlesDao(ArticlesDao articlesDao) {
22         this.articlesDao = articlesDao;
23     }
24 
25     @Override
26     public List<Articles> list() {
27         // TODO Auto-generated method stub
28         return articlesDao.list();
29     }
30 
31     @Override
32     public int add(Articles articles) {
33         // TODO Auto-generated method stub
34         return articlesDao.add(articles);
35     }
36 
37     @Override
38     public int edit(Articles articles) {
39         // TODO Auto-generated method stub
40         return articlesDao.edit(articles);
41     }
42 
43     @Override
44     public int delete(Articles articles) {
45         // TODO Auto-generated method stub
46         return articlesDao.delete(articles);
47     }
48     
49     
50 
51 }

1.4、dao层-ArticlesDao

 1 package com.SSHTest.dao;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 import org.hibernate.HibernateException;
 7 import org.hibernate.Session;
 8 import org.springframework.orm.hibernate5.HibernateCallback;
 9 
10 import com.SSHTest.entity.Articles;
11 import com.yuan.base.dao.BaseDao;
12 
13 
14 
15 public class ArticlesDao extends BaseDao {
16 
17     
18     
19     private static final long serialVersionUID = 1L;
20 
21     public List<Articles> list(){
22         
23         return this.getHibernateTemplate().execute(new HibernateCallback<List<Articles>>() {
24 
25             @Override
26             public List<Articles> doInHibernate(Session session) throws HibernateException {
27                 
28                 
29                 return session.createQuery("from Articles").list();
30             }
31         });
32     }
33     
34     
35     public int add(Articles articles) {
36         
37         return (int) this.getHibernateTemplate().save(articles);
38     }
39     
40     public int edit(Articles articles) {
41         
42         this.getHibernateTemplate().update(articles);
43         
44         return 1;
45     }
46     
47     public int delete(Articles articles) {
48         
49         this.getHibernateTemplate().delete(articles);
50             
51         return 1;
52     }
53     
54 
55 }

1.5、web层-ArticlesAction

 1 package com.SSHTest.web;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.biz.ArticlesBiz;
 6 import com.SSHTest.entity.Articles;
 7 import com.opensymphony.xwork2.ModelDriven;
 8 import com.yuan.base.web.BaseAction;
 9 
10 public class ArticlesAction extends BaseAction implements ModelDriven<Articles> {
11 
12     
13     private static final long serialVersionUID = 1L;
14 
15     private Articles articles = new Articles();
16     
17     private ArticlesBiz articlesBiz ;
18     
19 
20     public ArticlesBiz getArticlesBiz() {
21         return articlesBiz;
22     }
23 
24 
25     public void setArticlesBiz(ArticlesBiz articlesBiz) {
26         this.articlesBiz = articlesBiz;
27     }
28 
29     
30     public String list() {
31         List<Articles> list = articlesBiz.list();
32         for (Articles a : list) {
33             System.out.println(a);
34         }
35         
36         return null;
37     }
38     
39     public String add() {
40         articlesBiz.add(articles);
41         
42         return null;
43     }
44     public String edit() {
45         articlesBiz.edit(articles);
46         
47     
48         return null;
49     }
50     public String del() {
51         articlesBiz.delete(articles);
52     
53         return null;
54     }
55     
56 
57     @Override
58     public Articles getModel() {
59         // TODO Auto-generated method stub
60         return articles;
61     }
62     
63     
64 
65 }

1.6、spring-articles.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-aop-4.3.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
11 
12 
13 <bean id="articlesDao" class="com.SSHTest.dao.ArticlesDao" parent="baseDao" ></bean>
14         <bean id="articlesBiz" class="com.SSHTest.biz.impl.ArticlesBizImpl" parent="baseBiz" >
15             <property name="articlesDao" ref="articlesDao"></property>
16         </bean>
17         
18         <bean id="articlesAction" class="com.SSHTest.web.ArticlesAction" parent="baseAction">
19             <property name="articlesBiz" ref="articlesBiz"></property>
20         </bean>
21 </beans>

1.7、struts-articles.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 4     "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 <struts>
 6     <package name="articles" extends="base" namespace="/articles">
 7         <action name="/articles_*" class="articlesAction" method="{1}">
 8             
 9         </action>
10     </package>
11 </struts>

 

2、User

2.1、User实体类

 1 package com.SSHTest.entity;
 2 
 3 import com.yuan.base.entity.BaseEntity;
 4 
 5 public class User extends BaseEntity {
 6 
 7     
 8     private static final long serialVersionUID = 1L;
 9     private String uname;
10     private String upwd;
11     public String getUname() {
12         return uname;
13     }
14     public void setUname(String uname) {
15         this.uname = uname;
16     }
17     public String getUpwd() {
18         return upwd;
19     }
20     public void setUpwd(String upwd) {
21         this.upwd = upwd;
22     }
23     @Override
24     public String toString() {
25         return "User [uname=" + uname + ", upwd=" + upwd + "]";
26     }
27     public User() {
28         super();
29         // TODO Auto-generated constructor stub
30     }
31     public User(String uname, String upwd) {
32         super();
33         this.uname = uname;
34         this.upwd = upwd;
35     }
36     
37 }

   实体类映射文件User.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 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 <hibernate-mapping>
 6     <class table="t_vue_user" name="com.SSHTest.entity.User">
 7         <id name="uname" type="java.lang.String" column="uname"></id>
 8         
 9         <property name="upwd" type="java.lang.String" column="pwd"></property>
10     </class>
11 </hibernate-mapping>

2.2、biz--UserBiz

 1 package com.SSHTest.biz;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.entity.TreeNode;
 6 import com.SSHTest.entity.User;
 7 
 8 
 9 
10 public interface UserBiz {
11     public List<User> list(User user);
12     public int add(User user);
13     public List<TreeNode> listNode();
14 
15 }

  biz实现类UserBizImpl

 1 package com.SSHTest.biz.impl;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.biz.UserBiz;
 6 import com.SSHTest.dao.UserDao;
 7 import com.SSHTest.entity.TreeNode;
 8 import com.SSHTest.entity.User;
 9 
10 
11 
12 public class UserBizImpl implements UserBiz {
13     
14     private UserDao userDao ;
15     
16     
17     
18 
19     public UserDao getUserDao() {
20         return userDao;
21     }
22 
23     public void setUserDao(UserDao userDao) {
24         this.userDao = userDao;
25     }
26 
27     @Override
28     public List<User> list(User user) {
29         return userDao.list(user);
30     }
31 
32     @Override
33     public int add(User user) {
34         // TODO Auto-generated method stub
35         return userDao.add(user);
36     }
37 
38 
39     @Override
40     public List<TreeNode> listNode() {
41         // TODO Auto-generated method stub
42         return userDao.listNode();
43     }
44 
45 }

2.3、dao层--UserDao

 1 package com.SSHTest.dao;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 import org.hibernate.HibernateException;
 7 import org.hibernate.Session;
 8 import org.hibernate.query.Query;
 9 import org.springframework.orm.hibernate5.HibernateCallback;
10 
11 import com.SSHTest.entity.TreeNode;
12 import com.SSHTest.entity.User;
13 import com.yuan.base.dao.BaseDao;
14 import com.yuan.base.util.StringUtils;
15 
16 
17 
18 public class UserDao extends BaseDao {
19 
20     
21     private static final long serialVersionUID = 1L;
22 
23     public List<User> list(User user){
24         
25         return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
26 
27             @Override
28             public List<User> doInHibernate(Session arg0) throws HibernateException {
29                 Query query = arg0.createQuery("from User");
30                 String uname = user.getUname();
31                 String upwd = user.getUpwd();
32                 if(StringUtils.isNotBlank(uname)&& StringUtils.isNotBlank(upwd)) {
33                          query = arg0.createQuery("from User where uname = :uname and upwd = :upwd ");
34                          query.setParameter("uname", uname);
35                          query.setParameter("upwd", upwd);
36                 }
37                 return query.list();
38             }
39         });
40     }
41     
42     public int add(User user) {
43         Serializable a = this.getHibernateTemplate().save(user);
44         int n = 0;
45         if(StringUtils.isNotBlank(a+"")) {
46             n=1;
47         }
48         return  n;
49     }
50     
51     public List<TreeNode> listNode(){
52         
53         return this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {
54 
55             @Override
56             public List<TreeNode> doInHibernate(Session session) throws HibernateException {
57                 return session.createQuery("from TreeNode").list();
58             }
59         });
60     }
61     
62     
63 
64 }

2.4、web层--UserAction

 1 package com.SSHTest.web;
 2 
 3 import java.util.List;
 4 
 5 import com.SSHTest.biz.UserBiz;
 6 import com.SSHTest.entity.TreeNode;
 7 import com.SSHTest.entity.User;
 8 import com.opensymphony.xwork2.ModelDriven;
 9 import com.yuan.base.web.BaseAction;
10 
11 public class UserAction extends BaseAction implements ModelDriven<User> {
12 
13     
14     private static final long serialVersionUID = 1L;
15 
16     private User user = new User();
17     
18     private UserBiz userBiz ;
19     
20     
21     public User getUser() {
22         return user;
23     }
24 
25     public void setUser(User user) {
26         this.user = user;
27     }
28 
29 
30     public String dologin() {
31         
32         List<User> list = userBiz.list(user);
33         if(list.size()>0) {
34             System.out.println("登录成功");
35         }
36         else {
37             System.out.println("登录失败,用户名或密码错误");
38         }
39         return null;
40     }
41     
42     public String addUser() {
43         
44         int n = userBiz.add(user);
45         if(n>0) {
46             System.out.println("注册成功");
47         }
48         else {
49             System.out.println("注册失败");
50         }
51         
52         return null;
53     }
54 
55     public String  list() {
56         
57         List<User> list = userBiz.list(user);
58         for (User user : list) {
59             System.out.println(user);
60         }
61         
62         return null;
63     }
64     
65     public String listNode() {
66         List<TreeNode> listNode = userBiz.listNode();
67         for (TreeNode treeNode : listNode) {
68             System.out.println(treeNode);
69         }
70         
71         return null;
72     }
73     
74 
75     public UserBiz getUserBiz() {
76         return userBiz;
77     }
78 
79 
80     public void setUserBiz(UserBiz userBiz) {
81         this.userBiz = userBiz;
82     }
83 
84 
85 
86     @Override
87     public User getModel() {
88         // TODO Auto-generated method stub
89         return user;
90     }
91 }

2.5、spring-user.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 9     
10         <bean id="userDao" class="com.SSHTest.dao.UserDao" parent="baseDao" ></bean>
11         <bean id="userBiz" class="com.SSHTest.biz.impl.UserBizImpl" parent="baseBiz" >
12             <property name="userDao" ref="userDao"></property>
13         </bean>
14         
15         <bean id="userAction" class="com.SSHTest.web.UserAction" parent="baseAction">
16             <property name="userBiz" ref="userBiz"></property>
17         </bean>
18 
19 </beans>

2.6、struts-user.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 4     "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 <struts>
 6     <package name="user" extends="base" >
 7         <action name="user_*" class="userAction" method="{1}">
 8         </action>
 9     </package>
10 </struts>

 

2.7、TreeNode实体类

 1 package com.SSHTest.entity;
 2 
 3 import com.yuan.base.entity.BaseEntity;
 4 
 5 public class TreeNode extends BaseEntity {
 6 
 7     
 8     private static final long serialVersionUID = 1L;
 9     private int treenodeid;
10     private String treenodename;
11     private int treenodetype;
12     private int parentnodeid;
13     private String url;
14     private int position;
15     private String icon;
16     public int getTreenodeid() {
17         return treenodeid;
18     }
19     public void setTreenodeid(int treenodeid) {
20         this.treenodeid = treenodeid;
21     }
22     public String getTreenodename() {
23         return treenodename;
24     }
25     public void setTreenodename(String treenodename) {
26         this.treenodename = treenodename;
27     }
28     public int getTreenodetype() {
29         return treenodetype;
30     }
31     public void setTreenodetype(int treenodetype) {
32         this.treenodetype = treenodetype;
33     }
34     public int getParentnodeid() {
35         return parentnodeid;
36     }
37     public void setParentnodeid(int parentnodeid) {
38         this.parentnodeid = parentnodeid;
39     }
40     public String getUrl() {
41         return url;
42     }
43     public void setUrl(String url) {
44         this.url = url;
45     }
46     public int getPosition() {
47         return position;
48     }
49     public void setPosition(int position) {
50         this.position = position;
51     }
52     public String getIcon() {
53         return icon;
54     }
55     public void setIcon(String icon) {
56         this.icon = icon;
57     }
58     @Override
59     public String toString() {
60         return "TreeNode [treenodeid=" + treenodeid + ", treenodename=" + treenodename + ", treenodetype="
61                 + treenodetype + ", parentnodeid=" + parentnodeid + ", url=" + url + ", position=" + position
62                 + ", icon=" + icon + "]";
63     }
64     public TreeNode(int treenodeid, String treenodename, int treenodetype, int parentnodeid, String url, int position,
65             String icon) {
66         super();
67         this.treenodeid = treenodeid;
68         this.treenodename = treenodename;
69         this.treenodetype = treenodetype;
70         this.parentnodeid = parentnodeid;
71         this.url = url;
72         this.position = position;
73         this.icon = icon;
74     }
75     public TreeNode() {
76         super();
77         // TODO Auto-generated constructor stub
78     }
79     
80     
81 }

 实体类映射文件Treenode.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 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 <hibernate-mapping>
 6     <class table="t_vue_tree_node" name="com.SSHTest.entity.TreeNode">
 7         <id name="treenodeid" type="java.lang.Integer" column="tree_node_id">
 8             <generator class="increment"></generator>
 9         </id>
10         
11         <property name="treenodename" type="java.lang.String" column="tree_node_name"></property>
12         <property name="treenodetype" type="java.lang.Integer" column="tree_node_type"></property>
13         <property name="parentnodeid" type="java.lang.Integer" column="parent_node_id"></property>
14         <property name="url" type="java.lang.String" column="url"></property>
15         <property name="position" type="java.lang.Integer" column="position"></property>
16         <property name="icon" type="java.lang.String" column="icon"></property>
17     </class>
18 </hibernate-mapping>

 

3、xml配置

3.1、spring-hibernate.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4    xmlns:context="http://www.springframework.org/schema/context"
  5    xmlns:tx="http://www.springframework.org/schema/tx"
  6    xmlns:aop="http://www.springframework.org/schema/aop"
  7    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  8        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
 10        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 11  
 12      <!-- 
 13          1、注册数据库连接信息文件
 14          2、配置连接池
 15          3、配置sessionfactory
 16          4、配置事务
 17          5、配置切面
 18          6、配置通用模块
 19       -->
 20  <!-- 1、注册jdbc相关的配置文件 -->
 21     <context:property-placeholder location="classpath:db.properties" />
 22  
 23     <!-- 配置c3p0连接池 -->
 24     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 25         <property name="user" value="${db.username}"></property>
 26         <property name="password" value="${db.password}"></property>
 27         <property name="driverClass" value="${db.driverClass}"></property>
 28         <property name="jdbcUrl" value="${db.jdbcUrl}"></property>
 29 
 30         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
 31         <property name="initialPoolSize" value="${db.initialPoolSize}"></property>
 32         <!--连接池中保留的最大连接数。Default: 15 -->
 33         <property name="maxPoolSize" value="${db.maxPoolSize}"></property>
 34         <!--连接池中保留的最小连接数。 -->
 35         <property name="minPoolSize" value="${db.minPoolSize}" />
 36         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
 37         <property name="maxIdleTime" value="${db.maxIdleTime}" />
 38 
 39         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
 40         <property name="acquireIncrement" value="${db.acquireIncrement}" />
 41 
 42         <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。 
 43             所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
 44             0 -->
 45         <property name="maxStatements" value="${db.maxStatements}" />
 46 
 47         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
 48         <property name="idleConnectionTestPeriod" value="${db.idleConnectionTestPeriod}" />
 49 
 50         <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
 51         <property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}" />
 52 
 53         <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。 
 54             如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
 55         <property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}" />
 56 
 57         <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod 
 58             或automaticTestTable 等方法来提升连接测试的性能。Default: false -->
 59         <property name="testConnectionOnCheckout" value="${db.breakAfterAcquireFailure}" />
 60     </bean>
 61 
 62     <!-- 3、配置sessionfactory相关信息 -->
 63     <bean id="sessionFactory"
 64         class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
 65         <!-- 数据源 -->
 66         <property name="dataSource">
 67             <ref bean="dataSource" />
 68         </property>
 69         <!-- hibernate相关属性 -->
 70         <property name="hibernateProperties">
 71             <props>
 72                 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
 73                 <!--spring与Hibernate集成无法显示sql语句问题,请见集成后hibernate无法显示sql语句.txt -->
 74                 <prop key="hibernate.show_sql">true</prop>
 75                 <prop key="hibernate.format_sql">true</prop>
 76             </props>
 77         </property>
 78         <!-- 实体映射文件 -->
 79         <property name="mappingResources">
 80             <list>
 81                 <value>com/yuan/book/entity/book.hbm.xml</value>
 82                 <value>com/SSHTest/entity/Aricles.hbm.xml</value>
 83                 <value>com/SSHTest/entity/Treenode.hbm.xml</value>
 84                 <value>com/SSHTest/entity/User.hbm.xml</value>
 85             </list>
 86         </property>
 87     </bean>
 88 
 89     <!-- 4、配置事务 -->
 90     <!--声明式事务配置开始 -->
 91     <!-- 
 92         静态代理:
 93             一个代理对象->一个目标对象
 94             BookProxy(BookBizImpl+myMethodBeforeAdvice)->bookBiz
 95             OrderProxy(OrderBizImpl+myMethodBeforeAdvice2)->    OrderBiz
 96         
 97         动态代理:
 98             一个代理对象->多个目标对象
 99      -->
100     
101     <!--1) 开启自动代理 -->
102     <aop:aspectj-autoproxy />
103 
104     <!--2) 事务管理器 -->
105     <bean id="transactionManager"
106         class="org.springframework.orm.hibernate5.HibernateTransactionManager">
107         <property name="sessionFactory" ref="sessionFactory" />
108     </bean>
109 
110     <!--3) 定义事务特性 -->
111     <tx:advice id="txAdvice" transaction-manager="transactionManager">
112         <tx:attributes>
113             <tx:method name="add*" propagation="REQUIRED" />
114             <tx:method name="save*" propagation="REQUIRED" />
115             <tx:method name="insert*" propagation="REQUIRED" />
116 
117             <tx:method name="edit*" propagation="REQUIRED" />
118             <tx:method name="update*" propagation="REQUIRED" />
119 
120             <tx:method name="del*" propagation="REQUIRED" />
121             <tx:method name="remove*" propagation="REQUIRED" />
122 
123             <tx:method name="load*" propagation="REQUIRED" read-only="true" />
124             <tx:method name="list*" propagation="REQUIRED" read-only="true" />
125             <tx:method name="select*" propagation="REQUIRED" read-only="true" />
126             <tx:method name="query*" propagation="REQUIRED" read-only="true" />
127 
128             <tx:method name="do*" propagation="REQUIRED" />
129         </tx:attributes>
130     </tx:advice>
131 
132     <!--4) 定义切入点 -->
133     <aop:config>
134         <!-- pointcut属性用来定义一个切入点,分成四个部分理解 [* ][*..][*Biz][.*(..)] -->
135         <!-- A: 返回类型,*表示返回类型不限 -->
136         <!-- B: 包名,*..表示包名不限 -->
137         <!-- C: 类或接口名,*Biz表示类或接口必须以Biz结尾 -->
138         <!-- D: 方法名和参数,*(..)表示方法名不限,参数类型和个数不限 -->
139         <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Biz.*(..))" />
140     </aop:config>
141     <!-- 声明式事务配置结束 -->
142     
143     <!-- 5、配置HibernateTemplate 可以看成session -->
144     <bean class="org.springframework.orm.hibernate5.HibernateTemplate" id="hibernateTemplate">
145         <property name="sessionFactory" ref="sessionFactory"></property>
146     </bean>
147     <!-- 6、分模块开发(只配置base模块) -->
148     <bean class="com.yuan.base.entity.BaseEntity" abstract="true" id="baseEntity"></bean>
149     <bean class="com.yuan.base.dao.BaseDao" abstract="true" id="baseDao" >
150         <property name="hibernateTemplate" ref="hibernateTemplate"></property>
151     </bean>
152     <bean class="com.yuan.base.biz.BaseBiz" abstract="true" id="baseBiz"></bean>
153     <bean class="com.yuan.base.web.BaseAction" abstract="true" id="baseAction"></bean>
154  
155 </beans>

3.2、spring-context.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xmlns:context="http://www.springframework.org/schema/context"
 5    xmlns:tx="http://www.springframework.org/schema/tx"
 6    xmlns:aop="http://www.springframework.org/schema/aop"
 7    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 8        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
11  
12          <import resource="spring-hibernate.xml"/>
13          <import resource="spring-book.xml"/>
14          <import resource="spring-articles.xml"></import>
15          <import resource="spring-user.xml"></import>
16  
17 </beans>

3.3、struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 4     "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 <struts>
 6     <include file="struts-default.xml"></include>
 7     <include file="struts-base.xml"></include>
 8     <include file="struts-book.xml"></include>
 9     <include file="struts-articles.xml"></include>
10     <include file="struts-user.xml"></include>
11 </struts>

 

 

谢谢观看!!!

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!