最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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異常棧

    淺析java異常棧

    給大家通過一個簡單的代碼實例給大家分型了java異常棧問題,需要的朋友參考一下吧。
    2017-12-12
  • 在Mac下IDEA安裝并使用protobuf方式(Java)

    在Mac下IDEA安裝并使用protobuf方式(Java)

    這篇文章主要介紹了在Mac下IDEA安裝并使用protobuf方式(Java),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java多線程環(huán)境下死鎖模擬

    Java多線程環(huán)境下死鎖模擬

    這篇文章主要介紹了模擬Java多線程環(huán)境下的死鎖,文章介紹一些死鎖的產(chǎn)生條件的相關(guān)資料,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2021-12-12
  • springboot中用fastjson處理返回值為null的屬性值

    springboot中用fastjson處理返回值為null的屬性值

    在本篇文章里小編給大家整理的是一篇關(guān)于springboot中用fastjson處理返回值問題詳解內(nèi)容,需要的朋友們參考下。
    2020-03-03
  • 為什么禁止在SpringBoot項目中使用@Autowired注解

    為什么禁止在SpringBoot項目中使用@Autowired注解

    寫代碼的時候突然發(fā)現(xiàn)?idea?在屬性注入的?@Autowired?注解上給出警告提示,spring?framework?4.0?以后開始出現(xiàn)的,spring?4.0?開始就不推薦使用屬性注入,改為推薦構(gòu)造器注入和?setter?注入,推薦的方法是使用基于構(gòu)造函數(shù)和基于setter的依賴注入
    2024-08-08
  • Spring Boot中操作使用Redis實現(xiàn)詳解

    Spring Boot中操作使用Redis實現(xiàn)詳解

    Spring Boot與Redis結(jié)合使用,通過使用Spring Data Redis來實現(xiàn)對Redis的操作,實現(xiàn)數(shù)據(jù)緩存和高效存儲,提高應(yīng)用程序的性能和響應(yīng)速度??梢岳肧pring Boot自帶的Redis Starter方便地集成和配置Redis
    2023-04-04
  • 詳解SpringCloud微服務(wù)架構(gòu)之Hystrix斷路器

    詳解SpringCloud微服務(wù)架構(gòu)之Hystrix斷路器

    本篇文章主要介紹了詳解SpringCloud微服務(wù)架構(gòu)之Hystrix斷路器,Hystrix是一個庫,通過添加延遲容差和容錯邏輯來幫助您控制這些分布式服務(wù)之間的交互,有興趣的可以了解一下
    2018-01-01
  • 分布式系統(tǒng)中的降級熔斷設(shè)計問題面試

    分布式系統(tǒng)中的降級熔斷設(shè)計問題面試

    這篇文章主要為大家介紹了分布式系統(tǒng)中的降級熔斷設(shè)計問題面試解答,有需要的朋友可以借鑒參考下,希望能有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • 模擬打印機排隊打印效果

    模擬打印機排隊打印效果

    本節(jié)主要介紹了模擬打印機排隊打印效果的具體實現(xiàn),感興趣的朋友可以參考下
    2014-07-07
  • logback自定義json日志輸出示例詳解

    logback自定義json日志輸出示例詳解

    這篇文章主要為大家介紹了logback自定義json日志輸出,就是通過logback日志體系以及l(fā)ogstash提供的json?log依賴將數(shù)據(jù)以json格式記錄到日志文件的例子
    2022-03-03

最新評論

大足县| 永昌县| 岳阳县| 璧山县| 洞口县| 友谊县| 固安县| 友谊县| 敖汉旗| 宜君县| 新龙县| 观塘区| 福清市| 天等县| 金湖县| 湘乡市| 泸水县| 通河县| 凤山县| 米泉市| 凤阳县| 汝阳县| 云阳县| 开化县| 崇礼县| 麟游县| 惠安县| 山阳县| 山西省| 绥芬河市| 射洪县| 沿河| 汾阳市| 卢湾区| 岳池县| 建昌县| 鄂州市| 云梦县| 甘肃省| 家居| 醴陵市|