一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring4整合Hibernate5詳細步驟

Spring4整合Hibernate5詳細步驟

2020-09-21 15:52GW_Cheng Java教程

本篇文章主要介紹了Spring4整合Hibernate5詳細步驟,具有一定的參考價值,有興趣的同學可以了解一下

Spring與Hiberante整合

通過hibernate的學習,我們知道,hibernate主要在hibernate.cfg.xml配置文件中

接下來我們看一下hibernate的一個配置文件

hibernate配置文件

hibernate.cfg.xml

?
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
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- 指定連接數據庫所用的驅動 -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <!-- 指定連接數據庫的url,其中hibernate是本應用連接的數據庫名 -->
    <property name="connection.url">jdbc:mysql://localhost/hibernate_test</property>
    <!-- 指定連接數據庫的用戶名 -->
    <property name="connection.username">root</property>
    <!-- 指定連接數據庫的密碼 -->
    <property name="connection.password">cheng</property>
    <!-- 指定連接池里最大連接數 -->
    <property name="hibernate.c3p0.max_size">20</property>
    <!-- 指定連接池里最小連接數 -->
    <property name="hibernate.c3p0.min_size">1</property>
    <!-- 指定連接池里連接的超時時長 -->
    <property name="hibernate.c3p0.timeout">5000</property>
    <!-- 指定連接池里最大緩存多少個Statement對象 -->
    <property name="hibernate.c3p0.max_statements">100</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.validate">true</property>
    <!-- 指定數據庫方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- 根據需要自動創建數據表 -->
    <property name="hbm2ddl.auto">update</property><!--①-->
    <!-- 顯示Hibernate持久化操作所生成的SQL -->
    <property name="show_sql">true</property>
    <!-- 將SQL腳本進行格式化后再輸出 -->
    <property name="hibernate.format_sql">true</property>
    <!-- 避免這個錯誤信息Disabling contextual LOB creation as createClob() method threw error :java.lang.reflect.InvocationTargetException -->
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    <!-- 羅列所有持久化類的類名 -->
    <mapping class="com.wechat.entity.po.User"/>
    <mapping class="com.wechat.entity.po.Person"/>
  </session-factory>
</hibernate-configuration>

配置文件的作用

hibernate.cfg.xml文件的主要作用就是配置了一個session-factory

  1. 在session-factory中主要通過property配置一些數據庫的連接信息,我們知道,spring通常會將這種數據庫連接用dataSource來表示,這樣一來,hibernate.cfg.xml文件中的所有跟數據庫連接的都可以干掉了,直接用spring的dataSource,而dataSource也可以用c3p0、dbcp等。
  2. 在session-factory中通過property除了配置一些數據庫的連接信息之外,還有一些hibernate的配置,比如方言、自動創建表機制、格式化sql等,這些信息也需要配置起來。
  3. 還有最關鍵的一個持久化類所在路徑的配置

當不采用spring整合的時候,我們使用hibernate時主要是用hibernate從sessionFactory中去的session,然后用session來操作持久化對象,而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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
StandardServiceRegistry registry = null;
SessionFactory sessionFactory = null;
Session session = null;
Transaction transaction = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
@Before
public void init() {
 
  registry = new StandardServiceRegistryBuilder()
      .configure() // configures settings from hibernate.cfg.xml
      .build();
  sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
  session = sessionFactory.openSession();
  //開始事務
  transaction = session.getTransaction();
  transaction.begin();
}
 
@Test
public void testSaveUser() {
  User user = new User();
  user.setUsername("張學友");
  user.setPassword("jacky");
  user.setRegistDate(sdf.format(new Date()));
  File file = new File("D:"+File.separator+"ubuntu.png");
  String fileName = file.getName();
  String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
  System.out.println(prefix);
  InputStream input = null;
  try {
    input = new FileInputStream(file);
 
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
 
  Blob image = null;
  try {
    image = Hibernate.getLobCreator(session).createBlob(input,input.available());
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  user.setUserPic(image);
  session.save(user);
}
 
@After
public void destroy(){
  transaction.commit();
  session.close();
  sessionFactory.close();
  StandardServiceRegistryBuilder.destroy( registry );
}

Spring對hibernate的整合就是將上述三點通過spring配置起來,而hibernate最關鍵的sessionFactroy就是spring的一個bean

這些理解了整合就簡單了,

SessionFactoryBean

spring的sessionFactroy像下面這樣配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 加載配置文件 -->
  <context:property-placeholder location="classpath:jdbc.properties"
    file-encoding="utf-8" ignore-unresolvable="true" />
 
  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
      <list>
        <!-- 可以加多個包 -->
        <value>com.wechat.entity.po</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
      </props>
    </property>
  </bean>

通過bean的配置可以看出該bean就是hibernate的sessionFactroy

因為它指向了org.springframework.orm.hibernate5.LocalSessionFactoryBean

在這個bean中主要配置了上面說的三點:

  1. 數據源dataSource
  2. hibernate的配置,包括方言,輸出sql等
  3. 持久化類的位置,通過包進行掃描

下面給出數據源dataSource的配置

dataSource

?
1
2
3
4
5
6
7
8
9
<!-- 配置數據源 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close" p:driverClass="${jdbc.driverClassName}"
    p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"
    p:testConnectionOnCheckout="${jdbc.c3p0.testConnectionOnCheckout}"
    p:testConnectionOnCheckin="${jdbc.c3p0.testConnectionOnCheckin}"
    p:idleConnectionTestPeriod="${jdbc.c3p0.idleConnectionTestPeriod}"
    p:initialPoolSize="${jdbc.c3p0.initialPoolSize}" p:minPoolSize="${jdbc.c3p0.minPoolSize}"
    p:maxPoolSize="${jdbc.c3p0.maxPoolSize}" p:maxIdleTime="${jdbc.c3p0.maxIdleTime}" />

還有數據庫的連接信息

jdbc.properties

?
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
31
32
33
34
35
36
37
38
39
40
#-----------------------------------------------------
# 數據庫配置
#-----------------------------------------------------
#服務器地址
host=127.0.0.1
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://${host}:3306/hibernate_test
jdbc.username=root
jdbc.password=cheng
 
#-----------------------------------------------------
# 適用于c3p0的配置
#-----------------------------------------------------
#-----------------------------------------------------
# c3p0反空閑設置,防止8小時失效問題28800
#-----------------------------------------------------
#idleConnectionTestPeriod要小于MySQL的wait_timeout
jdbc.c3p0.testConnectionOnCheckout=false
jdbc.c3p0.testConnectionOnCheckin=true
jdbc.c3p0.idleConnectionTestPeriod=3600
#-----------------------------------------------------
# c3p0連接池配置
#-----------------------------------------------------
#initialPoolSize, minPoolSize, maxPoolSize define the number of Connections that will be pooled.
#Please ensure that minPoolSize <= maxPoolSize.
#Unreasonable values of initialPoolSize will be ignored, and minPoolSize will be used instead.
jdbc.c3p0.initialPoolSize=10
jdbc.c3p0.minPoolSize=10
jdbc.c3p0.maxPoolSize=100
#maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool.
jdbc.c3p0.maxIdleTime=3600
#-----------------------------------------------------
# hibernate連接池配置
#-----------------------------------------------------
hibernate.connection.driverClass=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://${host}:3306/${dbName}
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update

配置完這些還有spring強大的事務管理

?
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
<!-- 配置Hibernate事務管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>
 
  <!-- 配置事務異常封裝 -->
  <bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
 
  <!-- 基于數據源的事務管理器 -->
  <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource" /> -->
 
  <!-- 配合<tx:advice>和<aop:advisor>完成了事務切面的定義 -->
  <!-- 使用強大的切點表達式是語言輕松定義目標方法 -->
  <aop:config proxy-target-class="true">
    <!-- 通過aop定義事務增強切面 -->
    <aop:pointcut expression=" execution(* com.wechat.service..*(..))"
      id="serviceMethod" />
    <!-- 引用事務增強 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
  </aop:config>
  <!-- 事務增強 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- 事務屬性定義 -->
    <tx:attributes>
      <tx:method name="*" />
    </tx:attributes>
  </tx:advice>

好了,這些配置好之后就可以使用在spring中配置的sessionFactroy了

UserDao

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wechat.dao;
 
import java.util.List;
 
import com.wechat.entity.po.User;
 
public interface UserDao {
  // 得到所有用戶
  public List<User> getAllUser();
 
  // 檢測用戶名是否存在
  public boolean isExists(String username);
 
}

實現類

?
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
31
32
33
34
35
36
37
38
39
package com.wechat.dao.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import com.wechat.dao.UserDao;
import com.wechat.entity.po.User;
@Repository
public class UserDaoImpl implements UserDao {
  //注入sessionFactory
  @Autowired
  private SessionFactory sessionFactory;
 
  @SuppressWarnings("unchecked")
  @Override
  public List<User> getAllUser() {
    List<User> userList = new ArrayList<User>();
    String hsql="from User";
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hsql);
    userList = query.list();
    return userList;
  }
 
  @Override
  public boolean isExists(String username) {
    Query query = sessionFactory.openSession()
        .createQuery("from User u where u.username = :username").setParameter("username", username);
    System.out.println(query.list().size());
    return query.list().size()>0?true:false;
  }
 
}

UserService

?
1
2
3
4
5
6
7
8
9
10
11
package com.wechat.service.user;
 
import java.util.List;
 
import com.wechat.entity.po.User;
 
public interface UserService {
  public List<User> getAllUser();
  public boolean isExists(String username);
 
}

實現類

?
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.wechat.service.user.impl;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.wechat.dao.UserDao;
import com.wechat.entity.po.User;
import com.wechat.service.user.UserService;
@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserDao userDao;
  @Override
  public List<User> getAllUser() {
    return userDao.getAllUser();
  }
  @Override
  @Cacheable(cacheNames="isExists", key="#username")
  public boolean isExists(String username) {
    return userDao.isExists(username);
  }
 
}

因為事務管理是配置在service層,所以用service來測試

測試

?
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
package com.wechat.dao;
 
import java.util.List;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import com.wechat.entity.po.User;
import com.wechat.service.user.UserService;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-core.xml" })
public class UserServiceTest {
  @Autowired
  private UserService userService;
 
  @Test
  public void test() {
    List<User> userList = userService.getAllUser();
    for(User user:userList){
      System.out.println(user.getUsername());
    }
 
  }
 
}

輸入結果

?
1
2
3
4
5
6
7
8
9
10
11
Hibernate:
  select
    user0_.userid as userid1_2_,
    user0_.password as password2_2_,
    user0_.registDate as registDa3_2_,
    user0_.userPic as userPic4_2_,
    user0_.username as username5_2_
  from
    user_info user0_
程高偉
張學友

數據庫表

Spring4整合Hibernate5詳細步驟

好了Spring整合hibernate就寫到這里。

項目地址:https://github.com/peer44/testwechat

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/frankcheng5143/article/details/50634487

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人把大ji巴放进女人小说 | japonensis中国东北老人 | 成年人视频在线免费看 | 草莓视频深夜释放 | 高清国产精品久久久久 | 精品精品国产自在现拍 | 美女被狂干| 被老外玩爽的中国美女视频 | 日本免费高清在线 | 18国产精品白浆在线观看免费 | 欧美男男gaygaysxxx | 久久婷婷五月综合色精品首页 | 性做久久久久久久久浪潮 | 免费大片a一级一级 | 欧美色在线 | 成年视频在线观看免费 | 亚洲欧美精品一区天堂久久 | 青苹果乐园影院在线播放 | 美日韩一区二区三区 | 午夜影院小视频 | 久久精品成人免费网站 | 国色天香社区视频在线观看免费完整版 | 香蕉eeww99国产精选播放 | 亚洲欧美一区二区久久 | 亚洲美女啪啪 | 性欧美金发洋妞xxxxbbbb | 亭亭色| α级毛片 | 国产精品va在线观看手机版 | 日韩视频免费一区二区三区 | 日噜噜 | 免费观看成年肉动漫网站 | 222aaa精品影院| 5555国产在线观看精品 | 好大好猛好爽好深视频免费 | 国产男女乱淫真视频全程播放 | 校花的第一次好紧好爽 | 精品牛牛影视久久精品 | www.国产自拍 | 日韩欧美推理片免费看完整版 | 国产成人影院一区二区 |