Spring整合Hibernate

Spring版本:3.2.0

Hibernate版本:3.6.0

applicationContext配置文件:

Spring整合Hibernate
<?xml version="1.0" encoding="utf-8"?>
<!-- Spring配置文件 -->
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:util="http://www.springframework.org/schema/util" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                           http://www.springframework.org/schema/util 
                           http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <!-- 数据源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/rbac</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
        <property name="maxActive">
            <value>20</value>
        </property>
        <property name="initialSize">
            <value>1</value>
        </property>
        <property name="maxWait">
            <value>60000</value>
        </property>
        <property name="maxIdle">
            <value>20</value>
        </property>
        <property name="minIdle">
            <value>3</value>
        </property>
         <property name="removeAbandoned">
             <value>true</value>
        </property>
        <property name="removeAbandonedTimeout">
            <value>180</value>
        </property>
        <property name="connectionProperties">
            <value>clientEncoding=UTF-8</value>
        </property>
    </bean>        
    
    <!-- 定义Hibernate的SessionFactory -->    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations" value="classpath:com/rbac/domain/*.hbm.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
                hibernate.show_sql = true
                hibernate.format_sql = true
            </value>
        </property>
    </bean>   
</beans>
Spring整合Hibernate

Dao实现类:

Spring整合Hibernate
package com.rbac.dao.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.rbac.dao.UserDao;
import com.rbac.domain.Menu;
import com.rbac.domain.User;
import com.rbac.utils.HibernateUtil;
import com.rbac.utils.Tree;

@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

    @Resource(name="sessionFactory")         
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }  


    /**
     * 根据用户名查询用户
     */
    public User getUserByName(User user) {
        String hql = "from User where username = ?";
        Object[] parameters = {user.getUsername()};
        List<User> list = (List<User>)getHibernateTemplate().find(hql, parameters);
        if(list.size()==1){
            return list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 获取用户所有的根菜单
     */
    public List<Menu> getRootUserMenus(User user) {
        String hql = "select rm.menu " +
                     "from UserRole as ur,RoleMenu as rm " +
                     "where ur.role.id = rm.role.id and rm.menu.pid is null and ur.user.id = ?";
        Object[] parameters = {user.getId()};
        List<Menu> list = getHibernateTemplate().find(hql, parameters);
        return list;
    }

    /**
     * 获取用户某一菜单下的所有子菜单
     */
    public List<Menu> getChildUserMenus(User user,Menu menu) {
        String hql = "from Menu as m where m.pid = ? and m.id in " +
                     "(select rm.menu.id from User as u,UserRole as ur,RoleMenu as rm " +
                     "where u.id = ur.user.id and ur.role.id = rm.role.id and u.id = ?)";
        Object[] parameters = {menu.getId(),user.getId()};
        List<Menu> list = getHibernateTemplate().find(hql, parameters);
        return list;
    }

}
Spring整合Hibernate

 

 

Spring整合Hibernate,布布扣,bubuko.com

Spring整合Hibernate

上一篇:C语言判断系统数据大/小端存储方式


下一篇:Java判断文件编码格式