溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何快速入門(mén)Mybatis中的bind與多數(shù)據(jù)源支持JavaAPI

發(fā)布時(shí)間:2021-09-15 09:09:50 來(lái)源:億速云 閱讀:133 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了如何快速入門(mén)Mybatis中的bind與多數(shù)據(jù)源支持JavaAPI,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

一、bind

// 測(cè)試bind
List<Person> testBind(@Param("name") String name);
<!--測(cè)試bind-->
<!--相當(dāng)于SQL select * from person where name like '%小強(qiáng)%' -->
<select id="testBind" resultType="entity.Person">
    <bind name="bidname" value="'%'+name+'%'" />
    select * from person where name like #{bidname}
</select>
import dao.PersonMapper;
import entity.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.*;

/**
 * @author 發(fā)現(xiàn)更多精彩  關(guān)注公眾號(hào):木子的晝夜編程
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 */
public class TestMain03 {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            PersonMapper mapper = session.getMapper(PersonMapper.class);
            List<Person> list = mapper.testBind("小強(qiáng)");
            Optional.ofNullable(list).orElse(new ArrayList<>()).forEach(item -> {
                System.out.println(item);
            });
        }
    }
}

bind就是允許使用OGNL表達(dá)式創(chuàng)建一個(gè)變量(例如:bidname) ,然后將其綁定在當(dāng)前上下文

二、 多數(shù)據(jù)庫(kù)支持

搞了半天搞錯(cuò)了,浪費(fèi)了點(diǎn)兒點(diǎn)兒時(shí)間

2.1 pom.xml

我用的jar包版本是3.4.5

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testDB</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- 引入Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!-- mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
</project>

2.2 mybatis-config.xml

databaseIdProvider我用了默認(rèn)配置 沒(méi)有自定義,下一篇天寫(xiě)一個(gè)自定義實(shí)現(xiàn)類(lèi)的示例

<?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>
    <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://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--DB_VENDOR是默認(rèn)實(shí)現(xiàn)  這里可以定義自己的實(shí)現(xiàn)類(lèi) 下一篇寫(xiě)-->
    <databaseIdProvider type="DB_VENDOR" >
        <!--這里是因?yàn)樵Q(chēng)太長(zhǎng)了 指定一下縮寫(xiě) xml中判斷類(lèi)型就用縮寫(xiě)名稱(chēng)判斷-->
        <property name="DB2" value="db2" />
        <property name="Oracle" value="oracle" />
        <property name="Adaptive Server Enterprise" value="sybase" />
        <property name="MySQL" value="mysql" />
    </databaseIdProvider>
    <!--掃描-->
    <mappers>
        <mapper resource="PersonMapper.xml"/>
    </mappers>
</configuration>

2.3 接口 PersonMapper

package dao;


/**
 * @author 發(fā)現(xiàn)更多精彩  關(guān)注公眾號(hào):木子的晝夜編程  分享一個(gè)生活在互聯(lián)網(wǎng)底層做著增刪改查的碼農(nóng)的感悟與學(xué)習(xí)
 * @create 2021-08-30 21:54
 */
public interface PersonMapper {
    // 測(cè)試返回當(dāng)前時(shí)間
    String testDb();
}

2.4 xml PersonMapper.xml

<?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="dao.PersonMapper">
    <!--選擇不同的數(shù)據(jù)庫(kù)-->
    <select id="testDb" resultType="string" >
        <!--如果是mysql 執(zhí)行這個(gè) -->
        <if test="_databaseId == 'mysql'">
            select CONCAT("mysql-->",#{_databaseId},"-->",now()) from dual
        </if>
        <!--如果是oracle 執(zhí)行這個(gè)-->
        <if test="_databaseId == 'oracle'">
            select "oracle-->"||#{_databaseId}  from dual
        </if>
    </select>

</mapper>

2.5 測(cè)試

import dao.PersonMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @author 發(fā)現(xiàn)更多精彩  關(guān)注公眾號(hào):木子的晝夜編程
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-09-02 21:42
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            PersonMapper mapper = session.getMapper(PersonMapper.class);
            String type = mapper.testDb();
            System.out.println("數(shù)據(jù)庫(kù)類(lèi)型:"+type);
    }
}

可以看到我pom里邊引入的是Mysql的驅(qū)動(dòng)包,所以我這里結(jié)果肯定是Mysql,如果引入多個(gè)包,那么會(huì)默認(rèn)使用databaseIdProvider第一個(gè)匹配到的,引入多個(gè)驅(qū)動(dòng)下一篇寫(xiě)demo

輸出結(jié)果:

如何快速入門(mén)Mybatis中的bind與多數(shù)據(jù)源支持JavaAPI

  1. 引入多驅(qū)動(dòng) 表現(xiàn)結(jié)果

到此這篇關(guān)于一小時(shí)迅速入門(mén)Mybatis之bind與多數(shù)據(jù)源支持 Java API的文章就介紹到這了,更多相關(guān)Mybatis bind 多數(shù)據(jù)源支持 Java API內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

上述內(nèi)容就是如何快速入門(mén)Mybatis中的bind與多數(shù)據(jù)源支持JavaAPI,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI