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;
    }
}

Spring整合Hibernate:1、annotation方式管理SessionFactory的更多相关文章

  1. 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题

    问题描述: Error occured processing XML 'org/aopalliance/intercept/MethodInterceptor'. See Error Log for ...

  2. Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理

    1.加入DataSourceTransactionManager的命名空间 修改applicationContext.xml文件,增加如下内容: 1 2 3 4 5 6 7 8 9 10 11 12 ...

  3. Spring整合Hibernate的两种方式

    在使用spring注解整合hibernate时出现"org.hibernate.MappingException: Unknown entity: com.ssh.entry.Product ...

  4. java框架之Spring(4)-Spring整合Hibernate和Struts2

    准备 导包 Struts2 导入 Struts2 zip 包解压目录下 'apps/struts-blank.war' 中所有 jar 包,如下: asm-3.3.jar asm-commons-3. ...

  5. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  6. 二 SSH整合:Spring整合Hibernate,无障碍整合&无核心配置整合,Hibernate模版常用方法,

    重建SSH项目 java项目可以直接复制,但是web项目除了改名字还要该配置,如下: 方式一:无障碍整合:带Hibernate配置文件 <?xml version="1.0" ...

  7. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  8. 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能

    1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...

  9. Spring整合Hibernate笔记

    1. Spring 指定datasource a) 参考文档,找dbcp.BasicDataSource i. c3p0 ii. dbcp <bean id="dataSource&q ...

随机推荐

  1. 前端虚拟接口mockjs的使用

    最近在学习VueJS,也进一步学习了ES6,看了一遍之后,难免手痒,所以想仿写点什么,但是数据是个问题,你总不能写个分页,写个轮播吧,但是在公司做自己的东西找后台要接口也不那么像回事,怎么办呢? 无意 ...

  2. mac/linux查询网络端口占用

    参考:http://www.cnblogs.com/kaiye/archive/2013/05/25/3099393.html netstat命令 netstat -an|grep 8080 lsof ...

  3. Selenium+Python定位实例

    常见的定位方式参见:http://www.cnblogs.com/ranxf/p/7928732.html 1.ID定位(find_element_by_id) <input class=&qu ...

  4. java多态 以及静态绑定 动态绑定积累

    重载,英文名是overload,是指在一个类中定义了一个以上具有相同名称的方法,这些方法的参数个数.参数类型和顺序不能相同.返回类型可以相同,也可以不同. 重写,英文名是overrid,是指在继承情况 ...

  5. 一键安装lnmp(1)

    #!/bin/bash#author:zhaocl#Software directory:$pathpath=`pwd`cd $path. $path/cacti.sh. $path/nginx.sh ...

  6. 20145312 实验五《Java网络编程》

    20145312 实验五<Java网络编程> 一. 实验内容及要求 实验内容: 运行下载的TCP代码,结对进行,一人服务器,一人客户端: 利用加解密代码包,编译运行代码,一人加密,一人解密 ...

  7. Linux内存管理的基本框架⭐⭐

    Linux内核的映射机制设计成三层,在页面目录和页面表中间增设了一层“中间目录”.在代码中,页面目录称为PGD,中间目录称为PMD,而页面表称为PT.PT中的表项称为PTE,PTE是“Page Tab ...

  8. php 与 c++ openssl 加密通信

    $key = '1234567890123456'; $iv = '1234567890123456'; $enc = openssl_encrypt("hello wolrd!" ...

  9. yarn命令使用

    yarn 常用命令 修改日期 2017.12.26 最初接触 yarn 还是在 0.17.10 版本,由于各种各样的原因,使用时没 npm 顺手, 目前 yarn 的版本已经升级为 1.3.2 各种之 ...

  10. Animal_human_kp人脸与马脸迁移学习GitHub 论文实现

    Interspecies Knowledge Transfer for Facial Keypoint Detection关键点检测   Github地址:Interspecies Knowledge ...