Spring整合Hibernate:1、annotation方式管理SessionFactory

来源:互联网 发布:sql注入绕过 concat 0x 编辑:程序博客网 时间:2024/06/03 13:20
1、在applicationContext.xml文件中初始化SessionFactory(annotation方式)

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
<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:jdbc.properties" />
</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>
<!-- annotation方式管理hibernate的sessionFactory -->
<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.fz.annotation.model.User</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.format_sql">true</prop>
        </props>
    </property>
</bean>

一个数据库连接,只需要一个SessionFactory即可。通过sessionFactory来获取session,所以让Spring来管理hibernate的SessionFactory比较合适

因为,Spring管理的bean默认就是单例模式。

在使用了annotation方式之后就不需要配置User.hbm.xml文件了,例如,在<property>标签的list中定义的是对象,而不是User.hbm.xml

1.1 使用PackagesToScan来扫描包

查看annotatedClasses这个属性,每一个实体类都要写在这里,比较麻烦。

<property name="annotatedClasses">
        <list>
            <value>com.fz.annotation.model.User</value>
        </list>

</property>

所以,可以使用AnnotationSessionFactoryBean的一个packagesToScan属性即可


修改后的配置如下:以后的实体都放到相应的包里面即可

1
2
3
4
5
<property name="packagesToScan">
    <list>
        <value>com.fz.annotation.model</value>
    </list>
</property>


2、导入hibernate3的jar包


3、编写User实体类

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
package com.fz.annotation.model;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
@Entity
public class User {
    private int id;
    private String username;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
     
}

该实体类属性需要和数据库中字段名对应,不能多

4、使用sessionFactory

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
package com.fz.annotation.dao.impl;
 
import javax.annotation.Resource;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
 
import com.fz.annotation.dao.UserDao;
import com.fz.annotation.model.User;
 
 
@Repository("userDao")
public class UserDaoImpl implements UserDao{
    private SessionFactory sessionFactory;
    public void userAdd(User user) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}






来自为知笔记(Wiz)


0 0
原创粉丝点击