mybatis快速入门
前面文章牵扯到数据库连接,所以也对mybatis做个总结,主要分为快速搭建,mybatis运行原理,springboot-mybatis结合运行核心,以及现在更方便的mybatis-plus几个系列
ORM框架
Object Relational Mapping为了解决面向对象与关系数据库存在的互不匹配的现象的技术
快速搭建
依赖
<dependencies>
<!--mybatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mysql链接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--引入测试包-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter-api.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
mybatis配置类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--扫描POJO包,配合注解@Alias,后续给映射xml名字会比较短-->
<typeAliases>
<package name="com.daniel.entity"/>
</typeAliases>
<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://localhost:3306/daniel_test?serverTimeZone=GMT+8;useSSL=false;characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<!--包扫描的方式将所有mapper添加进去-->
<mappers>
<package name="com.daniel.mapper"/>
</mappers>
</configuration>
sqlSessionFactory的封装
public class MyBatisConfig {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = null;
if(sqlSessionFactory!=null){
sqlSession = sqlSessionFactory.openSession();
}
return sqlSession;
}
public static void closeSqlSession(){
SqlSession sqlSession = getSqlSession();
sqlSession.close();
}
public static <T> T getMapper(Class<T> type){
SqlSession sqlSession = getSqlSession();
return sqlSession.getMapper(type);
}
}
StudentMapper.xml
这里IDEA可以安装mybatis的插件,可以帮我们生成这个xml,跳转也非常方便
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--这里namespace要对应我们的mapper接口的,也是通过这个和我们接口关联起来的-->
<mapper namespace="com.daniel.mapper.StudentMapper">
<!--这是一个sql片段,方便公用-->
<sql id="studentAllFiles">
id,
name,
age,
address,
gender
</sql>
<select id="getAllStudent" resultType="Student">
select <include refid="studentAllFiles"/> from student;
</select>
</mapper>
Interface
public interface StudentMapper {
List<Student> getAllStudent();
}
Entity
@Alias("Student")
public class Student {
private Integer id;
private int age;
private String address;
private String gender;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", address='" + address + '\'' +
", gender='" + gender + '\'' +
", name='" + name + '\'' +
'}';
}
}
编写测试类
public class MapperTest {
@AfterEach
public void close(){
MyBatisConfig.closeSqlSession();
}
@Test
public void testGetAllStudent(){
List<Student> students = MyBatisConfig.getMapper(StudentMapper.class).getAllStudent();
System.out.println(students);
}
}
控制台输入: `[Student{id=1, age=20, address='陕西省西安市', gender='男', name='张三'}, Student{id=2, age=18, address='北京市', gender='女', name='Sisi'}]
OK,测试正常,mybatis就成功了。接下来总结下核心思想并debug下看下原理