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

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

 更新時(shí)間:2022年12月08日 08:31:39   作者:-BoBooY-  
mybatis-plus是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Mybatis-plus案例及用法實(shí)例的相關(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整合包 【重點(diǎn)】

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

7、配置Maven靜態(tài)資源過(guò)濾問(wèn)題

<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

編寫(xiě)pojo實(shí)體類(lèi)

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

實(shí)現(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接口編寫(xiě)

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

接口對(duì)應(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>

測(cè)試類(lèi)

@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包中的一些重要類(lèi);

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

什么是 MyBatis-Spring?

MyBatis-Spring 會(huì)幫助你將 MyBatis 代碼無(wú)縫地整合到 Spring 中。

知識(shí)基礎(chǔ)

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

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)|西:一個(gè) SqlSessionFactory 和至少一個(gè)數(shù)據(jù)映射器類(lèi)。

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

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

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

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

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

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

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

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

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

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

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

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

現(xiàn)在,這個(gè) bean 就可以直接注入到你的 DAO bean 中了。你需要在你的 bean 中添加一個(gè) 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>

整合實(shí)現(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、注冊(cè)sqlSessionTemplate,關(guān)聯(lián)sqlSessionFactory;

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

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

public class UserDaoImpl implements UserMapper {
   //sqlSession不用我們自己創(chuàng)建了,Spring來(lái)管理
   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、注冊(cè)bean實(shí)現(xiàn)

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

7、測(cè)試

   @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>

整合實(shí)現(xiàn)二

mybatis-spring1.2.3版以上的才有這個(gè) .

官方文檔截圖 :

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

測(cè)試:

1、將我們上面寫(xiě)的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、測(cè)試

@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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論