关于当前搭建Spring Mvc和hibernate框架接口的总结

来源:互联网 发布:卡扎菲怎么死的 知乎 编辑:程序博客网 时间:2024/06/18 01:26

在hibernate中,进行了对jdbc的轻量级的封装,对数据库的操作可通过获取session然后进行一系列事务操作

在Spring中,则又提供了对hibernate进行了部分数据库操作的封装HibernateTemplate,这个类中包含了对hibernate事务操作的一系列开启事务  打开session等等,并且也提供了直接执行hql和criteria的操作的接口,当然对于特殊的需求,HibernateTemplate也可直接获取session进行操作

hibernate进行多表查询每个表中各取几个字段,也就是说查询出来的结果集没有一个实体类与之对应如何解决;

 解决方案一,按照Object[]数据取出数据,然后自己组bean

解决方案二,对每个表的bean写构造函数,比如表一要查出field1,field2两个字段,那么有一个构造函数就是Bean(type1 filed1,type2

field2) ,然后在hql里面就可以直接生成这个bean了。

以下为封装的模版
package com.cqut.dao.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.cqut.dao.base.DAOSupport;
import com.cqut.dao.user.customInterface.IUserDao;
import com.cqut.entity.user.User;
import com.cqut.util.StringUtil;
@Repository("userDao")
public class UserDao extends DAOSupport implements IUserDao {

    @Override
    public boolean save(User user) {
        try{
            this.template.save(user);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String saveEntityWithReturn(User user) {
        return  this.template.save(user).toString();
    }

    @Override
    public boolean saveEntities(final User[] users) {
        boolean result=(Boolean)this.template.execute(new HibernateCallback()
        {
            @Override
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                if(users!=null&&users.length>0){
                    try{
                        int length=users.length;
                        session.beginTransaction();
                        for(int i=0;i<length;i++){
                            session.save(users[i]);
                            if (i % 10 == 0) {  
                               session.flush();  
                               session.clear();  
                            }  
                        }
                        session.getTransaction().commit(); // 提交事物
                        return true;
                    }catch(Exception e){
                        e.printStackTrace();
                        session.getTransaction().rollback();
                    }finally {  
                        session.close(); // 关闭Session  
                    }
                    return false;
                }
                else{
                    return false;
                }
            }
        });
        return result;
    }

    @Override
    public boolean updateByID(User user, String userID) {
        user.setUserID(userID);
        try{
            this.template.update(user);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateByCondition(User user, String condition) {
        String sql="update User set "+user.toValueString()+" where "+condition;
        int count=this.template.bulkUpdate(sql);
        if(count>0){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean updateByEntity(User user, User userCondition) {
        String sql="update User set "+user.toValueString().replaceAll("AND", ",")+" where "+userCondition.toValueString();
        int count=this.template.bulkUpdate(sql);
        if(count>0){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean deleteByID(String userID) {
        User user=new User();
        user.setUserID(userID);
        return this.deleteByEntity(user);
    }
    
    @Override
    public boolean deleteByCondition(String condition) {
        String sql="delete from User where "+condition;
        int count=this.template.bulkUpdate(sql);
        if(count>0){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean deleteByEntity(User user) {
        try{
            this.template.delete(user);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public User getUserByID(String userID) {
        return this.template.get(User.class, userID);
    }
    
    @Override
    public Map<String, Object> findUserByID(String[] properties, String userID) {
        String[] property=StringUtil.analyseArray(properties);
        String hql=null;
        if(property[1]==""||property[1]==null){    //如果不存在别名
            hql="select new Map("+property[0]+") from User where userID='"+userID+"'";
        }
        else{
            hql="select new Map("+property[0]+") from "+property[1]+" where user.userID='"+userID+"'";
        }
        List<Map> maps=this.template.find(hql);
        if(maps!=null&&maps.size()>0){
            Map<String,Object> map=new HashMap<String,Object>();
            return (Map<String, Object>)maps.get(0);
        }
        else{
            return null;
        }        
    }

    @Override
    public User getUserByCondition(String condition) {
        String sql="from User where "+condition;
        List<User> users=this.template.find(sql);
        if(users!=null&&users.size()>0){
            return users.get(0);
        }
        else{
            return null;
        }
    }

    @Override
    public Map<String, Object> findUserByCondition(String[] properties,
            String condition) {
        String[] property=StringUtil.analyseArray(properties);
        String hql=null;
        if(property[1]==""||property[1]==null){    //如果不存在别名
            hql="select new Map("+property[0]+" ) from User where "+condition;
        }
        else{
            hql="select new Map("+property[0]+" ) from "+property[1]+" where "+condition;
        }
        List<Map> lists=this.template.find(hql);
        if(lists!=null&&lists.size()>0){
            return (Map<String, Object>)lists.get(0);
        }
        else{
            return null;
        }
    }

    @Override
    public List<User> getUsersByCondition(String condition) {
        String sql="from User where "+condition;
        List<User> users=this.template.find(sql);
        return users;
    }

    @Override
    public List<Map<String, Object>> findUsersByCondition(String[] properties,
            String condition) {
        String[] property=StringUtil.analyseArray(properties);
        String hql=null;
        if(property[1]==""||property[1]==null){    //如果不存在别名
            hql="select new Map( "+property[0]+" ) from User where "+condition;
        }
        else{
            hql="select new Map( "+property[0]+" ) from "+property[1]+" where "+condition;
        }
        List<Map<String,Object>> lists=this.template.find(hql);
        return lists;
    }

    @Override
    public List<User> getUsersByEntity(User user) {
        List<User> users=this.template.findByExample(user);
        return users;
    }

    @Override
    public List<Map<String, Object>> findUsersByEntity(String[] properties,
            User user) {
        String[] property=StringUtil.analyseArray(properties);
        String hql=null;
        if(property[1]==""||property[1]==null){    //如果不存在别名
            hql="select new Map("+property[0]+") from User where "+user.toValueString();
        }
        else{
            hql="select new Map( "+property[0]+" ) from "+property[1]+" where "+user.toValueString();
        }
        
        List<Map<String,Object>> lists=this.template.find(hql);
        return lists;
    }

}


0 0