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 = ; 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() > ) {
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();
System.out.println(node);
System.out.println(node.getChildren());
for (TreeNode n: node.getChildren()) {
System.out.println("我是子节点:" + n.getNodeName());
}
return null;
}
}
SSH整合二的更多相关文章
- SSH整合(二)
SSH框架实现登录.新闻增删改查.树形菜单 项目结构 pom.xml 网不好不要一次引入太多,容易下不全 <project xmlns="http://maven.apache.org ...
- 三大框架SSH整合
三大框架SSH整合 -------------------------------Spring整合Hibernate------------------------------- 一.为什么要整合Hi ...
- 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】
一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...
- Spring(八)SSH整合简述
一.Spring与Struts2的整合 1.1.整合步骤 1.2.配置web.xml 1.3.配置spring配置文件applicationContext.xml 1.4.配置struts配置文件 1 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
- 【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一 介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...
- SSH整合总结(xml与注解)
本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入ja ...
- Spring第四天——SSH整合
(从整合开始,使用回归使用eclipse) 一.三大框架版本: struts2 hibernate5 spring4 二.SSH三大框架回顾: Hibernate: ORM思想 核心配置文件: 单独 ...
- Java - 框架之 SSH 整合
代码获取 十四. ssh 整合1 - 包 1. Struts jar 包 - Struts-2.xx\apps\stutrs2-blank\WEB-INF ...
随机推荐
- 05-深入python的set和dict
一.深入python的set和dict 1.1.dict的abc继承关系 from collections.abc import Mapping,MutableMapping #dict属于mappi ...
- .Net WebApi 使用Session
直接使用Session 会报错“未将对象引用设置到对象的实例”. 解决办法:在Global中添加如下代码 /// <summary> /// 打开session /// </summ ...
- PostgreSQL中的onflict
PostgreSQL 9.5 引入了一项新功能,UPSERT(insert on conflict do),当插入遇到约束错误时,直接返回,或者改为执行UPDATE. 1.不存在则插入,存在则更新 i ...
- 表单生成器(Form Builder)之mongodb表单数据——整理数据
在上篇笔记中,为车辆信息表.车辆耗损表以及车辆营收表插入了一些数据.之后便是查询了,重点也在查询……按照之前定好的数据结构,如果查询mongodb document的最外层比较简单,但是我们的重点应该 ...
- [Go] 使用net包作为tcp客户端读取http
1.tcp的客户端,并且直接读取http协议的全部内容,每次读取4096字节,直到最后一个字节是\n并且读取的长度小于4096 conn, err := net.Dial("tcp" ...
- 浮点运算与boost.multiprecision
在C++中,float占4个字节,double占8个字节,均采用 IEEE 754 浮点标准:内部都是以二进制为基础,表述实数,有些实数可以被精确表述,比如0.2,但有些不行,比如0.3.针对这一点, ...
- [C]链接和生存周期
链接和生存周期的区别: 链接是标识符的属性: 生存周期是对象的属性: 链接可以是外部(external),内部(internal)或没有(none): 生存周期可以是自动的.静态的,或已分配的(all ...
- 华为hcnp r&s考试一共有三门,R&S-IERS,R&S-IENP,R&S-IEEP
大纲1.HCNP-R&S-IEEP V2.0考试大纲 考试内容:HCNP-R&S-IEEP考试覆盖对企业网络的规划.设计.实施.维护.排障.优化以及网络割接等. 知识点: 网络规划:1 ...
- 01-Nginx安装
一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.首先安装PCRE PC ...
- 在Windows下配置多个git账号
1.生成并部署SSH key 安装好Git客户端后,打开git bash,输入以下命令生成user1的SSH Key: ssh-keygen -t rsa -C "user1@email.c ...