标签:
整合hibernate
整合什么?
?
先加入hibernate 驱动包
新建hibernate.cfg.xml
配置hibernate的基本属性
<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 ? jdbc.initPoolSize=5 jdbc.maxPoolSize=10 |
?
?
配置数据源
需导入
????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笔记④--spring整合hibernate链接数据库
标签:
原文地址:http://www.cnblogs.com/chengzhipcx/p/4767935.html