注:虽然前面写过一些文档,包含MyBatis连接Db2和MySQL的内容,但是貌似没有单独记录用Maven方式连接DB的文档,所以单写了这一篇文档,方便以后需要快速搭建MyBatis环境时参考。
注:有一篇文档“MyBatis访问Db2小例子”,但是是用手工方式处理的各种包,比较麻烦,不像Maven,直接配置一下就搞定。
环境
- Ubuntu 22.04
- IntelliJ IDEA 2022.1.3
- JDK 17
- MyBatis 3.5.11
- Db2 11.5.0
- MySQL 8.0.30
代码
新建Maven项目 test1213
。
打开 pom.xml
文件,添加如下内容:
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.db2.jcc/db2jcc -->
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>db2jcc4</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
这样,就通过POM文件,直接配置了所需的包,不需要再手工处理了。
注:可以到 https://mvnrepository.com
看一下,各个包是否是最新版本。
<build></build>
是设置资源目录。所谓资源文件,会复制到 target
的相应目录下。资源目录的默认值是 src/main/resources
,所以 pom.xml
文件会自动复制到 target
的相应目录下。本例中因为在 src/main/java
中也有XML文件,所以需要显式设置资源目录。注意:设置 src/main/java
为资源目录,覆盖了默认设置,所以也需显式设置 src/main/resources
为资源目录。
创建POJO MyObject
如下:
package com.example.demo.pojo;
public class MyObject {
private int c1;
private String c2;
public int getC1() {
return c1;
}
public void setC1(int c1) {
this.c1 = c1;
}
public String getC2() {
return c2;
}
public void setC2(String c2) {
this.c2 = c2;
}
@Override
public String toString() {
return "MyObject{" +
"c1=" + c1 +
", c2='" + c2 + '\'' +
'}';
}
}
创建 MyMapper.java
文件如下:
package com.example.demo.dao;
import com.example.demo.pojo.MyObject;
import java.util.List;
public interface MyMapper {
List<MyObject> read();
}
在相同目录下创建 MyMapper.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="com.example.demo.dao.MyMapper">
<select id = "read" resultType="com.example.demo.pojo.MyObject">
select c1, c2 from t1
</select>
</mapper>
在 src/main/resources
目录下创建 mybatis-config.xml
文件如下:
<?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="db2">-->
<!-- <environment id="db2">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="com.ibm.db2.jcc.DB2Driver"/>-->
<!-- <property name="url" value="jdbc:db2://localhost:50000/sample"/>-->
<!-- <property name="username" value="db2inst1"/>-->
<!-- <property name="password" value="passw0rd"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/repo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.example.demo.dao.MyMapper"/>
</mappers>
</configuration>
注意对比Db2和MySQL的设置。
在 src/test/java
目录下,创建测试 TestMybatis.java
文件如下:
package com.example.demo.test;
import com.example.demo.dao.MyMapper;
import com.example.demo.pojo.MyObject;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMybatis {
@Test
public void Test1() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
try {
MyMapper mapper = sqlSession.getMapper(MyMapper.class);
List<MyObject> list = mapper.read();
list.forEach(System.out::println);
} finally {
sqlSession.close();
}
}
}
运行测试,结果如下:
MyObject{c1=1, c2='9800'}
MyObject{c1=2, c2='10200'}
在 mybatis-config.xml
里,切换到Db2,则测试结果如下:
MyObject{c1=1, c2='444'}
MyObject{c1=2, c2='222'}
MyObject{c1=3, c2='333'}
可见,MyBatis对底层DB做了抽象,切换Db2和MySQL时,业务代码无需更改。
代码结构如下: