SSH测试
1、Articles
1.1、 实体类--Articles
1 packagecom.SSHTest.entity;2
3 importcom.yuan.base.entity.BaseEntity;4
5 public class Articles extendsBaseEntity {6
7 private static final long serialVersionUID = 1L;8 private intId;9 privateString Title;10 privateString Body;11 public intgetId() {12 returnId;13 }14 public void setId(intid) {15 Id =id;16 }17 publicString getTitle() {18 returnTitle;19 }20 public voidsetTitle(String title) {21 Title =title;22 }23 publicString getBody() {24 returnBody;25 }26 public voidsetBody(String body) {27 Body =body;28 }29 @Override30 publicString toString() {31 return "Articles [Id=" + Id + ", Title=" + Title + ", Body=" + Body + "]";32 }33 public Articles(intid, String title, String body) {34 super();35 Id =id;36 Title =title;37 Body =body;38 }39 publicArticles() {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 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6
7
8
9
10
11
12
13
14
1.3、biz--AritclesBiz
1 packagecom.SSHTest.biz;2
3 importjava.util.List;4
5 importcom.SSHTest.entity.Articles;6
7
8
9 public interfaceArticlesBiz {10 public Listlist();11 public intadd(Articles articles);12 public intedit(Articles articles);13 public intdelete(Articles articles);14
15 }
biz实现类ArticlesBizImpl
1 packagecom.SSHTest.biz.impl;2
3 importjava.util.List;4
5 importcom.SSHTest.biz.ArticlesBiz;6 importcom.SSHTest.dao.ArticlesDao;7 importcom.SSHTest.entity.Articles;8
9
10
11 public class ArticlesBizImpl implementsArticlesBiz {12
13 privateArticlesDao articlesDao ;14
15
16
17 publicArticlesDao getArticlesDao() {18 returnarticlesDao;19 }20
21 public voidsetArticlesDao(ArticlesDao articlesDao) {22 this.articlesDao =articlesDao;23 }24
25 @Override26 public Listlist() {27 //TODO Auto-generated method stub
28 returnarticlesDao.list();29 }30
31 @Override32 public intadd(Articles articles) {33 //TODO Auto-generated method stub
34 returnarticlesDao.add(articles);35 }36
37 @Override38 public intedit(Articles articles) {39 //TODO Auto-generated method stub
40 returnarticlesDao.edit(articles);41 }42
43 @Override44 public intdelete(Articles articles) {45 //TODO Auto-generated method stub
46 returnarticlesDao.delete(articles);47 }48
49
50
51 }
1.4、dao层-ArticlesDao
1 packagecom.SSHTest.dao;2
3 importjava.io.Serializable;4 importjava.util.List;5
6 importorg.hibernate.HibernateException;7 importorg.hibernate.Session;8 importorg.springframework.orm.hibernate5.HibernateCallback;9
10 importcom.SSHTest.entity.Articles;11 importcom.yuan.base.dao.BaseDao;12
13
14
15 public class ArticlesDao extendsBaseDao {16
17
18
19 private static final long serialVersionUID = 1L;20
21 public Listlist(){22
23 return this.getHibernateTemplate().execute(new HibernateCallback>() {24
25 @Override26 public List doInHibernate(Session session) throwsHibernateException {27
28
29 return session.createQuery("from Articles").list();30 }31 });32 }33
34
35 public intadd(Articles articles) {36
37 return (int) this.getHibernateTemplate().save(articles);38 }39
40 public intedit(Articles articles) {41
42 this.getHibernateTemplate().update(articles);43
44 return 1;45 }46
47 public intdelete(Articles articles) {48
49 this.getHibernateTemplate().delete(articles);50
51 return 1;52 }53
54
55 }
1.5、web层-ArticlesAction
1 packagecom.SSHTest.web;2
3 importjava.util.List;4
5 importcom.SSHTest.biz.ArticlesBiz;6 importcom.SSHTest.entity.Articles;7 importcom.opensymphony.xwork2.ModelDriven;8 importcom.yuan.base.web.BaseAction;9
10 public class ArticlesAction extends BaseAction implements ModelDriven{11
12
13 private static final long serialVersionUID = 1L;14
15 private Articles articles = newArticles();16
17 privateArticlesBiz articlesBiz ;18
19
20 publicArticlesBiz getArticlesBiz() {21 returnarticlesBiz;22 }23
24
25 public voidsetArticlesBiz(ArticlesBiz articlesBiz) {26 this.articlesBiz =articlesBiz;27 }28
29
30 publicString list() {31 List list =articlesBiz.list();32 for(Articles a : list) {33 System.out.println(a);34 }35
36 return null;37 }38
39 publicString add() {40 articlesBiz.add(articles);41
42 return null;43 }44 publicString edit() {45 articlesBiz.edit(articles);46
47
48 return null;49 }50 publicString del() {51 articlesBiz.delete(articles);52
53 return null;54 }55
56
57 @Override58 publicArticles getModel() {59 //TODO Auto-generated method stub
60 returnarticles;61 }62
63
64
65 }
1.6、spring-articles.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
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/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd
9 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-aop-4.3.xsd
10 http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
11
12
13
14
15
16
17
18
19
20
21
1.7、struts-articles.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4 "http://struts.apache.org/dtds/struts-2.5.dtd">
5
6
7
8
9
10
11
2、User
2.1、User实体类
1 packagecom.SSHTest.entity;2
3 importcom.yuan.base.entity.BaseEntity;4
5 public class User extendsBaseEntity {6
7
8 private static final long serialVersionUID = 1L;9 privateString uname;10 privateString upwd;11 publicString getUname() {12 returnuname;13 }14 public voidsetUname(String uname) {15 this.uname =uname;16 }17 publicString getUpwd() {18 returnupwd;19 }20 public voidsetUpwd(String upwd) {21 this.upwd =upwd;22 }23 @Override24 publicString toString() {25 return "User [uname=" + uname + ", upwd=" + upwd + "]";26 }27 publicUser() {28 super();29 //TODO Auto-generated constructor stub
30 }31 publicUser(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 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6
7
8
9
10
11
2.2、biz--UserBiz
1 packagecom.SSHTest.biz;2
3 importjava.util.List;4
5 importcom.SSHTest.entity.TreeNode;6 importcom.SSHTest.entity.User;7
8
9
10 public interfaceUserBiz {11 public Listlist(User user);12 public intadd(User user);13 public ListlistNode();14
15 }
biz实现类UserBizImpl
1 packagecom.SSHTest.biz.impl;2
3 importjava.util.List;4
5 importcom.SSHTest.biz.UserBiz;6 importcom.SSHTest.dao.UserDao;7 importcom.SSHTest.entity.TreeNode;8 importcom.SSHTest.entity.User;9
10
11
12 public class UserBizImpl implementsUserBiz {13
14 privateUserDao userDao ;15
16
17
18
19 publicUserDao getUserDao() {20 returnuserDao;21 }22
23 public voidsetUserDao(UserDao userDao) {24 this.userDao =userDao;25 }26
27 @Override28 public Listlist(User user) {29 returnuserDao.list(user);30 }31
32 @Override33 public intadd(User user) {34 //TODO Auto-generated method stub
35 returnuserDao.add(user);36 }37
38
39 @Override40 public ListlistNode() {41 //TODO Auto-generated method stub
42 returnuserDao.listNode();43 }44
45 }
2.3、dao层--UserDao
1 packagecom.SSHTest.dao;2
3 importjava.io.Serializable;4 importjava.util.List;5
6 importorg.hibernate.HibernateException;7 importorg.hibernate.Session;8 importorg.hibernate.query.Query;9 importorg.springframework.orm.hibernate5.HibernateCallback;10
11 importcom.SSHTest.entity.TreeNode;12 importcom.SSHTest.entity.User;13 importcom.yuan.base.dao.BaseDao;14 importcom.yuan.base.util.StringUtils;15
16
17
18 public class UserDao extendsBaseDao {19
20
21 private static final long serialVersionUID = 1L;22
23 public Listlist(User user){24
25 return this.getHibernateTemplate().execute(new HibernateCallback>() {26
27 @Override28 public List doInHibernate(Session arg0) throwsHibernateException {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 returnquery.list();38 }39 });40 }41
42 public intadd(User user) {43 Serializable a = this.getHibernateTemplate().save(user);44 int n = 0;45 if(StringUtils.isNotBlank(a+"")) {46 n=1;47 }48 returnn;49 }50
51 public ListlistNode(){52
53 return this.getHibernateTemplate().execute(new HibernateCallback>() {54
55 @Override56 public List doInHibernate(Session session) throwsHibernateException {57 return session.createQuery("from TreeNode").list();58 }59 });60 }61
62
63
64 }
2.4、web层--UserAction
1 packagecom.SSHTest.web;2
3 importjava.util.List;4
5 importcom.SSHTest.biz.UserBiz;6 importcom.SSHTest.entity.TreeNode;7 importcom.SSHTest.entity.User;8 importcom.opensymphony.xwork2.ModelDriven;9 importcom.yuan.base.web.BaseAction;10
11 public class UserAction extends BaseAction implements ModelDriven{12
13
14 private static final long serialVersionUID = 1L;15
16 private User user = newUser();17
18 privateUserBiz userBiz ;19
20
21 publicUser getUser() {22 returnuser;23 }24
25 public voidsetUser(User user) {26 this.user =user;27 }28
29
30 publicString dologin() {31
32 List 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 publicString 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 publicString list() {56
57 List list =userBiz.list(user);58 for(User user : list) {59 System.out.println(user);60 }61
62 return null;63 }64
65 publicString listNode() {66 List listNode =userBiz.listNode();67 for(TreeNode treeNode : listNode) {68 System.out.println(treeNode);69 }70
71 return null;72 }73
74
75 publicUserBiz getUserBiz() {76 returnuserBiz;77 }78
79
80 public voidsetUserBiz(UserBiz userBiz) {81 this.userBiz =userBiz;82 }83
84
85
86 @Override87 publicUser getModel() {88 //TODO Auto-generated method stub
89 returnuser;90 }91 }
2.5、spring-user.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
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/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd
8 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">
9
10
11
12
13
14
15
16
17
18
19
2.6、struts-user.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4 "http://struts.apache.org/dtds/struts-2.5.dtd">
5
6
7
8
9
10
2.7、TreeNode实体类
1 packagecom.SSHTest.entity;2
3 importcom.yuan.base.entity.BaseEntity;4
5 public class TreeNode extendsBaseEntity {6
7
8 private static final long serialVersionUID = 1L;9 private inttreenodeid;10 privateString treenodename;11 private inttreenodetype;12 private intparentnodeid;13 privateString url;14 private intposition;15 privateString icon;16 public intgetTreenodeid() {17 returntreenodeid;18 }19 public void setTreenodeid(inttreenodeid) {20 this.treenodeid =treenodeid;21 }22 publicString getTreenodename() {23 returntreenodename;24 }25 public voidsetTreenodename(String treenodename) {26 this.treenodename =treenodename;27 }28 public intgetTreenodetype() {29 returntreenodetype;30 }31 public void setTreenodetype(inttreenodetype) {32 this.treenodetype =treenodetype;33 }34 public intgetParentnodeid() {35 returnparentnodeid;36 }37 public void setParentnodeid(intparentnodeid) {38 this.parentnodeid =parentnodeid;39 }40 publicString getUrl() {41 returnurl;42 }43 public voidsetUrl(String url) {44 this.url =url;45 }46 public intgetPosition() {47 returnposition;48 }49 public void setPosition(intposition) {50 this.position =position;51 }52 publicString getIcon() {53 returnicon;54 }55 public voidsetIcon(String icon) {56 this.icon =icon;57 }58 @Override59 publicString toString() {60 return "TreeNode [treenodeid=" + treenodeid + ", treenodename=" + treenodename + ", treenodetype="
61 + treenodetype + ", parentnodeid=" + parentnodeid + ", url=" + url + ", position=" +position62 + ", icon=" + icon + "]";63 }64 public TreeNode(int treenodeid, String treenodename, int treenodetype, int parentnodeid, String url, intposition,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 publicTreeNode() {76 super();77 //TODO Auto-generated constructor stub
78 }79
80
81 }
实体类映射文件Treenode.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
3、xml配置
3.1、spring-hibernate.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">
11
12
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
45
46
47
48
49
50
51
52
53
55
56
57
59
60
61
62
63
64 class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
65
66
67
68
69
70
71
72 org.hibernate.dialect.MySQLDialect
73
74 true
75 true
76
77
78
79
80
81 com/yuan/book/entity/book.hbm.xml
82 com/SSHTest/entity/Aricles.hbm.xml
83 com/SSHTest/entity/Treenode.hbm.xml
84 com/SSHTest/entity/User.hbm.xml
85
86
87
88
89
90
91
100
101
102
103
104
105
106 class="org.springframework.orm.hibernate5.HibernateTransactionManager">
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
3.2、spring-context.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd
10 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">
11
12
13
14
15
16
17
3.3、struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4 "http://struts.apache.org/dtds/struts-2.5.dtd">
5
6
7
8
9
10
11
谢谢观看!!!