Spring笔记④--spring整合hibernate链接数据库
整合hibernate
整合什么?
- 有ioc容器来管理hibernate的SessionFactory
- 让hibernate使用上spring的声明式事务
先加入hibernate 驱动包
新建hibernate.cfg.xml
配置hibernate的基本属性
- 数据源需配置到IOC 容器中,所以在此处不再需要配置数据源
- 关联的.hbm.xml也在IOC 容器配置SessionFactory实例时进行配置。
- 配置hibernate的基本属性:方言,sql的显示及格式化,生成数据表的策略以及二级缓存等。
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</propert |
Ctrl+shift+T打开源码文件
在加入spring
Db.properties
|
jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring |
配置数据源
需导入
xmlns:context="http://www.springframework.org/schema/context"
|
<!-- 配置数据源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> |
测试类测试能否拿到datasource
|
public class Go { private static ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); public static void main(String[] args) throws SQLException { DataSource d= ctx.getBean(DataSource.class); System.out.println(d.getConnection()); } } |
通过spring来操作hibernate且使用spring的事务
Spring的applicationContext.xml配置文件
|
<?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"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring?userUnicode=true&characterEnchoding=utf-8"> </property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <!-- 指向hibernate的配置文件 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/ssh/spring_hibernate/entity/*.hbm.xml"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务的切点 --> <aop:config> <aop:pointcut expression="execution(* com.ssh.spring_hibernate.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <tx:annotation-driven transaction-manager="transactionManager" /></beans> |
两个Javabean
Account
private Integer id;
private int balance;
private String username;
Book
private Integer id;
private String bookName;
private String isbn;
private int stock;
private int price;
对应的hbm.xml
|
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ssh.spring_hibernate.entity.Account" table="SH_ACCOUNT"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="username" type="java.lang.String"> <column name="USER_NAME" /> </property> <property name="balance" type="int"> <column name="BALANCE" /> </property> </class> </hibernate-mapping> |
|
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ssh.spring_hibernate.entity.Book" table="SH_BOOK"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="bookName" type="java.lang.String"> <column name="BOOK_NAME" /> </property> <property name="isbn" type="java.lang.String"> <column name="ISBN" /> </property> <property name="price" type="int"> <column name="PRICE" /> </property> <property name="stock" type="int"> <column name="STOCK" /> </property> </class> </hibernate-mapping> |
Spring笔记④--spring整合hibernate链接数据库的更多相关文章
- Spring 学习笔记之整合Hibernate
Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQ ...
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- Spring Data初步--整合Hibernate
Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...
- Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- Spring笔记——Spring框架简介和初次框架配置
Spring简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Deve ...
- Spring学习笔记之整合hibernate
1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...
- Spring再接触 整合Hibernate
首先更改配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- Spring笔记——Spring+JDBC组合开发
使用Spring+JDBC集成步骤如下: 1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...
- Hibernate(链接数据库方便得多)!
首先让我们看一下配置文件,我这里先是用struts搞得controller,不明白struts的可以去百度一下这里就不讲解了: 之后我们需要做一个hibernate的配置文件内容如下(这里链接的是my ...
随机推荐
- p标签不自动换行原因
前言:发现以前写的就像是笔记,哪像博客啊,这里再次修改. 问题描述: 在固定宽度的p元素里(任何块级元素同理),长单词不自动换行,中文字符会自动换行,效果如:http://codepen.io/ali ...
- Lint Code 1365. Minimum Cycle Section
这题可以看作POJ 1961 最小循环节的一个简化版本.某补习班广告贴里给出的两个指针的参考解法简直大误. 受POJ 1961的启发,把数组看作字串,观察可知,如果字串全部由循环节构成(包括最后一段是 ...
- Android签名验证漏洞POC及验证
poc实际上就是一段漏洞利用代码,以下是最近炒得很火Android签名验证漏洞POC,来自https://gist.github.com/poliva/36b0795ab79ad6f14fd8 #!/ ...
- BootStrap 获得轮播中的索引和当前活动的焦点对象
$('#myCarousel').on('slide.bs.carousel', function (event) { var $hoder = $('#myCarousel').find('.ite ...
- 【python3】爬取鼠绘汉化的海贼王漫画
特别说明: 因为早些时候鼠绘的接口调整,之前的代码已经不能用了. 正好最近在学习scrapy,于是重新写了一个,项目放在github https://github.com/TurboWay/ishu ...
- 从Iris数据集开始---机器学习入门
代码多来自<Introduction to Machine Learning with Python>. 该文集主要是自己的一个阅读笔记以及一些小思考,小总结. 前言 在开始进行模型训练之 ...
- Kubernetes学习之路(十五)之Ingress和Ingress Controller
目录 一.什么是Ingress? 1.Pod 漂移问题 2.端口管理问题 3.域名分配及动态更新问题 二.如何创建Ingress资源 三.Ingress资源类型 1.单Service资源型Ingres ...
- AServer - 基于Asp.net core Kestrel的超迷你http服务器
AServer是基于ASP.NET Core Kestrel封装的一个超迷你http服务器.它可以集成进你的Core程序里,用来快速的响应Http请求,而不需要集成整个ASP.NET Core MVC ...
- Docker创建数据卷
当程序在容器中运行时,需要与其他容器中的程序或者容器外部的程序进行文件读写操作时,就需要进行数据的交换:容器内部的文件系统,是一个临时层,当容器停止运行并被删除时,这个临时层就会被一同丢弃:为了达到从 ...
- OpenGL(2)-窗口
写在前面 通过本节,你可以毫不费力的--->创建一个窗口 OpenGL中窗口,即载体 导入头文件 #include <glad/glad.h> #include <GLFW/g ...