一、spring整合hibernate
注意:框架的整合,和我们前面学习的各个框架的基础,有很大区别;有点面目全非;
1、添加依赖
spring、hibernate、mysql、c3p0、sprig-orm
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.17.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!--spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- c3p0连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency>
2、src目录中创建jdbc.properties
# 更多c3p0的属性配置,请参考:https://baike.baidu.com/item/c3p0/3719378?fr=aladdin driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://127.0.0.1:3306/spring-test?characterEncoding=utf-8 user=root password=123456 minPoolSize=1 maxPoolSize=25 initialPoolSize=1
3、spring-hibernate.xml中配置数据源
<!-- 引用属性资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源(C3P0连接池) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="${minPoolSize}" />
<!--连接池中保留的最大连接数。-->
<property name="maxPoolSize" value="${maxPoolSize}" />
<!-- 连接池初始化时的连接数 -->
<property name="initialPoolSize" value="${initialPoolSize}" />
</bean>
注意,这里需要先引用我们的属性资源文件
4、spring-hibernate.xml中配置会话工厂
<!-- 配置sessionFacotry会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate映射文件,加载po包中所有的hbm.xml映射文件 --> <property name="mappingLocations" value="classpath:com/yujun/maven/po/*.hbm.xml"/> <!-- hibernate属性配置 --> <property name="hibernateProperties"> <props> <!-- 数据库方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 是否在控制台打印SQL语句 --> <prop key="hibernate.show_sql">true</prop> <!-- 将控制台输出的SQL语句格式化 --> <prop key="hibernate.format_sql">true</prop> <!-- 是否让hibernate同步数据库和实体类 --> <prop key="hibernate.hbm2ddl.auto">none</prop> </props> </property> </bean>
此会话工厂的作用,就是将我们之前学习的hibernate的总配置文件(hibernate.cfg.xml)中的内容全部配置到spring中;把会话工厂交由spring进行统一管理;
5、UserInfo实体类
@Data @AllArgsConstructor @NoArgsConstructor public class UserInfo { private Integer id; private String no; private String name; private String pwd; private Integer sex; private Integer age; }
6、UserInfo.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"> <hibernate-mapping> <class name="com.yujun.maven.po.UserInfo" table="userInfo"> <id name="id" type="int"> <column name="id" /> <generator class="native" /> </id> <property name="no" type="java.lang.String"> <column name="no" /> </property> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="pwd" type="java.lang.String"> <column name="pwd" /> </property> <property name="sex" type="int"> <column name="sex" /> </property> <property name="age" type="int"> <column name="age" /> </property> </class> </hibernate-mapping>
此po包中的映射文件(xxx.hbm.xml)最终会被spring中注册的sessionFactory所加载
<!-- hibernate映射文件,加载po包中所有的hbm.xml映射文件 --> <property name="mappingLocations" value="classpath:com/yujun/maven/po/*.hbm.xml"/>
7、UserInfoDao接口
public interface UserInfoDao { //查询所有用户 List<UserInfo> queryAll(); }
8、UserInfoDaoImpl实现类
//HibernateDaoSupport:需要依赖sessionFactory ---> DataSource public class UserInfoDaoImpl extends HibernateDaoSupport implements UserInfoDao { private SessionFactory factory; //set方法会在spring注入属性值时给调用 public void setFactory(SessionFactory factory) { System.out.println("1----------------------------------------------"); this.factory = factory; //把会话工厂设置到父类HibernateDaoSupport中 super.setSessionFactory(factory); } //查询全部 @Override public List<UserInfo> queryAll() { return (List<UserInfo>)getHibernateTemplate().find("from UserInfo"); } }
9、spring-hibernate.xml中配置impl实现类
<bean class="com.yujun.maven.dao.impl.UserInfoDaoImpl">
<property name="factory" ref="sessionFactory"></property>
</bean>
10、查询全部
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-hibernate.xml"); UserInfoDao dao = context.getBean(UserInfoDao.class); List<UserInfo> list = dao.queryAll(); list.forEach(System.out::println); }