一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API
這次說(shuō)一下bind、多數(shù)據(jù)源支持、Java API
一、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)配置 沒有自定義,下一篇天寫一個(gè)自定義實(shí)現(xiàn)類的示例
<?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&characterEncoding=utf-8&useSSL=false&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)類 下一篇寫-->
<databaseIdProvider type="DB_VENDOR" >
<!--這里是因?yàn)樵Q太長(zhǎng)了 指定一下縮寫 xml中判斷類型就用縮寫名稱判斷-->
<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ù)類型:"+type);
}
}
可以看到我pom里邊引入的是Mysql的驅(qū)動(dòng)包,所以我這里結(jié)果肯定是Mysql,如果引入多個(gè)包,那么會(huì)默認(rèn)使用databaseIdProvider第一個(gè)匹配到的,引入多個(gè)驅(qū)動(dòng)下一篇寫demo
輸出結(jié)果:

下集預(yù)告:
- 自定義DatabaseIdProvider 自己定義_databaseId 類型
- databaseId標(biāo)簽使用 不再用if
- 引入多驅(qū)動(dòng) 表現(xiàn)結(jié)果
到此這篇關(guān)于一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API的文章就介紹到這了,更多相關(guān)Mybatis bind 多數(shù)據(jù)源支持 Java API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于java實(shí)現(xiàn)簡(jiǎn)單發(fā)紅包功能
這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)簡(jiǎn)單發(fā)紅包功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
在?Spring?Boot?中使用?Quartz?調(diào)度作業(yè)的示例詳解
這篇文章主要介紹了在?Spring?Boot?中使用?Quartz?調(diào)度作業(yè)的示例詳解,在本文中,我們將看看如何使用Quartz框架來(lái)調(diào)度任務(wù),Quartz支持在特定時(shí)間運(yùn)行作業(yè)、重復(fù)作業(yè)執(zhí)行、將作業(yè)存儲(chǔ)在數(shù)據(jù)庫(kù)中以及Spring集成,需要的朋友可以參考下2022-07-07
SpringBoot響應(yīng)處理實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot響應(yīng)處理實(shí)現(xiàn)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10

