本文介绍 MyBatis 连接示例的配置依赖和配置文件。
配置依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
配置文件
mybatis-config.xml 文件
内容如下:
<?xml version="1.0" encoding="UTF8"?>
<!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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=utf-8&useServerPrepStmts=false&useCursorFetch=true"/>
<property name="username" value="a****"/>
<property name="password" value="******"/>
</dataSource>
</environment>
</environments>
<!--注册 mapper(mapper.xml 所在地址)-->
<mappers>
<mapper resource="com/test/UserMapper.xml"></mapper>
</mappers>
</configuration>
mapper.xml 文件
内容如下:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 是 mapper 接口,不能填错-->
<mapper namespace="com.test.UserMapper">
<select id="selectUser" resultType="com.test.User" fetchSize="40000">
select * from user;
</select>
<delete id="delete" >
delete from user;
</delete>
</mapper>