SSH简易后台
这里我们用SSH完成一个登录,注册,树形菜单,文章的crud的后台
配置文件
基础的配置文件这里就不展示了,详情请看SSH整合
spring-article.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"
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">
<bean id="articleDao" class="com.shl.article.dao.ArticleDao" parent="baseDao"></bean>
<bean id="articleBiz" class="com.shl.article.biz.ArticleBizImpl" parent="baseBiz">
<property name="articleDao" ref="articleDao"></property>
</bean>
<bean id="articleAction" class="com.shl.article.web.ArticleAction" parent="baseAction">
<property name="articleBiz" ref="articleBiz"></property>
</bean>
</beans>
spring-treenode.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"
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">
<bean id="nodeDao" class="com.shl.node.dao.NodeDao" parent="baseDao"></bean>
<bean id="nodeBiz" class="com.shl.node.biz.NodeBizImpl" parent="baseBiz">
<property name="nodeDao" ref="nodeDao"></property>
</bean>
<bean id="nodeAction" class="com.shl.node.web.NodeAction" parent="baseAction">
<property name="nodeBiz" ref="nodeBiz"></property>
</bean>
</beans>
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"
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">
<bean id="userDao" class="com.shl.user.dao.UserDao" parent="baseDao"></bean>
<bean id="userBiz" class="com.shl.user.biz.UserBizImpl" parent="baseBiz">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.shl.user.web.UserAction" parent="baseAction">
<property name="userBiz" ref="userBiz"></property>
</bean>
</beans>
struts-article.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="ar" extends="base" namespace="/ar">
<!-- 这里的class不再是全路径名了,而是spring所管理的Bean的id -->
<action name="/article_*" class="articleAction" method="{1}">
</action>
</package>
</struts>
struts-treenode.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="node" extends="base" namespace="/node">
<!-- 这里的class不再是全路径名了,而是spring所管理的Bean的id -->
<action name="/node_*" class="nodeAction" method="{1}">
</action>
</package>
</struts>
struts-user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="user" extends="base" namespace="/user">
<!-- 这里的class不再是全路径名了,而是spring所管理的Bean的id -->
<action name="/user_*" class="userAction" method="{1}">
</action>
</package>
</struts>
Article的crud
ArticleBiz
package com.shl.article.biz;
import java.util.List;
import com.shl.article.entity.Article;
import com.shl.base.util.PageBean;
public interface ArticleBiz {
public int add(Article ar);
public void del(Article ar);
public void edit(Article ar);
List<Article> list(Article ar, PageBean pageBean);
}
ArticleBizImpl
package com.shl.article.biz;
import java.util.List;
import com.shl.article.dao.ArticleDao;
import com.shl.article.entity.Article;
import com.shl.base.util.PageBean;
public class ArticleBizImpl implements ArticleBiz {
private ArticleDao articleDao;
public ArticleDao getArticleDao() {
return articleDao;
}
public void setArticleDao(ArticleDao articleDao) {
this.articleDao = articleDao;
}
@Override
public int add(Article ar) {
return articleDao.add(ar);
}
@Override
public void del(Article ar) {
articleDao.del(ar);
}
@Override
public void edit(Article ar) {
articleDao.edit(ar);
}
@Override
public List<Article> list(Article ar,PageBean pageBean) {
return articleDao.list(ar, pageBean);
}
}
Article
package com.shl.article.entity;
import com.shl.base.entity.BaseEntity;
public class Article extends BaseEntity {
private static final long serialVersionUID = -6409101675377070678L;
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;
}
public Article() {
super();
}
public Article(Integer id, String title, String body) {
super();
this.id = id;
this.title = title;
this.body = body;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", body=" + body + "]";
}
}
Article.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.shl.article.entity.Article">
<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>
ArticleDao
package com.shl.article.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.shl.article.entity.Article;
import com.shl.base.dao.BaseDao;
import com.shl.base.util.PageBean;
import com.shl.base.util.StringUtils;
public class ArticleDao extends BaseDao {
/**
* 增加
* @param user
* @return
*/
public int add(Article ar) {
return (int) this.getHibernateTemplate().save(ar);
}
/**
* 删除
* @param ar
*/
public void del(Article ar) {
this.getHibernateTemplate().delete(ar);
}
/**
* 修改
* @param ar
*/
public void edit(Article ar) {
Article a = this.getHibernateTemplate().get(Article.class, ar.getId());
a.setTitle(ar.getTitle());
a.setBody(ar.getBody());
}
/**
* 模糊查
* @return
*/
public List<Article> list(Article ar,PageBean pageBean){
return (List<Article>) this.getHibernateTemplate().execute(new HibernateCallback<List<Article>>() {
@Override
public List<Article> doInHibernate(Session session) throws HibernateException {
// TODO Auto-generated method stub
if(StringUtils.isNotBlank(ar.getTitle())) {
return session.createQuery("from Article where title like '%"+ar.getTitle()+"%'").list();
}else {
return session.createQuery("from Article").list();
}
}
});
}
}
ArticleAction
package com.shl.article.web;
import java.util.List;
import com.opensymphony.xwork2.ModelDriven;
import com.shl.article.biz.ArticleBiz;
import com.shl.article.entity.Article;
import com.shl.base.util.PageBean;
import com.shl.base.web.BaseAction;
public class ArticleAction extends BaseAction implements ModelDriven<Article>{
private ArticleBiz articleBiz;
private Article ar=new Article();
private PageBean pageBean;
public ArticleBiz getArticleBiz() {
return articleBiz;
}
public void setArticleBiz(ArticleBiz articleBiz) {
this.articleBiz = articleBiz;
}
/**
* 增加
* @return
*/
public String add() {
int add = articleBiz.add(ar);
return null;
}
public String list() {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List<Article> list = articleBiz.list(ar,pageBean);
for (Article ar : list) {
System.out.println(ar);
}
return null;
}
public String edit() {
articleBiz.edit(ar);
return null;
}
public String del() {
articleBiz.del(ar);
return null;
}
@Override
public Article getModel() {
return ar;
}
}
看看查所有的结果
User登录、注册
UserBiz
package com.shl.user.biz;
import java.util.List;
import com.shl.user.entity.User;
public interface UserBiz {
public String addUser(User user);
public User queryLogin(User user);
public List<User> list();
}
UserBizImpl
package com.shl.user.biz;
import java.util.List;
import com.shl.user.biz.UserBiz;
import com.shl.user.dao.UserDao;
import com.shl.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 String addUser(User user) {
// TODO Auto-generated method stub
return userDao.addUser(user);
}
@Override
public User queryLogin(User user) {
// TODO Auto-generated method stub
return userDao.queryLogin(user);
}
@Override
public List<User> list() {
// TODO Auto-generated method stub
return userDao.list();
}
}
User
package com.shl.user.entity;
import com.shl.base.entity.BaseEntity;
public class User extends BaseEntity {
private static final long serialVersionUID = 7920395488381294293L;
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;
}
public User() {
super();
}
public User(String uname, String pwd) {
super();
this.uname = uname;
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uname=" + uname + ", pwd=" + pwd + "]";
}
}
User.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_user" name="com.shl.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>
UserDao
package com.shl.user.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.shl.base.dao.BaseDao;
import com.shl.user.entity.User;
public class UserDao extends BaseDao {
/**
* 注册
* @param user
* @return
*/
public String addUser(User user) {
return (String) this.getHibernateTemplate().save(user);
}
/**
* 登录
*/
public User queryLogin(User user) {
return this.getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql="from User where uname='"+user.getUname()+"' and pwd='"+user.getPwd()+"'";
List<User> list = session.createQuery(hql).list();
if(list.size()==0) {
return null;
}
return list.get(0);
}
});
}
/**
* 查所有
* @return
*/
public List<User> list(){
return (List<User>) this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
@Override
public List<User> doInHibernate(Session session) throws HibernateException {
return session.createQuery("from User").list();
}
});
}
}
UserAction
package com.shl.user.web;
import com.opensymphony.xwork2.ModelDriven;
import com.shl.base.web.BaseAction;
import com.shl.user.biz.UserBiz;
import com.shl.user.entity.User;
public class UserAction extends BaseAction implements ModelDriven<User>{
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() {
User u = userBiz.queryLogin(user);
System.out.println(u);
if(u ==null) {
}
return null;
}
/**
* 注册
* @return
*/
public String register() {
String n = userBiz.addUser(user);
System.out.println(n);
return null;
}
@Override
public User getModel() {
return user;
}
}
树形菜单
NodeBiz
package com.shl.node.biz;
import java.util.List;
import com.shl.node.entity.TreeNode;
public interface NodeBiz {
public List<TreeNode> list();
}
NodeBizImpl
package com.shl.node.biz;
import java.util.List;
import com.shl.node.dao.NodeDao;
import com.shl.node.entity.TreeNode;
public class NodeBizImpl implements NodeBiz {
private NodeDao nodeDao;
public NodeDao getNodeDao() {
return nodeDao;
}
public void setNodeDao(NodeDao nodeDao) {
this.nodeDao = nodeDao;
}
@Override
public List<TreeNode> list() {
return nodeDao.list();
}
}
TreeNode
package com.shl.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>();
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 name="com.shl.node.entity.TreeNode" table="t_vue_tree_node">
<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>
<many-to-one name="parent" class="com.shl.node.entity.TreeNode" column="parent_node_id"/>
<set name="children" cascade="save-update" inverse="true">
<key column="parent_node_id"></key>
<one-to-many class="com.shl.node.entity.TreeNode"/>
</set>
</class>
</hibernate-mapping>
NodeDao
package com.shl.node.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.core.util.JsonUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.shl.article.entity.Article;
import com.shl.base.dao.BaseDao;
import com.shl.base.util.PageBean;
import com.shl.base.util.StringUtils;
import com.shl.node.entity.TreeNode;
import com.shl.user.entity.User;
public class NodeDao extends BaseDao {
public List<TreeNode> list(){
return (List<TreeNode>) this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {
@Override
public List<TreeNode> doInHibernate(Session session) throws HibernateException {
// TODO Auto-generated method stub
return session.createQuery("from TreeNode").list();
}
});
}
}
NodeAction
package com.shl.node.web;
import java.util.List;
import com.opensymphony.xwork2.ModelDriven;
import com.shl.base.web.BaseAction;
import com.shl.node.biz.NodeBiz;
import com.shl.node.entity.TreeNode;
public class NodeAction extends BaseAction implements ModelDriven<TreeNode>{
private NodeBiz nodeBiz;
private TreeNode treeNode=new TreeNode();
public NodeBiz getNodeBiz() {
return nodeBiz;
}
public void setNodeBiz(NodeBiz nodeBiz) {
this.nodeBiz = nodeBiz;
}
public String list() {
List<TreeNode> list = nodeBiz.list();
for (TreeNode t : list) {
System.out.println(t);
}
return null;
}
@Override
public TreeNode getModel() {
return treeNode;
}
}
树新菜单控制台展示