学习笔记,有不对的地方还请大佬们指出
目录
项目结构
service层和dao层的结构可以参考这篇文章:Java的service层和dao层应该怎么写
在pom.xml文件中添加依赖
Maven的使用可以参考这篇文章:项目管理工具 Maven 的下载,安装,配置以及项目的创建和管理
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>xyz.smallsweetsgroupId>
<artifactId>with_to_spring_hibernateartifactId>
<version>1.0version>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.12version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>5.3.7.Finalversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
dependencies>project>
单独配置hibernate
步骤:
1.连接数据库
2.创建主配置文件
3.根据数据库自动生成实体类和映射文件
因为本篇主要记录怎么使用spring整合hibernate,所以配置hibernate的具体步骤就不写了,大家可以参考下面的教程:
spring整合hibernate
有关spring可以参考这些文章
创建spring主配置文件
修改spring主配置文件命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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="myDatabaseSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">-->
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="10" />
bean>
这里只需要根据你的实际情况改变url,username,password即可
创建SessionFactory对象
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDatabaseSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="mappingLocations" value="Student.hbm.xml" />
bean>
dataSource的属性值是你上面创建的数据源对象的名称,也就是id
configLocation的属性值是hibernate主配置文件的路径
mappingLocation的属性值是实体类映射文件的路径
创建HibernateTemplate对象
这是hibernate的模板,通过这个对象可以对数据库执行添加,删除,更新等操作
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"/>
bean>
sessionFactory属性的属性值是我们上面创建的SessionFactory对象
创建事务对象
事务管理器,事务通知,事务切面
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="pct" expression="execution(* dao.student.studentImpl.studentDaoImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pct"/>
aop:config>
创建dao层实现类对象
直接对数据库进行操作
<bean id="dao" class="dao.student.studentImpl.studentDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
bean>
创建service层实现类对象
面向用户,但不直接操作数据库,通过dao层实现类对象操作数据库
<bean id="serviceImpl" class="service.student.studentImpl.studentServiceImpl">
<property name="studentDaoImpl" ref="dao"/>
bean>
spring主配置文件完整内容
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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="myDatabaseSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">-->
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="10" />
bean>-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDatabaseSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="mappingLocations" value="Student.hbm.xml" />
bean>-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<bean id="serviceImpl" class="service.student.studentImpl.studentServiceImpl">
<property name="studentDaoImpl" ref="dao"/>
bean>
<bean id="dao" class="dao.student.studentImpl.studentDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="pct" expression="execution(* dao.student.studentImpl.studentDaoImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pct"/>
aop:config>beans>
测试
测试类
package Test;import service.student.studentImpl.studentServiceImpl;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.Student;public class MyTest {
@Test
public void test1(){// spring主配置文件名
String name = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(name);// 获取service实现类对象,spring中id为serviceImpl的bean
studentServiceImpl service = (studentServiceImpl) applicationContext.getBean("serviceImpl");// 创建实体类对象
Student student = new Student();
student.setName("张强");
student.setPassword("111");
student.setAddress("河北");
student.setPhone("123456");
student.setSex("男");// 调用添加方法
service.addInfo(student);
}}
执行过程
执行结果