1、mybatis入门
项目中一直使用mybatis,一直在用,但是原理不甚了解,最近抽时间深入学习下,准备写一系列mybatis文章,记录下。
一、mybatis是什么?
mybatis是持久化框架。啥是持久化?可以简单的理解:把内存某一刻的状态持久化到文件(照片就是把某某一刻的状态记录下来)。框架就是把重复性工作抽象出来,大家开发只关注业务逻辑即可。具体详细的解释也不唠叨,大家可以关注官网mybatis;其实学习新的一门课程还是建议大家先去官网学习,不过有些官网的文档实在不敢恭维。
二、mybatis入门
任何一门技术的学习我们都先用它实现下helloworld。本人使用的是idea(2019.1.3) + maven(3.6.0)+ jdk(1.8)构建的项目。
github
使用mybatis有两种方式:1、使用xml配置。2、纯java配置(无xml)
pom配置如下就可以运行项目了:
<!--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.4</version>
</dependency>
1、使用xml配置
上面图片可知使用mybatis代码主要有那些核心的组成:
①model;②Mapper;③Mapper.xml(非必须);④mybatis-config.xml(核心配置文件)
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="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://127.0.0.1:3306/test?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper class="org.imzdong.study.mybatis.domain.AnnotationUserMapper" />
</mappers>
</configuration>
启动程序:
package org.imzdong.study.mybatis.demo;
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.imzdong.study.mybatis.domain.AnnotationUserMapper;
import org.imzdong.study.mybatis.domain.User;
import org.imzdong.study.mybatis.domain.XmlUserMapper;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: xml配置mybatis
* @author: Winter
* @time: 2020/4/6
*/
public class XmlMyBatisDemo {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
try (SqlSession session = build.openSession()) {
//User user = session.selectOne("org.imzdong.study.mybatis.domain.UserMapper.selectUserById", 1);
XmlUserMapper mapper = session.getMapper(XmlUserMapper.class);
User user = mapper.selectUserById(1);
System.out.println("user: "+user);
AnnotationUserMapper annotationMapper = session.getMapper(AnnotationUserMapper.class);
User userAnnotation = annotationMapper.selectUserById(1);
System.out.println("userAnnotation: "+userAnnotation);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
就是这么简单。
2、纯java配置更加简单,贴出代码大家感受下:
package org.imzdong.study.mybatis.demo;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.imzdong.study.mybatis.domain.AnnotationUserMapper;
import org.imzdong.study.mybatis.domain.User;
/**
* @description: 非xml配置mybatis
* @author: Winter
* @time: 2020/4/6
*/
public class JavaConfigMyBatisDemo {
public static void main(String[] args) {
//1、获取mysql数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC";
String userName = "root";
String password = "root";
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl(url);
dataSource.setUser(userName);
dataSource.setPassword(password);
//2、构建事务管理工厂
TransactionFactory transactionFactory = new JdbcTransactionFactory();
//3、构造环境
Environment environment = new Environment("development", transactionFactory, dataSource);
//4、初始化配置
Configuration configuration = new Configuration(environment);
configuration.addMapper(AnnotationUserMapper.class);
//configuration.addMapper(XmlUserMapper.class);
//5、获取SqlSessionFactory
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(configuration);
try (SqlSession session = build.openSession()) {
AnnotationUserMapper annotationMapper = session.getMapper(AnnotationUserMapper.class);
User userAnnotation = annotationMapper.selectUserById(1);
System.out.println("userAnnotation: "+userAnnotation);
/*XmlUserMapper mapper = session.getMapper(XmlUserMapper.class);
User user = mapper.selectUserById(1);
System.out.println("user: "+user);*/
}
}
}
3、小结
从java配置demo,详细的看到,mybatis步骤数据库连接、事务控制、配置初始化、获取SqlSessionFactory,就可以操作数据库了,非常简单。mybatis封装了重复的jdbc操作,比如启动连接,关闭连接,事务等,让开发人员仅通过配置搞定这些重复的工作。