今天,我们在上界SSH整合(一)的基础上,实现crud操作,现在来看看代码吧。
由于SSH整合(一)中,我们已经完成了部分配置,代码实现,我们在今天的代码中就主要体现新配置,以及类。
这是我们的项目基本结构图:
Articles
ArticlesBiz
package com.ly.articles.biz;
import java.util.List;
import com.ly.articles.entity.Articles;
public interface ArticlesBiz {
public List<Articles> list();
public int add(Articles articles);
public int edit(Articles articles);
public int delete(Articles articles);
}
ArticlesBizImpl
package com.ly.articles.biz.impl;
import java.util.List;
import com.ly.articles.biz.ArticlesBiz;
import com.ly.articles.dao.ArticlesDao;
import com.ly.articles.entity.Articles;
public class ArticlesBizImpl implements ArticlesBiz {
private ArticlesDao articlesDao ;
public ArticlesDao getArticlesDao() {
return articlesDao;
}
public void setArticlesDao(ArticlesDao articlesDao) {
this.articlesDao = articlesDao;
}
@Override
public List<Articles> list() {
// TODO Auto-generated method stub
return articlesDao.list();
}
@Override
public int add(Articles articles) {
// TODO Auto-generated method stub
return articlesDao.add(articles);
}
@Override
public int edit(Articles articles) {
// TODO Auto-generated method stub
return articlesDao.edit(articles);
}
@Override
public int delete(Articles articles) {
// TODO Auto-generated method stub
return articlesDao.delete(articles);
}
}
ArticlesDao
package com.ly.articles.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.ly.articles.entity.Articles;
import com.ly.base.dao.BaseDao;
import com.ly.base.util.StringUtils;
public class ArticlesDao extends BaseDao {
private static final long serialVersionUID = 8651276097678426521L;
public List<Articles> list(){
return this.getHibernateTemplate().execute(new HibernateCallback<List<Articles>>() {
@Override
public List<Articles> doInHibernate(Session session) throws HibernateException {
return session.createQuery("from Articles").list();
}
});
}
public int add(Articles articles) {
Serializable a = this.getHibernateTemplate().save(articles);
int n = 0;
System.out.println("add增加过后的值:~~"+a);
if(StringUtils.isNotBlank(a+"")) {
n = 1;
}
return n;
}
public int edit(Articles articles) {
this.getHibernateTemplate().update(articles);
return 1;
}
public int delete(Articles articles) {
this.getHibernateTemplate().delete(articles);
return 1;
}
}
Articles
package com.ly.articles.entity;
import com.ly.base.entity.BaseEntity;
public class Articles extends BaseEntity {
private static final long serialVersionUID = -6188029223617912462L;
private int id;
private String title;
private String body;
public int getId() {
return id;
}
public void setId(int 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 Articles(int id, String title, String body) {
super();
this.id = id;
this.title = title;
this.body = body;
}
public Articles() {
super();
}
@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.ly.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>
web
ArticlesAction:
package com.ly.articles.web;
import java.util.List;
import com.ly.articles.biz.ArticlesBiz;
import com.ly.articles.entity.Articles;
import com.ly.base.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
public class ArticlesAction extends BaseAction implements ModelDriven<Articles> {
private static final long serialVersionUID = 5944659149847110488L;
private Articles articles = new Articles();
private ArticlesBiz articlesBiz ;
public ArticlesBiz getArticlesBiz() {
return articlesBiz;
}
public void setArticlesBiz(ArticlesBiz articlesBiz) {
this.articlesBiz = articlesBiz;
}
public String list() {
List<Articles> list = articlesBiz.list();
for (Articles a : list) {
System.out.println(a);
}
return null;
}
public String add() {
articlesBiz.add(articles);
return null;
}
public String edit() {
articlesBiz.edit(articles);
return null;
}
public String del() {
articlesBiz.delete(articles);
return null;
}
@Override
public Articles getModel() {
// TODO Auto-generated method stub
return articles;
}
}
user
UserBiz
package com.ly.user.biz;
import java.util.List;
import com.ly.user.entity.TreeNode;
import com.ly.user.entity.User;
public interface UserBiz {
public List<User> list(User user);
public int add(User user);
public List<TreeNode> listNode();
}
UserBizImpl
package com.ly.user.biz.impl;
import java.util.List;
import com.ly.user.entity.TreeNode;
import com.ly.user.biz.UserBiz;
import com.ly.user.dao.UserDao;
import com.ly.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 List<User> list(User user) {
return userDao.list(user);
}
@Override
public int add(User user) {
// TODO Auto-generated method stub
return userDao.add(user);
}
@Override
public List<TreeNode> listNode() {
// TODO Auto-generated method stub
return userDao.listNode();
}
}
UserDao
package com.ly.user.dao;
import java.io.Serializable;
import java.util.List;
import com.ly.user.entity.TreeNode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.ly.base.dao.BaseDao;
import com.ly.base.util.StringUtils;
import com.ly.user.entity.User;
public class UserDao extends BaseDao {
private static final long serialVersionUID = -1213415622340269960L;
public List<User> list(User user){
return this.getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
@Override
public List<User> doInHibernate(Session arg0) throws HibernateException {
Query query = arg0.createQuery("from User");
String uname = user.getUname();
String upwd = user.getUpwd();
if(StringUtils.isNotBlank(uname)&& StringUtils.isNotBlank(upwd)) {
query = arg0.createQuery("from User where uname = :uname and upwd = :upwd ");
query.setParameter("uname", uname);
query.setParameter("upwd", upwd);
}
return query.list();
}
});
}
public int add(User user) {
Serializable a = this.getHibernateTemplate().save(user);
int n = 0;
if(StringUtils.isNotBlank(a+"")) {
n=1;
}
return n;
}
public List<TreeNode> listNode(){
return this.getHibernateTemplate().execute(new HibernateCallback<List<TreeNode>>() {
@Override
public List<TreeNode> doInHibernate(Session session) throws HibernateException {
return session.createQuery("from TreeNode").list();
}
});
}
}
entity
User
package com.ly.user.entity;
import com.ly.base.entity.BaseEntity;
public class User extends BaseEntity {
private static final long serialVersionUID = 6566515100091330894L;
private String uname;
private String upwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public User(String uname, String upwd) {
super();
this.uname = uname;
this.upwd = upwd;
}
public User() {
super();
}
@Override
public String toString() {
return "User [uname=" + uname + ", upwd=" + upwd + "]";
}
}
TreeNode
package com.ly.user.entity;
import com.ly.base.entity.BaseEntity;
public class TreeNode extends BaseEntity {
private static final long serialVersionUID = 3404051699954127467L;
private int treenodeid;
private String treenodename;
private int treenodetype;
private int parentnodeid;
private String url;
private int position;
private String icon;
public int getTreenodeid() {
return treenodeid;
}
public void setTreenodeid(int treenodeid) {
this.treenodeid = treenodeid;
}
public String getTreenodename() {
return treenodename;
}
public void setTreenodename(String treenodename) {
this.treenodename = treenodename;
}
public int getTreenodetype() {
return treenodetype;
}
public void setTreenodetype(int treenodetype) {
this.treenodetype = treenodetype;
}
public int getParentnodeid() {
return parentnodeid;
}
public void setParentnodeid(int parentnodeid) {
this.parentnodeid = parentnodeid;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public TreeNode(int treenodeid, String treenodename, int treenodetype, int parentnodeid, String url, int position,
String icon) {
super();
this.treenodeid = treenodeid;
this.treenodename = treenodename;
this.treenodetype = treenodetype;
this.parentnodeid = parentnodeid;
this.url = url;
this.position = position;
this.icon = icon;
}
public TreeNode() {
super();
}
@Override
public String toString() {
return "TreeNode [treenodeid=" + treenodeid + ", treenodename=" + treenodename + ", treenodetype="
+ treenodetype + ", parentnodeid=" + parentnodeid + ", url=" + url + ", position=" + position
+ ", icon=" + icon + "]";
}
}
配置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.ly.user.entity.User">
<id name="uname" type="java.lang.String" column="uname"></id>
<property name="upwd" type="java.lang.String" column="pwd"></property>
</class>
</hibernate-mapping>
配置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.ly.user.entity.TreeNode">
<id name="treenodeid" type="java.lang.Integer" column="tree_node_id">
<generator class="increment"></generator>
</id>
<property name="treenodename" type="java.lang.String" column="tree_node_name"></property>
<property name="treenodetype" type="java.lang.Integer" column="tree_node_type"></property>
<property name="parentnodeid" type="java.lang.Integer" column="parent_node_id"></property>
<property name="url" type="java.lang.String" column="url"></property>
<property name="position" type="java.lang.Integer" column="position"></property>
<property name="icon" type="java.lang.String" column="icon"></property>
</class>
</hibernate-mapping>
web
UserAction
package com.ly.user.web;
import java.util.List;
import com.ly.user.entity.TreeNode;
import com.ly.user.entity.User;
import com.ly.base.web.BaseAction;
import com.ly.user.biz.UserBiz;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends BaseAction implements ModelDriven<User> {
private static final long serialVersionUID = -1655051258255282376L;
private User user = new User();
private UserBiz userBiz ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String dologin() {
List<User> list = userBiz.list(user);
if(list.size()>0) {
System.out.println("登录成功");
}
else {
System.out.println("登录失败,用户名或密码错误");
}
return null;
}
public String addUser() {
int n = userBiz.add(user);
if(n>0) {
System.out.println("注册成功");
}
else {
System.out.println("注册失败");
}
return null;
}
public String list() {
List<User> list = userBiz.list(user);
for (User user : list) {
System.out.println(user);
}
return null;
}
public String listNode() {
List<TreeNode> listNode = userBiz.listNode();
for (TreeNode treeNode : listNode) {
System.out.println(treeNode);
}
return null;
}
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
配置文件:
spring-articles.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="articlesDao" class="com.ly.articles.dao.ArticlesDao" parent="baseDao" ></bean>
<bean id="articlesBiz" class="com.ly.articles.biz.impl.ArticlesBizImpl" parent="baseBiz" >
<property name="articlesDao" ref="articlesDao"></property>
</bean>
<bean id="articlesAction" class="com.ly.articles.web.ArticlesAction" parent="baseAction">
<property name="articlesBiz" ref="articlesBiz"></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.ly.user.dao.UserDao" parent="baseDao" ></bean>
<bean id="userBiz" class="com.ly.user.biz.impl.UserBizImpl" parent="baseBiz" >
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.ly.user.web.UserAction" parent="baseAction">
<property name="userBiz" ref="userBiz"></property>
</bean>
</beans>
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">
<action name="/user_*" class="userAction" method="{1}">
</action>
</package>
</struts>
struts-articles.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="articles" extends="base" namespace="/articles">
<action name="/articles_*" class="articlesAction" method="{1}">
</action>
</package>
</struts>
spring-context.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">
<import resource="spring-hibernate.xml"/>
<import resource="spring-book.xml"/>
<import resource="spring-user.xml"/>
<import resource="spring-articles.xml"/>
</beans>
struts.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>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<include file="struts-user.xml"></include>
<include file="struts-articles.xml"></include>
</struts>
实体映射文件
<property name="mappingResources">
<list>
<value>com/ly/book/entity/Book.hbm.xml</value>
<value>com/ly/user/entity/User.hbm.xml</value>
<value>com/ly/user/entity/TreeNode.hbm.xml</value>
<value>com/ly/articles/entity/Articles.hbm.xml</value>
</list>
</property>
测试: