码迷,mamicode.com
首页 > 编程语言 > 详细

Spring---SSH整合(二)

时间:2019-08-18 22:17:54      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:lan   extends   url   apache   lang   throws   schema   sof   sys   

基于Spring---SSH整合,使用SSH编写后台:

User模块层

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.spring.ssh.user.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>
TreeNode 
 1 package com.spring.ssh.user.entity;
 2 
 3 import com.spring.ssh.base.entity.BaseEntity;
 4 
 5 public class TreeNode extends BaseEntity {
 6     
 7     private int treenodeid;
 8     private String treenodename;
 9     private int treenodetype;
10     private int parentnodeid;
11     private String url;
12     private int position;
13     private String icon;
14     public int getTreenodeid() {
15         return treenodeid;
16     }
17     public void setTreenodeid(int treenodeid) {
18         this.treenodeid = treenodeid;
19     }
20     public String getTreenodename() {
21         return treenodename;
22     }
23     public void setTreenodename(String treenodename) {
24         this.treenodename = treenodename;
25     }
26     public int getTreenodetype() {
27         return treenodetype;
28     }
29     public void setTreenodetype(int treenodetype) {
30         this.treenodetype = treenodetype;
31     }
32     public int getParentnodeid() {
33         return parentnodeid;
34     }
35     public void setParentnodeid(int parentnodeid) {
36         this.parentnodeid = parentnodeid;
37     }
38     public String getUrl() {
39         return url;
40     }
41     public void setUrl(String url) {
42         this.url = url;
43     }
44     public int getPosition() {
45         return position;
46     }
47     public void setPosition(int position) {
48         this.position = position;
49     }
50     public String getIcon() {
51         return icon;
52     }
53     public void setIcon(String icon) {
54         this.icon = icon;
55     }
56     public TreeNode(int treenodeid, String treenodename, int treenodetype, int parentnodeid, String url, int position,
57             String icon) {
58         this.treenodeid = treenodeid;
59         this.treenodename = treenodename;
60         this.treenodetype = treenodetype;
61         this.parentnodeid = parentnodeid;
62         this.url = url;
63         this.position = position;
64         this.icon = icon;
65     }
66     public TreeNode() {
67         
68     }
69     @Override
70     public String toString() {
71         return "TreeNode [treenodeid=" + treenodeid + ", treenodename=" + treenodename + ", treenodetype="
72                 + treenodetype + ", parentnodeid=" + parentnodeid + ", url=" + url + ", position=" + position
73                 + ", icon=" + icon + "]";
74     }
75     
76 
77 }
User 
 1 package com.spring.ssh.user.entity;
 2 
 3 import com.spring.ssh.base.entity.BaseEntity;
 4 
 5 public class User extends BaseEntity {
 6     private String uname;
 7     private String upwd;
 8     public String getUname() {
 9         return uname;
10     }
11     public void setUname(String uname) {
12         this.uname = uname;
13     }
14     public String getUpwd() {
15         return upwd;
16     }
17     public void setUpwd(String upwd) {
18         this.upwd = upwd;
19     }
20     public User(String uname, String upwd) {
21         this.uname = uname;
22         this.upwd = upwd;
23     }
24     public User() {
25         
26     }
27     @Override
28     public String toString() {
29         return "User [uname=" + uname + ", upwd=" + upwd + "]";
30     }
31     
32 }

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.spring.ssh.user.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>

Dao方法

UserDao 
 1 package com.spring.ssh.user.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.spring.ssh.base.dao.BaseDao;
12 import com.spring.ssh.base.util.StringUtils;
13 import com.spring.ssh.user.entity.TreeNode;
14 import com.spring.ssh.user.entity.User;
15 
16 public class UserDao extends BaseDao {
17     
18     public List<User> list(User user){
19         
20         return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
21 
22             @Override
23             public List<User> doInHibernate(Session arg0) throws HibernateException {
24                 Query query = arg0.createQuery("from User");
25                 String uname = user.getUname();
26                 String upwd = user.getUpwd();
27                 if(StringUtils.isNotBlank(uname)&& StringUtils.isNotBlank(upwd)) {
28                          query = arg0.createQuery("from User where uname = :uname and upwd = :upwd ");
29                          query.setParameter("uname", uname);
30                          query.setParameter("upwd", upwd);
31                 }
32                 return query.list();
33             }
34         });
35     }
36     
37     public int add(User user) {
38         Serializable a = this.getHibernateTemplate().save(user);
39         int n = 0;
40         if(StringUtils.isNotBlank(a+"")) {
41             n=1;
42         }
43         return  n;
44     }
45     
46     public List<TreeNode> listNode(){
47         
48         return this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {
49             @Override
50             public List<TreeNode> doInHibernate(Session session) throws HibernateException {
51                 return session.createQuery("from TreeNode").list();
52             }
53         });
54     }
55 }

biz

UserBiz

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

web

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

配置User

spring-user.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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

     <bean id="userDao" class="com.spring.ssh.user.dao.UserDao" parent="baseDao" ></bean>
     <bean id="userBiz" class="com.spring.ssh.user.biz.impl.UserBizImpl" parent="baseBiz" >
        <property name="userDao" ref="userDao"></property>
     </bean>
     <bean id="userAction" class="com.spring.ssh.user.web.UserAction" parent="baseAction">
        <property name="userBiz" ref="userBiz"></property>
     </bean>
</beans>

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" namespace="/user" >
7         <action name="user_*" class="userAction" method="{1}"></action>
8     </package>
9 </struts>

Articles模块层

Articles 
 1 package com.spring.ssh.articles.entity;
 2 
 3 import com.spring.ssh.base.entity.BaseEntity;
 4 
 5 public class Articles extends BaseEntity {
 6 
 7     private int id;
 8     private String title;
 9     private String body;
10     public int getId() {
11         return id;
12     }
13     public void setId(int id) {
14         this.id = id;
15     }
16     public String getTitle() {
17         return title;
18     }
19     public void setTitle(String title) {
20         this.title = title;
21     }
22     public String getBody() {
23         return body;
24     }
25     public void setBody(String body) {
26         this.body = body;
27     }
28     public Articles(int id, String title, String body) {
29         super();
30         this.id = id;
31         this.title = title;
32         this.body = body;
33     }
34     public Articles() {
35         super();
36     }
37     @Override
38     public String toString() {
39         return "Articles [id=" + id + ", title=" + title + ", body=" + body + "]";
40     }
41 }
Dao方法
ArticlesDao 
 1 package com.spring.ssh.articles.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.spring.ssh.articles.entity.Articles;
11 import com.spring.ssh.base.dao.BaseDao;
12 import com.spring.ssh.base.util.StringUtils;
13 
14 public class ArticlesDao extends BaseDao {
15 
16     public List<Articles> list() {
17 
18         return this.getHibernateTemplate().execute(new HibernateCallback<List<Articles>>() {
19 
20             @Override
21             public List<Articles> doInHibernate(Session session) throws HibernateException {
22 
23                 return session.createQuery("from Articles").list();
24             }
25         });
26     }
27 
28     public int add(Articles articles) {
29 
30         Serializable a = this.getHibernateTemplate().save(articles);
31         int n = 0;
32         System.out.println("add增加过后的值:" + a);
33         if (StringUtils.isNotBlank(a + "")) {
34             n = 1;
35         }
36         return n;
37     }
38 
39     public int edit(Articles articles) {
40 
41         this.getHibernateTemplate().update(articles);
42 
43         return 1;
44     }
45 
46     public int delete(Articles articles) {
47 
48         this.getHibernateTemplate().delete(articles);
49 
50         return 1;
51     }
52 
53 }
ArticlesBiz 
 1 package com.spring.ssh.articles.biz;
 2 
 3 import java.util.List;
 4 
 5 import com.spring.ssh.articles.entity.Articles;
 6 
 7 public interface ArticlesBiz {
 8     public List<Articles> list();
 9     public int add(Articles articles);
10     public int edit(Articles articles);
11     public int delete(Articles articles);
12 
13 }
ArticlesBizImpl 
 1 package com.spring.ssh.articles.biz.impl;
 2 
 3 import java.util.List;
 4 
 5 import com.spring.ssh.articles.biz.ArticlesBiz;
 6 import com.spring.ssh.articles.dao.ArticlesDao;
 7 import com.spring.ssh.articles.entity.Articles;
 8 
 9 public class ArticlesBizImpl implements ArticlesBiz {
10     
11     private ArticlesDao articlesDao ;
12     
13     public ArticlesDao getArticlesDao() {
14         return articlesDao;
15     }
16 
17     public void setArticlesDao(ArticlesDao articlesDao) {
18         this.articlesDao = articlesDao;
19     }
20 
21     @Override
22     public List<Articles> list() {
23         return articlesDao.list();
24     }
25 
26     @Override
27     public int add(Articles articles) {
28         return articlesDao.add(articles);
29     }
30 
31     @Override
32     public int edit(Articles articles) {
33         return articlesDao.edit(articles);
34     }
35 
36     @Override
37     public int delete(Articles articles) {
38         return articlesDao.delete(articles);
39     }
40 }

web

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

配置articles

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-context-4.3.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
11 
12     <bean id="articlesDao" class="com.spring.ssh.articles.dao.ArticlesDao" parent="baseDao" ></bean>
13     <bean id="articlesBiz" class="com.spring.ssh.articles.biz.impl.ArticlesBizImpl" parent="baseBiz" >
14         <property name="articlesDao" ref="articlesDao"></property>
15     </bean>
16         
17     <bean id="articlesAction" class="com.spring.ssh.articles.web.ArticlesAction" parent="baseAction" scope="proptype">
18         <property name="articlesBiz" ref="articlesBiz"></property>
19     </bean>
20         
21 </beans>

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         </action>
 9     </package>
10 </struts>

Spring---SSH整合(二)

标签:lan   extends   url   apache   lang   throws   schema   sof   sys   

原文地址:https://www.cnblogs.com/xcn123/p/11373814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!