一、新建maven项目,方便框架导入
1. 在pom文件中添加依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13-beta-1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.1</version> </dependency>
|
2. 导入改变 (Import Changes)
二、在resource文件夹下创建配置文件
0. 日志配置文件
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d - %c -%-4r [%t] %-5p %c %x - %m%n
|
1. 新建数据库配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/book?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=
|
2.新建Mybatis配置文件mybatis-config.xml
1.导入数据库连接配置
<!-- 导入数据库连接配置 --> <properties resource="db.properties"></properties>
|
2.定义数据源
设置事务管理器为JDBC,连接源类型为连接池,并且设置引用数据库连接配置中的变量
<!-- 定义数据源 --> <environments default="development"> <environment id="development"> <!--配置事务管理器--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments>
|
3.设置映射文件的配置
在package的name属性中指明mapper.xml文件所在的路径
<!-- 设置映射文件的包 --> <mappers> <package name="io.github.coinsjack.mapper"></package> </mapper
|
三、测试使用
项目结构,这里需要注意,如果想要将xml文件和java文件分离,就需要在recourse和java目录下建立相同的mapper目录结构,java里面写interface而resources里面写xml文件
1. pojo中创建我们的实体类Board
package io.github.coinsjack.pojo;
public class Board {
private String user; private String time; private String message;
public Board() { }
public Board(String user, String time, String message) { this.user = user; this.time = time; this.message = message; }
public void setUser(String user) { this.user = user; }
public void setTime(String time) { this.time = time; }
public void setMessage(String message) { this.message = message; }
@Override public String toString() { return "Board{" + "user='" + user + '\'' + ", time='" + time + '\'' + ", message='" + message + '\'' + '}'; } }
|
2.编写接口BoardMapper 假设在这里我们随便取出一条记录
package io.github.coinsjack.dao;
import io.github.coinsjack.pojo.Board;
public interface BoardMapper {
Board selectBoard();
}
|
3.编写mapper.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="io.github.coinsjack.dao.BoardMapper"> <select id="selectBoard" resultType="io.github.coinsjack.pojo.Board"> SELECT * FROM board LIMIT 1; </select> </mapper>
|
这里需要注意,将namespace指向对应的mapper接口类型, 而select标签中的id属性应当和接口中的方法名一致
4.编写工具类MyBatisUtil.java
1.准备好mybatis配置文件的路径名,可以从resource往下写
2.配置文件->InputStream->SqlSessionFactory->SqlSession
3.SqlSession就是我们想要的,通过它来获取Mapper
package io.github.coinsjack.util;
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.IOException; import java.io.InputStream;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static { try { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } catch (IOException e) { e.printStackTrace(); } }
public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
|
5.编写测试类BoardMapperTest
package io.github.coinsjack.dao;
import io.github.coinsjack.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test;
public class BoardMapperTest {
@Test public void testSelectBoard() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); BoardMapper boardMapper = sqlSession.getMapper(BoardMapper.class); System.out.println(boardMapper.selectBoard()); } }
|
到这里我们就可以访问到数据库中记录了