一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API
這次說一下bind、多數(shù)據(jù)源支持、Java API
一、bind
// 測試bind
List<Person> testBind(@Param("name") String name);
<!--測試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)注公眾號:木子的晝夜編程
* 一個(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()) {
// 通過sesson獲取Mapper 這個(gè)Mapper會編程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ù)庫支持
搞了半天搞錯(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太長了 指定一下縮寫 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)注公眾號:木子的晝夜編程 分享一個(gè)生活在互聯(lián)網(wǎng)底層做著增刪改查的碼農(nóng)的感悟與學(xué)習(xí)
* @create 2021-08-30 21:54
*/
public interface PersonMapper {
// 測試返回當(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ù)庫-->
<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 測試
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)注公眾號:木子的晝夜編程
* 一個(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();
// 通過sesson獲取Mapper 這個(gè)Mapper會編程Mybatis的代理Mapper
PersonMapper mapper = session.getMapper(PersonMapper.class);
String type = mapper.testDb();
System.out.println("數(shù)據(jù)庫類型:"+type);
}
}
可以看到我pom里邊引入的是Mysql的驅(qū)動包,所以我這里結(jié)果肯定是Mysql,如果引入多個(gè)包,那么會默認(rèn)使用databaseIdProvider第一個(gè)匹配到的,引入多個(gè)驅(qū)動下一篇寫demo
輸出結(jié)果:

下集預(yù)告:
- 自定義DatabaseIdProvider 自己定義_databaseId 類型
- databaseId標(biāo)簽使用 不再用if
- 引入多驅(qū)動 表現(xiàn)結(jié)果
到此這篇關(guān)于一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API的文章就介紹到這了,更多相關(guān)Mybatis bind 多數(shù)據(jù)源支持 Java API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用webmagic實(shí)現(xiàn)爬蟲程序示例分享
這篇文章主要介紹了使用webmagic實(shí)現(xiàn)爬蟲程序示例,需要的朋友可以參考下2014-04-04
SpringBoot啟動后的初始化數(shù)據(jù)加載原理解析與實(shí)戰(zhàn)
本文主要圍繞?Spring?Boot?啟動后的初始化數(shù)據(jù)加載展開,介紹了初始化任務(wù)的基本需求,包括全局配置加載、數(shù)據(jù)庫表初始化等,闡述了多種初始化加載方式,分析了它們的優(yōu)缺點(diǎn),需要的朋友可以參考下2024-11-11
swagger注解@ApiModelProperty失效情況的解決
這篇文章主要介紹了swagger注解@ApiModelProperty失效情況的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
使用SpringBoot+Prometheus+Grafana實(shí)現(xiàn)可視化監(jiān)控
本文主要給大家介紹了如何使用Spring?actuator+監(jiān)控組件prometheus+數(shù)據(jù)可視化組件grafana來實(shí)現(xiàn)對Spring?Boot應(yīng)用的可視化監(jiān)控,文中有詳細(xì)的代碼供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
SpringAop @Around執(zhí)行兩次的原因及解決
這篇文章主要介紹了SpringAop @Around執(zhí)行兩次的原因及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
IntelliJ?IDEA?2021.3?正式發(fā)布之支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)
IntelliJ?IDEA?2021.3?正式發(fā)布:支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)問題,在這個(gè)版本中的遠(yuǎn)程開發(fā)還不是一個(gè)正式版本,而是BETA版,但通過這個(gè)BETA版本,也可以體驗(yàn)IDEA“遠(yuǎn)程開發(fā)”給我們帶來的全新體驗(yàn)2021-12-12

