SSH整合2

关于SSH配置见:https://www.cnblogs.com/chenjiahao9527/p/11253964.html

使用SSH编写后台:

 登录和注册:

实体类:User

package com.cjh.crud.entity;

public class User {
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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 [id=" + id + ", 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 name="com.cjh.vue.entity.User" table="t_vue_user">
          <id name="id" type="java.lang.Integer" column="id">
            <generator class="increment"></generator>
        </id>
        <property name="uname" type="java.lang.String"
                  column="uname">
        </property>     
        <property name="pwd" type="java.lang.String"
                  column="pwd">
        </property>
        </class>
</hibernate-mapping>

  dao层:

package com.cjh.crud.dao;



import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.cjh.base.dao.BaseDao;
import com.cjh.crud.entity.User;

public class UserDao extends BaseDao{

	/**
	 * 用户登录
	 * @param user
	 * @return
	 */
	 public User login(User user) {
		 return this.getHibernateTemplate().execute(new HibernateCallback<User>() {

			@Override
			public User doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				return (User) session.createQuery(" from User where uname='"+user.getUname()+"' and pwd="+user.getPwd()).list().get(0);
			}
			 
			 
		});
		 
	 }
	 /**
	  * 用户注册
	  * @param user
	  * @return
	  */
	 public int register(User user) {
		 return (int) this.getHibernateTemplate().save(user);
	 }
	 
	
}

  Biz:

package com.cjh.crud.biz;

import com.cjh.crud.entity.User;

public interface UserBiz {
	/**
	 * 用户登录
	 * 
	 * @param user
	 * @return
	 */
	public User login(User user);

	/**
	 * 用户注册
	 * 
	 * @param user
	 * @return
	 */
	public int register(User user);


}

  Impl:

package com.cjh.crud.biz.impl;

import java.util.List;

import com.cjh.crud.biz.TreeNodeBiz;
import com.cjh.crud.dao.TreeNodeDao;
import com.cjh.crud.entity.TreeNode;

public class TreeNodeBizImpl implements TreeNodeBiz{
	
	private TreeNodeDao treeNodeDao;
	

	public TreeNodeDao getTreeNodeDao() {
		return treeNodeDao;
	}


	public void setTreeNodeDao(TreeNodeDao treeNodeDao) {
		this.treeNodeDao = treeNodeDao;
	}


	@Override
	public List<TreeNode> query() {
		// TODO Auto-generated method stub
		return treeNodeDao.query();
	}

}

  然后在spring-hibernate.xml的实体映射里面配置:

				<value>com/cjh/vue/entity/user.hbm.xml</value>

  配置spring.crud.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--UserDao-->
    <bean id="userDao" class="com.cjh.vue.dao.UserDao" parent="baseDao"></bean>

    <bean id="userBiz" class="com.cjh.vue.biz.impl.UserBizImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <bean id="userAction" class="com.cjh.vue.web.UserAction" parent="baseAction">
        <property name="userBiz" ref="userBiz"></property>
    </bean>
</beans>

  web层:

 

 

 

 

package com.cjh.crud.web;

import com.cjh.base.web.BaseAction;
import com.cjh.crud.biz.UserBiz;
import com.cjh.crud.entity.User;
import com.opensymphony.xwork2.ModelDriven;

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;
	}

	/**
	 * 用户登录
	 * 
	 * @param user`1
	 * @return
	 */

	public String login() {
		System.out.println(userBiz.login(user));
		return null;
	}

	/**
	 * 用户注册
	 * 
	 * @param user
	 * @return
	 */
	public String register() {
		userBiz.register(user);
		return null;
	}
	


	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}

	
	
}

  Tree树形:

dao:

package com.cjh.crud.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.cjh.base.dao.BaseDao;
import com.cjh.crud.entity.TreeNode;

public class TreeNodeDao extends BaseDao {

    /**
     * 这是加载树形菜单节点
     * @return
     */
    public List<TreeNode> query() {
        Session session = this.getHibernateTemplate().getSessionFactory().openSession();
        session.beginTransaction();
        List<TreeNode> treeNodes =  super.executeQuery(session, "from TreeNode where treeNodeType = 1", null,null); 
        return treeNodes;
    }


}

  
其他的配置和上面一样的外键不发了

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.cjh.crud.entity.TreeNode" table="t_crud_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.cjh.crud.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.cjh.crud.entity.TreeNode"/>
        </set>

    </class>
</hibernate-mapping>

  

web:

package com.cjh.crud.web;

import java.util.List;

import com.cjh.base.web.BaseAction;
import com.cjh.crud.biz.TreeNodeBiz;
import com.cjh.crud.entity.TreeNode;

/**
 * 节点请求Action
 */
public class TreeNodeAction extends BaseAction  {

    private TreeNodeBiz treeNodeBiz;

    public TreeNodeBiz getTreeNodeBiz() {
        return treeNodeBiz;
    }

    public void setTreeNodeBiz(TreeNodeBiz treeNodeBiz) {
        this.treeNodeBiz = treeNodeBiz;
    }

    /**
     * 加载树形菜单
     * @return
     */
    public String query(){
        List<TreeNode> list = treeNodeBiz.query();
       for (TreeNode t : list) {
		System.out.println(t);
	}
        return null;
    }


}

  增删改查:

dao:

package com.cjh.crud.dao;

import java.util.List;

import org.hibernate.Session;

import com.cjh.base.dao.BaseDao;
import com.cjh.crud.entity.Articles;

public class ArticlesDao extends BaseDao {
	/**
	 * 查询
	 * @return
	 */
	public List<Articles> list(){
		
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		session.beginTransaction();
		return super.executeQuery(session, " from Articles", null, null);
	}
	/**
	 * 删除
	 * @param articles
	 */
	public void remove(Articles articles) {
	 this.getHibernateTemplate().delete(articles);
	}
   /**
    * 添加
    * @param articles
    * @return
    */
	 public void add(Articles articles) {
		  this.getHibernateTemplate().save(articles);
		 
	 }
	 /**
	  * 修改
	  * @param articles
	  */
	 public void update (Articles articles) {
		 this.getHibernateTemplate().update(articles);
	 }
}

  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_crud_articles" name="com.cjh.crud.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:

package com.cjh.crud.web;

import java.util.List;

import com.cjh.base.web.BaseAction;
import com.cjh.crud.biz.ArticlesBiz;
import com.cjh.crud.entity.Articles;
import com.opensymphony.xwork2.ModelDriven;

public class ArticlesAction extends BaseAction implements ModelDriven<Articles> {
	private ArticlesBiz articlesBiz;

	public ArticlesBiz getArticlesBiz() {
		return articlesBiz;
	}

	public void setArticlesBiz(ArticlesBiz articlesBiz) {
		this.articlesBiz = articlesBiz;
	}

	private Articles articles = new Articles();

	@Override
	public Articles getModel() {
		// TODO Auto-generated method stub
		return articles;
	}

	public String list() {
		List<Articles> list2 = articlesBiz.list();
		for (Articles s : list2) {
			System.out.println(s);
		}
		return null;

	}

	public void remove() {
		articlesBiz.remove(articles);
	}

	public void update() {
		articlesBiz.update(articles);
	}

	public void add() {
		articlesBiz.add(articles);
	}

}

  

posted @ 2019-07-29 01:25  chenjiahao  阅读(122)  评论(0编辑  收藏  举报