欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合Mybatis思路梳理總結(jié)

 更新時間:2022年12月08日 08:31:39   作者:-BoBooY-  
mybatis-plus是一個 Mybatis 的增強工具,在 Mybatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Mybatis-plus案例及用法實例的相關(guān)資料,需要的朋友可以參考下

導(dǎo)入相關(guān)jar包

1、junit

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
</dependency>

2、mybatis

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.5.2</version>
</dependency>

3、mysql-connector-java

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>

4、spring相關(guān)

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>

5、aspectJ AOP 織入器

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.9.4</version>
</dependency>

6、mybatis-spring整合包 【重點】

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.2</version>
</dependency>

7、配置Maven靜態(tài)資源過濾問題

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

回憶MyBatis

編寫pojo實體類

package com.kuang.pojo;
public class User {
   private int id;  //id
   private String name;   //姓名
   private String pwd;   //密碼
}

實現(xiàn)mybatis的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <typeAliases>
       <package name="com.bby.pojo"/>
   </typeAliases>
   <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC"/>
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
               <property name="username" value="root"/>
               <property name="password" value="1234"/>
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <package name="com.bby.dao"/>
   </mappers>
</configuration>

UserDao接口編寫

public interface UserMapper {
   public List<User> selectUser();
}

接口對應(yīng)的Mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bby.dao.UserMapper">
   <select id="selectUser" resultType="User">
    select * from user
   </select> 
</mapper>

測試類

@Test
public void selectUser() throws IOException {
   String resource = "mybatis-config.xml";
   InputStream inputStream = Resources.getResourceAsStream(resource);
   SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   SqlSession sqlSession = sqlSessionFactory.openSession();
   UserMapper mapper = sqlSession.getMapper(UserMapper.class);
   List<User> userList = mapper.selectUser();
   for (User user: userList){
       System.out.println(user);
  }
   sqlSession.close();
}

MyBatis-Spring學(xué)習(xí)

引入Spring之前需要了解mybatis-spring包中的一些重要類;

http://www.mybatis.org/spring/zh/index.html

什么是 MyBatis-Spring?

MyBatis-Spring 會幫助你將 MyBatis 代碼無縫地整合到 Spring 中。

知識基礎(chǔ)

在開始使用 MyBatis-Spring 之前,你需要先熟悉 Spring 和 MyBatis 這兩個框架和有關(guān)它們的術(shù)語。這很重要

MyBatis-Spring 需要以下版本:

MyBatis-SpringMyBatisSpring 框架Spring BatchJava
2.03.5+5.0+4.0+Java 8+
1.33.4+3.2.2+2.1+Java 6+

如果使用 Maven 作為構(gòu)建工具,僅需要在 pom.xml 中加入以下代碼即可:

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.2</version>
</dependency>

要和 Spring 一起使用 MyBatis,需要在 Spring 應(yīng)用上下文中定義至少兩樣?xùn)|西:一個 SqlSessionFactory 和至少一個數(shù)據(jù)映射器類。

在 MyBatis-Spring 中,可使用SqlSessionFactoryBean來創(chuàng)建 SqlSessionFactory。要配置這個工廠 bean,只需要把下面代碼放在 Spring 的 XML 配置文件中:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
</bean>

注意:SqlSessionFactory需要一個 DataSource(數(shù)據(jù)源)。這可以是任意的 DataSource,只需要和配置其它 Spring 數(shù)據(jù)庫連接一樣配置它就可以了。

在基礎(chǔ)的 MyBatis 用法中,是通過 SqlSessionFactoryBuilder 來創(chuàng)建 SqlSessionFactory 的。而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來創(chuàng)建。

在 MyBatis 中,你可以使用 SqlSessionFactory 來創(chuàng)建 SqlSession。一旦你獲得一個 session 之后,你可以使用它來執(zhí)行映射了的語句,提交或回滾連接,最后,當(dāng)不再需要它的時候,你可以關(guān)閉 session。

SqlSessionFactory有一個唯一的必要屬性:用于 JDBC 的 DataSource。這可以是任意的 DataSource 對象,它的配置方法和其它 Spring 數(shù)據(jù)庫連接是一樣的。

一個常用的屬性是 configLocation,它用來指定 MyBatis 的 XML 配置文件路徑。它在需要修改 MyBatis 的基礎(chǔ)配置非常有用。通常,基礎(chǔ)配置指的是 < settings> 或 < typeAliases>元素。

需要注意的是,這個配置文件并不需要是一個完整的 MyBatis 配置。確切地說,任何環(huán)境配置(),數(shù)據(jù)源()和 MyBatis 的事務(wù)管理器()都會被忽略。SqlSessionFactoryBean 會創(chuàng)建它自有的 MyBatis 環(huán)境配置(Environment),并按要求設(shè)置自定義環(huán)境的值。

SqlSessionTemplate 是 MyBatis-Spring 的核心。作為 SqlSession 的一個實現(xiàn),這意味著可以使用它無縫代替你代碼中已經(jīng)在使用的 SqlSession。

模板可以參與到 Spring 的事務(wù)管理中,并且由于其是線程安全的,可以供多個映射器類使用,你應(yīng)該總是用 SqlSessionTemplate 來替換 MyBatis 默認(rèn)的 DefaultSqlSession 實現(xiàn)。在同一應(yīng)用程序中的不同類之間混雜使用可能會引起數(shù)據(jù)一致性的問題。

可以使用 SqlSessionFactory 作為構(gòu)造方法的參數(shù)來創(chuàng)建 SqlSessionTemplate 對象。

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
 <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

現(xiàn)在,這個 bean 就可以直接注入到你的 DAO bean 中了。你需要在你的 bean 中添加一個 SqlSession 屬性,就像下面這樣:

public class UserDaoImpl implements UserDao {
 private SqlSession sqlSession;
 public void setSqlSession(SqlSession sqlSession) {
   this.sqlSession = sqlSession;
}
 public User getUser(String userId) {
   return sqlSession.getMapper...;
}
}

按下面這樣,注入 SqlSessionTemplate:

<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
 <property name="sqlSession" ref="sqlSession" />
</bean>

整合實現(xiàn)一

1、引入Spring配置文件beans.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

2、配置數(shù)據(jù)源替換mybaits的數(shù)據(jù)源

<!--配置數(shù)據(jù)源:數(shù)據(jù)源有非常多,可以使用第三方的,也可使使用Spring的-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode	=true&amp;characterEncoding=utf8"/>
   <property name="username" value="root"/>
   <property name="password" value="1234"/>
</bean>

3、配置SqlSessionFactory,關(guān)聯(lián)MyBatis

<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <!--關(guān)聯(lián)Mybatis-->
   <property name="configLocation" value="classpath:mybatis-config.xml"/>
   <property name="mapperLocations" value="classpath:com/bby/dao/*.xml"/>
</bean>                   

4、注冊sqlSessionTemplate,關(guān)聯(lián)sqlSessionFactory;

<!--注冊sqlSessionTemplate , 關(guān)聯(lián)sqlSessionFactory-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
   <!--利用構(gòu)造器注入 SqlSessionTemplate類需要注入一個參數(shù) 這個參數(shù)是sqlSessionFactory 查看源碼 它沒有set方法,所以只能通過構(gòu)造器注入-->
   <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

5、增加Dao接口的實現(xiàn)類;私有化sqlSessionTemplatex

public class UserDaoImpl implements UserMapper {
   //sqlSession不用我們自己創(chuàng)建了,Spring來管理
   private SqlSessionTemplate sqlSession;
   public void setSqlSession(SqlSessionTemplate sqlSession) {
       this.sqlSession = sqlSession;
  }
   public List<User> selectUser() {
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       return mapper.selectUser();
  }
}

6、注冊bean實現(xiàn)

<bean id="userDao" class="com.bby.dao.UserDaoImpl">
   <property name="sqlSession" ref="sqlSession"/>
</bean>

7、測試

   @Test
   public void test2(){
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       UserMapper mapper = (UserMapper) context.getBean("userDao");
       List<User> user = mapper.selectUser();
       System.out.println(user);
  }

結(jié)果成功輸出!現(xiàn)在我們的Mybatis配置文件的狀態(tài)!發(fā)現(xiàn)都可以被Spring整合!

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <typeAliases>
       <package name="com.bby.pojo"/>
   </typeAliases>
</configuration>

整合實現(xiàn)二

mybatis-spring1.2.3版以上的才有這個 .

官方文檔截圖 :

dao繼承Support類 , 直接利用 getSqlSession() 獲得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate , 而且對事務(wù)的支持更加友好 . 可跟蹤源碼查看

測試:

1、將我們上面寫的UserDaoImpl修改一下

public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {
   public List<User> selectUser() {
       UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
       return mapper.selectUser();
  }
}

2、修改bean的配置

<bean id="userDao" class="com.bby.dao.UserDaoImpl">
   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

3、測試

@Test
public void test2(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   UserMapper mapper = (UserMapper) context.getBean("userDao");
   List<User> user = mapper.selectUser();
   System.out.println(user);
}

到此這篇關(guān)于Spring整合Mybatis思路梳理總結(jié)的文章就介紹到這了,更多相關(guān)Spring整合Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java double類型相加精度問題的解決

    java double類型相加精度問題的解決

    這篇文章主要介紹了java double類型相加精度問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • springboot2中session超時,退到登錄頁面方式

    springboot2中session超時,退到登錄頁面方式

    這篇文章主要介紹了springboot2中session超時,退到登錄頁面方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java匹配正則表達(dá)式匯總

    Java匹配正則表達(dá)式匯總

    java匹配字符串表達(dá)式在我們數(shù)據(jù)處理方面是及其重要的,現(xiàn)在就把我這幾天數(shù)據(jù)處理比較常用的向大家介紹一下,常規(guī)的一些匹配方式就不介紹了,我們來學(xué)習(xí)一些特殊的,感興趣的朋友跟隨小編一起看看吧
    2023-03-03
  • java類Circle定義計算圓的面積和周長代碼示例

    java類Circle定義計算圓的面積和周長代碼示例

    要用Java計算圓的周長和面積,需要使用圓的半徑和一些數(shù)學(xué)公式,下面這篇文章主要給大家介紹了關(guān)于java類Circle定義計算圓的面積、周長的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • java8 stream 由一個list轉(zhuǎn)化成另一個list案例

    java8 stream 由一個list轉(zhuǎn)化成另一個list案例

    這篇文章主要介紹了java8 stream 由一個list轉(zhuǎn)化成另一個list案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • shardingsphered 線程安全問題示例分析

    shardingsphered 線程安全問題示例分析

    這篇文章主要為大家介紹了shardingsphered 線程安全問題示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Java并發(fā)編程之StampedLock鎖介紹

    Java并發(fā)編程之StampedLock鎖介紹

    這篇文章主要介紹了Java并發(fā)編程之StampedLock鎖,StampedLock是并發(fā)包里面JDK8版本新增的一個鎖,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Java線程中Thread方法下的Join方法詳解

    Java線程中Thread方法下的Join方法詳解

    這篇文章主要介紹了Java線程中Thread方法下的Join方法詳解,在項目中往往會遇到這樣一個場景,就是需要等待幾件事情都給做完后才能走下面的事情,這個時候就需要用到Thread方法下的Join方法,join方法是無參且沒有返回值的,需要的朋友可以參考下
    2024-01-01
  • java web實現(xiàn)郵箱激活與忘記密碼

    java web實現(xiàn)郵箱激活與忘記密碼

    這篇文章主要為大家詳細(xì)介紹了java web實現(xiàn)郵箱激活與忘記密碼、重置密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • mybatis實現(xiàn)表與對象的關(guān)聯(lián)關(guān)系_動力節(jié)點Java學(xué)院整理

    mybatis實現(xiàn)表與對象的關(guān)聯(lián)關(guān)系_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了mybatis實現(xiàn)表與對象的關(guān)聯(lián)關(guān)系_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下
    2017-09-09

最新評論