这次说一下bind、多数据源支持、Java API
一、bind
1
2
|
// 测试bind List<Person> testBind( @Param ( "name" ) String name); |
1
2
3
4
5
6
|
<!--测试bind--> <!--相当于SQL select * from person where name like '%小强%' --> < select id = "testBind" resultType = "entity.Person" > < bind name = "bidname" value = "'%'+name+'%'" /> select * from person where name like #{bidname} </ select > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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 发现更多精彩 关注公众号:木子的昼夜编程 * 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作 */ 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 这个Mapper会编程Mybatis的代理Mapper PersonMapper mapper = session.getMapper(PersonMapper. class ); List<Person> list = mapper.testBind( "小强" ); Optional.ofNullable(list).orElse( new ArrayList<>()).forEach(item -> { System.out.println(item); }); } } } |
bind就是允许使用OGNL表达式创建一个变量(例如:bidname) ,然后将其绑定在当前上下文
二、 多数据库支持
搞了半天搞错了,浪费了点儿点儿时间
2.1 pom.xml
我用的jar包版本是3.4.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? 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我用了默认配置 没有自定义,下一篇天写一个自定义实现类的示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? 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是默认实现 这里可以定义自己的实现类 下一篇写--> < databaseIdProvider type = "DB_VENDOR" > <!--这里是因为原名称太长了 指定一下缩写 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
1
2
3
4
5
6
7
8
9
10
11
|
package dao; /** * @author 发现更多精彩 关注公众号:木子的昼夜编程 分享一个生活在互联网底层做着增删改查的码农的感悟与学习 * @create 2021-08-30 21:54 */ public interface PersonMapper { // 测试返回当前时间 String testDb(); } |
2.4 xml PersonMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? 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" > <!--选择不同的数据库--> < select id = "testDb" resultType = "string" > <!--如果是mysql 执行这个 --> < if test = "_databaseId == 'mysql'" > select CONCAT("mysql-->",#{_databaseId},"-->",now()) from dual </ if > <!--如果是oracle 执行这个--> < if test = "_databaseId == 'oracle'" > select "oracle-->"||#{_databaseId} from dual </ if > </ select > </ mapper > |
2.5 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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 发现更多精彩 关注公众号:木子的昼夜编程 * 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作 * @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 这个Mapper会编程Mybatis的代理Mapper PersonMapper mapper = session.getMapper(PersonMapper. class ); String type = mapper.testDb(); System.out.println( "数据库类型:" +type); } } |
可以看到我pom里边引入的是Mysql的驱动包,所以我这里结果肯定是Mysql,如果引入多个包,那么会默认使用databaseIdProvider第一个匹配到的,引入多个驱动下一篇写demo
输出结果:
下集预告:
- 自定义DatabaseIdProvider 自己定义_databaseId 类型
- databaseId标签使用 不再用if
- 引入多驱动 表现结果
到此这篇关于一小时迅速入门Mybatis之bind与多数据源支持 Java API的文章就介绍到这了,更多相关Mybatis bind 多数据源支持 Java API内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_36291682/article/details/120007279