源码地址:https://github.com/nieandsun/mybatis-study
感觉比较简单,有不懂或有兴趣的可以clone下来代码看一下,一看肯定就都懂了。不过多解释了,直接上代码。
<?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>com.nrsc</groupId>
<artifactId>mybatis-quick-start</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 单元测试相关依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 日志相关依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- mybatis相关依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
</project>
<?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>
<properties resource="db.properties"/>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置environment环境 -->
<environments default="development">
<!-- 环境配置1,每个SqlSessionFactory对应一个环境 -->
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<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>
<!-- 映射文件,指定mapper.xml的位置,有四种方式(详情可以看官网:https://mybatis.org/mybatis-3/configuration.html) -->
<mappers>
<mapper resource="mapper/TUserMapper.xml"/>
</mappers>
</configuration>
jdbc_driver=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/mybatis-study?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
jdbc_username=root
jdbc_password=123456
package com.nrsc.mybatis.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class TUser {
private Integer id;
private String userName;
private String realName;
private Byte sex;
private String mobile;
private String email;
private String note;
private Integer positionId;
}
package com.nrsc.mybatis.mapper;
import com.nrsc.mybatis.domain.TUser;
public interface TUserMapper {
TUser selectByPrimaryKey(Integer id);
}
<mapper namespace="com.nrsc.mybatis.mapper.TUserMapper">
<select id="selectByPrimaryKey" resultType="com.nrsc.mybatis.domain.TUser" parameterType="java.lang.Integer">
select
id,
user_name ,
real_name ,
sex,
mobile,
email,
note,
position_id positionId
from t_user
where id = #{id,jdbcType=INTEGER}
select>
mapper>
import com.nrsc.mybatis.domain.TUser;
import com.nrsc.mybatis.mapper.TUserMapper;
import lombok.extern.slf4j.Slf4j;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
@Slf4j
public class MybatisQuickStart {
private SqlSessionFactory sqlSessionFactory;
/****
* 构建全局变量SqlSessionFactory
* @throws IOException
*/
@Before
public void init() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1.读取mybatis配置文件创SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
}
/***
* 快速入门1 --- ibatis的方式,其实Mybatis底层也用的这种方式
*/
@Test
public void quickStart1() {
// 2.获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3.执行查询语句并返回结果 --- 第二次会走缓存
TUser user = sqlSession.selectOne("com.nrsc.mybatis.mapper.TUserMapper.selectByPrimaryKey", 1);
TUser user2 = sqlSession.selectOne("com.nrsc.mybatis.mapper.TUserMapper.selectByPrimaryKey", 1);
// 4.关闭session
sqlSession.close();
log.info("user111:" + user);
log.info("user111:" + user2);
}
/***
* mybatis的方式
*/
@Test
public void quickStart2() {
// 2.获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3.获取对应mapper
TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
// 4.执行查询语句并返回结果
TUser user = mapper.selectByPrimaryKey(1);
//5.关闭session
sqlSession.close();
log.info("user222:" + user);
}
}
启动quickStart1或quickStart2测试类,可以打印出如下sql,并获取到一条用户数据,证明Mybatis框架已经搭建成功。