Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQL语句,直接与对象打交道。 将Hibernate做完映射之后的对象交给Spring来管理是再合适不过的事情了, Spring也同时提供了对Hibernate的SessionFactory的集成功能。所以pring+hibernate整合对我们实际开发是非常有必要的。Spring整合hibernate有多种方式,我用的只是其中的一种,我这种不需要hibernate的配置文件,直接配置我们的beans.xml里了。
经测试需要在原先jar包上,新加入的包。
PS:在Spring中,当您每学习一个新的知识的时候,你可能不知道要加入哪个JAR包,这时不要乱加,你可以不断测试发现少哪个包就加哪个,千万别加多了,以免引起冲突。
示例:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
<context:component-scan base-package="net.zmcheng"/>
<bean id=" logInterceptor" class="net.zmcheng.aop.LogInterceptor"/>
<aop:config>
<aop:pointcut expression="execution(public * net.zmcheng.serviceImpl.UserServiceImpl.add())" id="servicePointCut"/>
<aop:aspect id="logAspect" ref=" logInterceptor">
<aop:before method="before" pointcut-ref="servicePointCut"/>
<aop:around method="aroundMethod" pointcut-ref="servicePointCut"/>
</aop:aspect>
</aop:config>
<!-- 设置数据源:提供了一个标准化的取得数据库连接的方式,一看到property我们就应该有一个意识,那就是这个类当中用这个属性的setter方法来进行注入 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/BSWS?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<!-- 整合Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 使用注解的方式
<property name="annotatedClasses">
<list><value>net.zmcheng.model.NlUser</value></list>
</property>
-->
<property name="mappingResources">
<list>
<value>net/zmcheng/model/NlUser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
|
PS:一看到property我们就应该有一个意识,那就是这个类当中用这个属性的setter方法来进行注入 。
daoImpl:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package net.zmcheng.daoImpl;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import net.zmcheng.dao.UserDao;
import net.zmcheng.model.NlUser;
@Component
public class UserDaoImpl implements UserDao {
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private SessionFactory sessionFactory;
public void add() {
try{
Session s = sessionFactory.openSession();
s.beginTransaction();
NlUser n = new NlUser();
n.setName("nihaomahh");
s.save(n);
s.getTransaction().commit();
System.out.println("添加员工成功");
}
catch (Exception e){
e.printStackTrace();
}
}
}
|
model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package net.zmcheng.model;
import java.util.Date;
@SuppressWarnings("serial")
public class NlUser implements java.io.Serializable{
private Integer id;
private String name;
private Double money=222.4;
private String number="1000000";
private String psw="123456";
public String getRpsw() {
return rpsw;
}
public void setRpsw(String rpsw) {
this.rpsw = rpsw;
}
private String rpsw;
private Date time=new Date();
public NlUser(){
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
|
NlUser.hbm.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="net.zmcheng.model.NlUser" table="User" catalog="BSWS">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="30" not-null="true" unique="true" />
</property>
<property name="psw" type="string">
<column name="psw" length="20" not-null="true" unique="true" />
</property>
<property name="money" type="double">
<column name="money" not-null="true" unique="true" />
</property>
<property name="time" type="timestamp">
<column name="time" not-null="true" unique="true" />
</property>
<property name="number" type="string">
<column name="number" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
|
测试结果:
成功在数据库中加入数据,其它的接口与类在之前篇博客中都贴了,本篇只贴了代码的类。
未经允许不得转载:程序没有猿 » Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能