SSH整合二
结构图
articles模块
实体类Articles.java
package com.jt.articles.entity; public class Articles { private Integer id; private String title; private String body; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } @Override public String toString() { return "Articles [id=" + id + ", title=" + title + ", body=" + body + "]"; } }
实体类映射文件articles.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="t_vue_articles" name="com.jt.articles.entity.Articles"> <id name="id" type="java.lang.Integer" column="id"> <generator class="increment"></generator> </id> <property name="title" type="java.lang.String" column="title"></property> <property name="body" type="java.lang.String" column="body"></property> </class> </hibernate-mapping>
ArticlesBiz.java
package com.jt.articles.biz; import java.util.List; import com.jt.articles.entity.Articles; import com.jt.base.util.PageBean; public interface ArticlesBiz { /** * 新增 * @param articles * @return */ public int add(Articles articles); /** * 修改 * @param articles */ public void update(Articles articles); /** * 删除 * @param articles */ public void del(Articles articles); /** * 查询 * @param articles * @param pageBean * @return */ public List<Articles> list(Articles articles,PageBean pageBean); }
ArticlesBizImpl.java
package com.jt.articles.biz.impl; import java.util.List; import com.jt.articles.biz.ArticlesBiz; import com.jt.articles.dao.ArticlesDao; import com.jt.articles.entity.Articles; import com.jt.base.util.PageBean; public class ArticlesBizImpl implements ArticlesBiz{ private ArticlesDao articlesDao; public ArticlesDao getArticlesDao() { return articlesDao; } public void setArticlesDao(ArticlesDao articlesDao) { this.articlesDao = articlesDao; } @Override public int add(Articles articles) { return articlesDao.add(articles); } @Override public void update(Articles articles) { articlesDao.update(articles); } @Override public void del(Articles articles) { articlesDao.del(articles); } @Override public List<Articles> list(Articles articles, PageBean pageBean) { // TODO Auto-generated method stub return articlesDao.list(articles, pageBean); } }
ArticlesDao.java
package com.jt.articles.dao; import java.util.List; import org.hibernate.Session; import com.jt.articles.entity.Articles; import com.jt.base.dao.BaseDao; import com.jt.base.util.PageBean; public class ArticlesDao extends BaseDao{ private static final long serialVersionUID = -6077790395680289782L; /** * 新增 * @param articles * @return */ public int add(Articles articles) { return (int) this.getHibernateTemplate().save(articles); } /** * * 删除 * @param articles */ public void del(Articles articles) { this.getHibernateTemplate().delete(articles); } /** * 修改 * @param articles */ public void update(Articles articles) { this.getHibernateTemplate().update(articles); } /** * 查询 * @param articles * @param pageBean * @return */ public List<Articles> list(Articles articles, PageBean pageBean){ Session session = this.getHibernateTemplate().getSessionFactory().openSession(); session.beginTransaction(); return super.executeQuery(session, "from Articles", null,pageBean); } }
ArticlesAction.java
package com.jt.articles.web; import com.jt.articles.biz.ArticlesBiz; import com.jt.articles.entity.Articles; import com.jt.base.web.BaseAction; import com.opensymphony.xwork2.ModelDriven; public class ArticlesAction extends BaseAction implements ModelDriven<Articles>{ private static final long serialVersionUID = 5943402081349505372L; private ArticlesBiz articlesBiz; private Articles articles =new Articles(); public ArticlesBiz getArticlesBiz() { return articlesBiz; } public void setArticlesBiz(ArticlesBiz articlesBiz) { this.articlesBiz = articlesBiz; } /** * 查询 * @return */ public String list() { System.out.println(articlesBiz.list(articles, null)); return null; } /** * 删除 */ public void del() { articlesBiz.del(articles); } /** * 修改 * @return */ public String update() { articlesBiz.update(articles); return null; } /** * 新增 * @return */ public String add() { articlesBiz.add(articles); return null; } @Override public Articles getModel() { // TODO Auto-generated method stub return articles; } }
User模块
User.java
package com.jt.user.entity; public class User { private String uname; private String pwd; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [uname=" + uname + ", pwd=" + pwd + "]"; } }
实体类映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="t_vue_user" name="com.jt.user.entity.User"> <id name="uname" type="java.lang.String" column="uname"> <generator class="assigned"></generator> </id> <property name="pwd" type="java.lang.String" column="pwd"></property> </class> </hibernate-mapping>
UserBiz.java
package com.jt.user.biz; import com.jt.user.entity.User; public interface UserBiz { /** * 登录 * @param user * @return */ public User login(User user); /** * 注册 * @param user * @return */ public int addUser(User user); }
UserBizImpl.java
package com.jt.user.biz.impl; import com.jt.user.biz.UserBiz; import com.jt.user.dao.UserDao; import com.jt.user.entity.User; public class UserBizImpl implements UserBiz{ private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public User login(User user) { return userDao.login(user); } @Override public int addUser(User user) { return userDao.addUser(user); } }
UserDao.java
package com.jt.user.dao; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate5.HibernateCallback; import com.jt.base.dao.BaseDao; import com.jt.user.entity.User; public class UserDao extends BaseDao{ private static final long serialVersionUID = 3670806315799801488L; public User login(User user) { return this.getHibernateTemplate().execute(new HibernateCallback<User>() { @Override public User doInHibernate(Session session) throws HibernateException { return (User) session.createQuery("from User where uname= '"+user.getUname()+"' and pwd= "+user.getPwd()).list(); } }); } public int addUser(User user) { return (int) this.getHibernateTemplate().save(user); } }
UserAction.java
package com.jt.user.web; import com.jt.base.web.BaseAction; import com.jt.user.biz.UserBiz; import com.jt.user.entity.User; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends BaseAction implements ModelDriven<User>{ private static final long serialVersionUID = 4244808665377509950L; private UserBiz userBiz; private User user=new User(); public UserBiz getUserBiz() { return userBiz; } public void setUserBiz(UserBiz userBiz) { this.userBiz = userBiz; } /** * 登录 * @return */ public String login() { userBiz.login(user); return null; } /** * 注册 * @return */ public String addUser() { userBiz.addUser(user); return null; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
tree_node模块
TreeNode.java
package com.jt.tree_node.entity; import java.util.HashSet; import java.util.Set; public class TreeNode { private Integer nodeId; private String nodeName; private Integer treeNodeType; private Integer position; private String url; // 关联父节点 private TreeNode parent; // 关联子节点"们" private Set<TreeNode> children = new HashSet<TreeNode>(); // 加载方式(1强制加载关联,0懒加载) private Integer initChildren = 0; public Integer getNodeId() { return nodeId; } public void setNodeId(Integer nodeId) { this.nodeId = nodeId; } public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } public Integer getTreeNodeType() { return treeNodeType; } public void setTreeNodeType(Integer treeNodeType) { this.treeNodeType = treeNodeType; } public Integer getPosition() { return position; } public void setPosition(Integer position) { this.position = position; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public TreeNode getParent() { return parent; } public void setParent(TreeNode parent) { this.parent = parent; } public Set<TreeNode> getChildren() { return children; } public void setChildren(Set<TreeNode> children) { this.children = children; } public Integer getInitChildren() { return initChildren; } public void setInitChildren(Integer initChildren) { this.initChildren = initChildren; } }
treeNode.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="t_vue_tree_node" name="com.jt.tree_node.entity.TreeNode"> <id name="nodeId" type="java.lang.Integer" column="tree_node_id"> <generator class="increment" /> </id> <property name="nodeName" type="java.lang.String" column="tree_node_name"> </property> <property name="treeNodeType" type="java.lang.Integer" column="tree_node_type"> </property> <property name="position" type="java.lang.Integer" column="position"> </property> <property name="url" type="java.lang.String" column="url"> </property> <!-- parent定义了管理树形 column中指定了通过那个列段来关联 对应的是当前表的主键 --> <many-to-one name="parent" class="com.jt.tree_node.entity.TreeNode" column="parent_node_id"/> <!-- 这是定义子节点关联 --> <set name="children" cascade="save-update" inverse="true"> <!--凡是parent_node_id是当前节点的tree_node_id的就统统加入集合--> <key column="parent_node_id"></key> <one-to-many class="com.jt.tree_node.entity.TreeNode"/> </set> </class> </hibernate-mapping>
TreeNodeBiz.java
package com.jt.tree_node.biz; import java.util.List; import com.jt.tree_node.entity.TreeNode; public interface TreeNodeBiz { /** * 加载树 * @return */ public List<TreeNode> list(); }
TreeNodBizImpl.java
package com.jt.tree_node.biz.impl; import java.util.List; import com.jt.tree_node.biz.TreeNodeBiz; import com.jt.tree_node.dao.TreeNodeDao; import com.jt.tree_node.entity.TreeNode; public class TreeNodBizImpl implements TreeNodeBiz{ private TreeNodeDao treeNodeDao; public TreeNodeDao getTreeNodeDao() { return treeNodeDao; } public void setTreeNodeDao(TreeNodeDao treeNodeDao) { this.treeNodeDao = treeNodeDao; } @Override public List<TreeNode> list() { // TODO Auto-generated method stub return treeNodeDao.list(); } }
TreeNodeDao.java
package com.jt.tree_node.dao; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; import com.jt.base.dao.BaseDao; import com.jt.tree_node.entity.TreeNode; public class TreeNodeDao extends BaseDao{ private static final long serialVersionUID = -4339387396332761044L; public List<TreeNode> list(){ Session session = this.getHibernateTemplate().getSessionFactory().openSession(); session.beginTransaction(); //这是最好的做做法 非常好 使用 hql语句进行查询 List<TreeNode> treeNodes = super.executeQuery(session, "from TreeNode where treeNodeType = 1", null,null); //进行强子加载子节点 if (treeNodes != null && treeNodes.size() > 0) { for (TreeNode t : treeNodes) { //强制加载子节点 Hibernate.initialize(t.getChildren()); } } return treeNodes; } }
TreeNodeAction.java
package com.jt.tree_node.web; import java.util.List; import com.jt.base.web.BaseAction; import com.jt.tree_node.biz.TreeNodeBiz; import com.jt.tree_node.entity.TreeNode; public class TreeNodeAction extends BaseAction{ private static final long serialVersionUID = -6038611615430719428L; private TreeNodeBiz treeNodeBiz; public TreeNodeBiz getTreeNodeBiz() { return treeNodeBiz; } public void setTreeNodeBiz(TreeNodeBiz treeNodeBiz) { this.treeNodeBiz = treeNodeBiz; } public String list() { List<TreeNode> list = treeNodeBiz.list(); System.out.println("我是长度" + list.size()); TreeNode node = list.get(0); System.out.println(node); System.out.println(node.getChildren()); for (TreeNode n: node.getChildren()) { System.out.println("我是子节点:" + n.getNodeName()); } return null; } }