您的位置:首页 > 编程语言 > Java开发

学习整合hibernate springmvc spring的 心得(2)

2016-06-21 16:42 633 查看
上一篇建立了User类 Role类以及Authority这三个主要类

下面通过spring整合hibernate ,讲原来的hibernate的xml交给spring去管理

这是applicationContext并且开启事物注解

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url"
value="jdbc:mysql://localhost:3306/ssh_user?useUnicode=true&characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="527090038"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 初始化3个链接 -->
<property name="initialSize" value="3"></property>
<!-- 最多500个链接 -->
<property name="maxActive" value="500"></property>
<!-- 最大空闲 值-->
<property name="maxIdle" value="2"></property>
<!-- 最小空闲值 -->
<property name="minIdle" value="1"></property>

</bean>
<!-- 配置会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 接管了hibernate的对象映射文件 -->
<property name="mappingResources">
<list>
<value>com/caicai/entity/User.hbm.xml</value>
<value>com/caicai/entity/Authority.hbm.xml</value>
<value>com/caicai/entity/Role.hbm.xml</value>
<value>com/caicai/entity/Group.hbm.xml</value>
<value>com/caicai/entity/Information.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<!--  <props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>-->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
</value>
</property>
</bean>
<!-- 配置UseService对象 -->
<bean id="userService" class="com.caicai.service.imp.UserService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="roleService" class="com.caicai.service.imp.RoleService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="GroupService" class="com.caicai.service.imp.GroupService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置事务管理器 统一管理事物 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

如上配置的UserService RoleService GroupService 这三个类的关系如下图所示

具体实现类如下

这里介绍下各个类接口 之间的关系

有基础接口 基础实现类 里面写一写常用的方法

然后用户接口 用户实现类

还有角色接口 角色实现类

以及权限接口 权限实现类

关系已用户为例子 看下图



下面是basisinter的代码

package com.caicai.basis;

import java.util.List;

public interface Basisinter {
//基础接口.声明一些常用的方法
//通过id查询
public Object FindByid(Class c,java.io.Serializable id);
//查询 通过hql
public List ExcuteQuery(String hql,Object[]ps);
//增加
public void Add(Object obj);
//删除
public void Delete(Class c,java.io.Serializable id);
//修改
public void Update(Object obj);
//其他再说
}
下面是基础实现类basisService

package com.caicai.basis;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
//开启事务用到的注解
@Transactional
public class BasisService implements Basisinter{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public SessionFactory getSessionFactory() {
return sessionFactory;
}

@Override
public Object FindByid(Class c, Serializable id) {
// TODO Auto-generated method stub
Object obj=this.sessionFactory.getCurrentSession().get(c, id);
return obj;
}

@Override
public List ExcuteQuery(String hql, Object[] ps) {
// TODO Auto-generated method stub
Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
if(ps!=null&&ps.length!=0)
{

for(int i=0;i<ps.length;i++)
{
query.setParameter(i, ps[i]);
}
}
return query.list();
}

@Override
public void Add (Object obj) {
this.sessionFactory.getCurrentSession().save(obj);
// TODO Auto-generated method stub
}
@Override
public void Delete(Class c,Serializable id) {
// TODO Auto-generated method stub
//this.sessionFactory.getCurrentSession().load(c.getClass(),id);
//需要把对象加载出来再删除
this.sessionFactory.getCurrentSession().delete(this.sessionFactory.getCurrentSession().load(c,id));

}
@Override
public void Update(Object obj) {
// TODO Auto-generated method stub
//必须要有主键
this.sessionFactory.getCurrentSession().update(obj);
}

}
下面是UserServiceinter 这个接口

他这里写上User才使用到的一些方法

package com.caicai.service.inter;

import java.util.List;

import com.caicai.basis.Basisinter;
import com.caicai.entity.User;

//user的 接口
public interface UserServiceinter extends Basisinter{
public User Userlogin(User u);//验证登录
public List<User> UserfindBypage(int start,int size,String uname,Integer rid,Integer gid);
public Number UserfindCount(String uname,Integer rid,Integer gid);
}


至于UserService 这个类 他继承了基础类和实现了UserServiceinter

所以他具有基本的方法和自己独有的方法

package com.caicai.service.imp;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import com.caicai.basis.BasisService;
import com.caicai.ent
4000
ity.User;
import com.caicai.service.inter.UserServiceinter;
public class UserService extends BasisService implements UserServiceinter{
@Override//这个方法用来验证用户的登录
public User Userlogin(User u) {
User ru=null;
String hql="from User where uname = ? and upassword= ? ";
Object[] ps={u.getUname(),u.getUpassword()};
List list=this.ExcuteQuery(hql, ps);
Iterator it=	 list.iterator();
//保证没有重复的用户
while(it.hasNext())
{
ru=(User) it.next();
}
return ru;
// TODO Auto-generated method stub
}
//分页查询
@Override
public List UserfindBypage(int start, int size, String uname,
Integer rid,Integer gid) {
// TODO Auto-generated method stub
System.out.println("start"+start+"size"+size+"uname"+uname+"rid"+rid);
Criteria cta=this.getSessionFactory().getCurrentSession().createCriteria(User.class);
if(rid!=0)
{
cta.createAlias("urole", "r").add(Restrictions.eq("r.rid", rid));
}
if(gid!=0)
{
cta.createAlias("ugroup", "g").add(Restrictions.eq("g.gid", gid));
}
if(uname!=null&&!uname.equals(""))
{
cta.add(Restrictions.eq("uname", uname));
}
return cta.setFirstResult(start).setMaxResults(size).list();
}
//分页的记录条数查询
@Override
public Number UserfindCount(String uname, Integer rid,Integer gid) {
Criteria cta=this.getSessionFactory().getCurrentSession().createCriteria(User.class);
if(rid!=0)
{
cta.createAlias("urole", "r").add(Restrictions.eq("r.rid", rid));
}
if(gid!=0)
{
cta.createAlias("ugroup", "g").add(Restrictions.eq("g.gid", gid));
}
if(uname!=null&&!uname.equals(""))
{
cta.add(Restrictions.eq("uname", uname));
}
Number t=(Number) cta.setProjection(Projections.rowCount()).uniqueResult();
cta.setProjection(null);
// TODO Auto-generated method stub
return t;
}
}
其他类省略 使用basis 可以节省很多代码 把具有相同功能的代码写在一块
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息