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

詳解Spring與MyBatis的整合的方法

 更新時(shí)間:2022年03月18日 11:37:22   作者:深情的大鵝  
這篇文章主要為大家詳細(xì)介紹了Spring與MyBatis的整合,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

一、問(wèn)題提出

mybatis是根據(jù)mapper.xml文件動(dòng)態(tài)生成mapper接口的實(shí)現(xiàn)類,按照Spring的常規(guī)組件掃描的方式不能添加到IOC容器中,需要做出相應(yīng)的配置

二、解決方案

圖片摘自尚硅谷尚籌網(wǎng)項(xiàng)目文檔。

如圖可知,需要在Spring和mybatis整合的配置文件中配置

  • 啟動(dòng)Spring組件掃描
  • 數(shù)據(jù)源
  • SqlsessionFactoryBean
  • MapperScannerConfigurer

三、MyBatis逆向工程

pom文件

 
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
    </dependencies>
    <!-- 控制maven在構(gòu)建過(guò)程中的配置 -->
    <build>
        <!--構(gòu)建過(guò)程中用到的插件-->
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.1</version>
                <!--插件用的依賴-->
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.4.1</version>
                    </dependency>
                    <dependency>
                        <groupId>com.mchange</groupId>
                        <artifactId>c3p0</artifactId>
                        <version>0.9.5.5</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.25</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!--mybatis generator:generate -->
    <context id="wdhTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!--是否去除自動(dòng)生成的注釋 true:是 ;false:否 -->
            <property name="suppressAllComments" value="ture" />
        </commentGenerator>
        <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類、連接地址、用戶名、密碼 -->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mbook"
                userId="root"
                password="123456">
        </jdbcConnection>
        <!--
        默認(rèn) false,把 JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時(shí)把
        JDBC DECIMAL
        和 NUMERIC 類型解析為 java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--targetProject:生成 Entity類的路徑 -->
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.wdh.entity">
            <!--enableSubPackages:是否讓 schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
            <!--從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--targetProject:XxxMapper.xml映射文件生成的路徑 -->
        <sqlMapGenerator targetProject=".\src\main\java"
                         targetPackage="com.wdh.mapper">
            <!--enableSubPackages:是否讓 schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--targetPackage Mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetProject=".\src\main\java"
                             targetPackage="com.wdh.mapper">
            <!--enableSubPackages:是否讓 schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 數(shù)據(jù)庫(kù)表名字和我們的 entity類對(duì)應(yīng)的映射指定 -->
        <table tableName="login" domainObjectName="Login" />
    </context>
</generatorConfiguration>

生成結(jié)果:

 把生成的文件移動(dòng)到對(duì)應(yīng)的工程文件中

四、Spring與mybatis的配置文件

主模塊依賴jar包

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.3.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.16</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.7</version>
        </dependency>
    </dependencies>

spring-persist-mybatis.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"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">    <!--啟動(dòng)組件掃描-->    <context:component-scan base-package="com.wdh"/>    <!--引用jdbc.properties文件-->    <context:property-placeholder location="classpath:jdbc.properties"/>    <!--配置druid數(shù)據(jù)源-->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">        <property name="username" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}"/>        <property name="url" value="${jdbc.url}"/>        <!--注意這里是dirverClassName-->        <property name="driverClassName" value="${jdbc.driver}"/>    </bean>    <!--配置SqlSessionFactoryBean-->    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">        <!--配置數(shù)據(jù)源-->        <property name="dataSource" ref="dataSource"/>        <!--配置mybatis-config文件位置-->        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>        <!--配置mapper映射文件位置-->        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>    </bean>    <!--配置MapperScannerConfigurer-->    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!--配置要掃描的包-->        <property name="basePackage" value="com.wdh.mapper"/>    </bean></beans><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--啟動(dòng)組件掃描-->
    <context:component-scan base-package="com.wdh"/>
    <!--引用jdbc.properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置druid數(shù)據(jù)源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <!--注意這里是dirverClassName-->
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置mybatis-config文件位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <!--配置mapper映射文件位置-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
    </bean>
    <!--配置MapperScannerConfigurer-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置要掃描的包-->
        <property name="basePackage" value="com.wdh.mapper"/>
    </bean>
</beans>

 文件目錄

五、測(cè)試

//junit與Spring整合
@RunWith(SpringJUnit4ClassRunner.class)
//加載Spring配置文件
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class DataSourceTest {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private LoginMapper loginMapper;
    @Test
    public void connTest() throws SQLException {
        System.out.println(dataSource.getConnection());
    }
    @Test
    public void insertTest(){
        Login login = new Login(2, "wdh", "123", false);
        int i = loginMapper.insert(login);
        System.out.println("受影響的行數(shù):"+ i);
    }
}

 結(jié)果:

六、總結(jié)

 

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

  • Java令牌Token登錄與退出的實(shí)現(xiàn)

    Java令牌Token登錄與退出的實(shí)現(xiàn)

    這篇文章主要介紹了Java令牌Token登錄與退出的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    這篇文章主要介紹了SpringBoot接口中如何直接返回圖片數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • logback整合rabbitmq實(shí)現(xiàn)消息記錄日志的配置

    logback整合rabbitmq實(shí)現(xiàn)消息記錄日志的配置

    這篇文章主要介紹了logback整合rabbitmq實(shí)現(xiàn)消息記錄日志的配置,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12
  • MyBatis中動(dòng)態(tài)SQL語(yǔ)句@Provider的用法

    MyBatis中動(dòng)態(tài)SQL語(yǔ)句@Provider的用法

    本文主要介紹了MyBatis中動(dòng)態(tài)SQL語(yǔ)句@Provider的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java輕松入門(mén)冒泡?選擇?插入?希爾?歸并排序算法

    Java輕松入門(mén)冒泡?選擇?插入?希爾?歸并排序算法

    這篇文章主要介紹了Java常用的排序算法及代碼實(shí)現(xiàn),在Java開(kāi)發(fā)中,對(duì)排序的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來(lái)詳細(xì)說(shuō)說(shuō)Java常見(jiàn)的排序算法,需要的朋友可以參考一下
    2022-02-02
  • MyBatis 執(zhí)行動(dòng)態(tài) SQL語(yǔ)句詳解

    MyBatis 執(zhí)行動(dòng)態(tài) SQL語(yǔ)句詳解

    大家對(duì)mybatis執(zhí)行任意sql語(yǔ)句都了解,那么MyBatis執(zhí)行動(dòng)態(tài)SQL語(yǔ)句呢?下面腳本之家小編給大家解答下mybatis執(zhí)行動(dòng)態(tài)sql語(yǔ)句的方法,非常不錯(cuò),感興趣的朋友參考下吧
    2016-08-08
  • java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例

    java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例

    這篇文章主要介紹了java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • 關(guān)于Java下奇怪的Base64詳解

    關(guān)于Java下奇怪的Base64詳解

    這篇文章主要給大家介紹了關(guān)于Java下奇怪的Base64的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • spring mvc中@PathVariable / 帶斜杠方式獲取

    spring mvc中@PathVariable / 帶斜杠方式獲取

    這篇文章主要介紹了spring mvc中@PathVariable / 帶斜杠方式獲取,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • springboot 使用ThreadLocal的實(shí)例代碼

    springboot 使用ThreadLocal的實(shí)例代碼

    這篇文章主要介紹了springboot 使用ThreadLocal的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論