一、什么是MyBatis
- mybatis是对jdbc的封装
- 将sql语句放在映射文件中(xml)
- 自动将输入参数映射到sql语句的动态参数上
- 自动将sql语句执行的结果映射成java对象
Mybatis工作原理
二、入门示例
创建mavn项目:
pom.xml
<?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.zy</groupId>
<artifactId>mybatis-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--log4j4日志-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<!--不需要显式的去写实体类的get set tostring方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!--这个必须配置,否则找不到配置文件-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
配置文件:
mysql.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=*******
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<!-- <File name="File" fileName="../logs/test.log" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File> -->
</appenders>
<loggers>
<logger name="com.lfy.mapping" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<!-- <logger name="log4j.logger.java.sql.Statement" level="info" additivity="false">
<appender-ref ref="File"/>
</logger>
<Logger name="com.lfy.mapping" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger> -->
<root level="DEBUG">
<appender-ref ref="Console"/>
<!-- <appender-ref ref="File"/> -->
</root>
</loggers>
</configuration>
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>
<!--引入mysql的配置文件-->
<properties resource="mysql.properties" />
<!--日志配置-->
<settings>
<setting name="logImpl" value="LOG4J2"/>
</settings>
<typeAliases>
<!--这种定义方式只能定义一种-->
<!--<typeAlias type="com.zy.entity.Blog" alias="Blog" />-->
<!--该包下的所有实体类都可以用类名作为别名-->
<package name="com.zy.entity" />
</typeAliases>
<!--定义数据源-->
<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>
<mappers>
<!--这种方式,如果增减一个mapper文件,都需要增加一个配置-->
<!--<mapper resource="com/zy/mapper/BlogMapper.xml"/>-->
<!--可以通过下面的方式,扫描整个包中的mapper文件-->
<package name="com.zy.mapper" />
</mappers>
</configuration>
代码:
定义pojo:
package com.zy.entity;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Builder(toBuilder = true)
public class Blog {
private Integer id;
private String title;
private int authorId;
private String state;
private Boolean featured;
private String style;
}
Mapper文件:
<?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">
<!--namespace一般是接口路径-->
<mapper namespace="com.huawei.mapper.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
<select id="insert" parameterType="Blog">
insert into Blog(id, title, author_id, state, featured, style)
values
(#{id}, #{title}, #{authorId}, #{state}, #{featured}, #{style});
</select>
</mapper>
定义接口:
package com.zy.mapper;
import com.huawei.entity.Blog;
public interface BlogMapper {
//接口中的方法名必须和mapper中sql语句的id相同
Blog selectBlog(Integer id);
//插入
void insert(Blog blog);
}
对应的类型:
定义DAO:
import com.huawei.entity.Blog;
import com.huawei.mapper.BlogMapper;
import com.huawei.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
public class BlogDao {
private SqlSession sqlSession = MyBatisUtil.getSqlSession();
private BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
public Blog queryBlogById(Integer id) {
return mapper.selectBlog(id);
}
public void insert(Blog blog) {
try {
mapper.insert(blog);
sqlSession.commit();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
工具类的编写:
package com.zy.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.Reader;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e) {
e.printStackTrace();
}
}
public MyBatisUtil() {
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
编写测试类:
package com.zy.mapper;
import com.huawei.dao.BlogDao;
import com.huawei.entity.Blog;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
public class BlogMapperTest {
private static final Logger logger = LogManager.getLogger(BlogMapperTest.class);
@Test
public void testSelectBlog() {
Blog blog = new BlogDao().queryBlogById(1);
System.out.println(blog);
logger.info("blog is:{}", blog);
}
@Test
public void testInsertBlog() {
Blog build = Blog.builder()
.authorId(1)
.state("ACTIVE")
.style("333")
.featured(false)
.title("三国演义")
.build();
new BlogDao().insert(build);
}
}
运行结果:
问题:
1.为什么代码中的sqlSession需要关闭,但是获取配置文件的Reader不需要关闭呢???
看源码:
查看build()方法:
public SqlSessionFactory build(Reader reader) {
return this.build((Reader)reader, (String)null, (Properties)null);
}
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
SqlSessionFactory var5;
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
var5 = this.build(parser.parse());
} catch (Exception var14) {
throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException var13) {
}
}
return var5;
}
我们发现,reader已经被关闭,因此不需要我们关闭
2.为什么插入数据时,需要手动提交呢???难道不能自动提交吗???
查看SqlSession的创建过程:
sqlSession = sqlSessionFactory.openSession();
进入DefaultSqlSessionFactory:
public SqlSession openSession() {
return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);
}
我们发现最后一个参数时false,这个参数时干嘛呢???继续往下看
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
}
return var8;
}
这个参数,其实就是autoCommit的参数,默认是false,表示不能自动提交,必须手动提交。
那么有没有支持传值的函数呢???
public SqlSession openSession(boolean autoCommit) {
return this.sqlSessionFactory.openSession(autoCommit);
}
显然是存在这样函数的,我们可以自己传参,来设置是否支持自动提交。
我的工具类可以改造为一个抽象类:
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.Reader;
public abstract class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
private static SqlSession sqlSession = null;
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e) {
e.printStackTrace();
}
}
public MyBatisUtil() {
}
public static SqlSession getSqlSession() {
sqlSession = sqlSessionFactory.openSession(true);
return sqlSession;
}
public <T> T getMapper(Class<T> typeClass) {
SqlSession sqlSession = getSqlSession();
return sqlSession.getMapper(typeClass);
}
public void close() {
if (sqlSession != null) {
sqlSession.close();
}
}
}
所有的Dao类都可以继承这个工具类,直接去获取Mapper,然后操作即可:
public class BlogDao extends MyBatisUtil {
private BlogMapper mapper = getMapper(BlogMapper.class);
public Blog queryBlogById(Integer id) {
return mapper.selectBlog(id);
}
public void insert(Blog blog) {
mapper.insert(blog);
this.close();
}
}