Python 官方文档:入门教程 => 点击学习
目录一、bind二、 多数据库支持2.1 pom.xml2.2 mybatis-config.xml2.3 接口 PersonMapper2.4 xml PersonMapper.x
这次说一下bind、多数据源支持、Java api
// 测试bind
List<Person> testBind(@Param("name") String name);
<!--测试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>
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.*;
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) ,然后将其绑定在当前上下文
搞了半天搞错了,浪费了点儿点儿时间
我用的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>
databaseIdProvider我用了默认配置 没有自定义,下一篇天写一个自定义实现类的示例
<?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>
package dao;
public interface PersonMapper {
// 测试返回当前时间
String testDb();
}
<?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>
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;
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
输出结果:
下集预告:
到此这篇关于一小时迅速入门Mybatis之bind与多数据源支持 Java API的文章就介绍到这了,更多相关Mybatis bind 多数据源支持 Java API内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 一小时迅速入门Mybatis之bind与多数据源支持 Java API
本文链接: https://lsjlt.com/news/135703.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0