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

菜鸟如何简单整合hibernate+spring(注解的方式)

2014-06-16 09:31 387 查看
首先加入hibernate+spring相关的jar包;

然后配置spring.xml,让spring来管理hibernate的连接池,session等等

<?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:context="http://www.springframework.org/schema/context"
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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config/>
<context:component-scan base-package="com.aode" />

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:log4j.properties</value>
</property>
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="packagesToScan">
<list>
<value>com.aode.model</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<!-- 事务 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<aop:config>
<aop:pointcut id="thisService"
expression="execution(public * com.aode.service..*.*(..))" />
<aop:advisor pointcut-ref="thisService"
advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- tx.end -->

<!-- 自创建session -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

</beans>


然后新建一个实体类,并加以注解,正向生成数据库字段:

package com.aode.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
//后台管理员测试表
@Entity
@Table(name="user")
public class UserModel {

private int id;
private String uname;
private String username;
private String password;
private int rank;

@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length = 20)
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Column(length = 10)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(length = 50)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(length = 4)
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}


新建一个继承HibernateDaoSupport的java类,并注入spring创建的sessionFactory;

最后在需要用到hibernate的CURD方法时继承这个类,就可以使用了:

//省略部分代码

public class UserDaoImp extends PLtemplate implements UserDao {

public boolean UserAdd(UserModel user) {
this.getHibernateTemplate().save(user);
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: