一、MyBatis介绍
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
二、MyBatis安装
2.1 Maven项目
如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
2.2 JAR包安装
要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于lib中,并导入项目即可。
三、Mybatis功能架构
Mybatis功能架构分为三层:
四、快速Demo
pom.xml:
<?xml versinotallow="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:schemaLocatinotallow="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MyBatatisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
resource/mybatis-config.xml
<?xml versinotallow="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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://XXXX:3306/petsystem?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="XXXX"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.laccoliths.mapper"/>
</mappers>
</configuration>
resource/com.laccoliths.mapper/PetSystemMapper.xml
<?xml versinotallow="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="com.laccoliths.mapper.PetSystemMapper">
<select id="selectPetAll" resultType="com.laccoliths.pojo.Pet">
select * from pet;
</select>
<select id="selectPetById" resultType="com.laccoliths.pojo.Pet">
select * from pet where id = #{id};
</select>
</mapper>
main/java/com.laccoliths.pojo/Pet.java
package com.laccoliths.pojo;
/**
* @author Laccoliths
*/
public class Pet {
private String id;
private String name;
private String gender;
private String age;
private String varieties;
private String symptom;
private String isCure;
private String clientName;
private String doctorName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getVarieties() {
return varieties;
}
public void setVarieties(String varieties) {
this.varieties = varieties;
}
public String getSymptom() {
return symptom;
}
public void setSymptom(String symptom) {
this.symptom = symptom;
}
public String getIsCure() {
return isCure;
}
public void setIsCure(String isCure) {
this.isCure = isCure;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getDoctorName() {
return doctorName;
}
public void setDoctorName(String doctorName) {
this.doctorName = doctorName;
}
public Pet(String id, String name, String gender, String age, String varieties, String symptom, String isCure, String clientName, String doctorName) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.varieties = varieties;
this.symptom = symptom;
this.isCure = isCure;
this.clientName = clientName;
this.doctorName = doctorName;
}
@Override
public String toString() {
return "Pet{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", varieties='" + varieties + '\'' +
", symptom='" + symptom + '\'' +
", isCure='" + isCure + '\'' +
", clientName='" + clientName + '\'' +
", doctorName='" + doctorName + '\'' +
'}';
}
}
b. 加载核心配置文件,获取SqlSessionFactory对象
main/java/com.laccoliths/MyBattisDemo.java
// 新建Demo类
String resources = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
c. 获取SqlSession对象,执行SQL语句
// 获取SqlSession对象,用它来执行Sql语句
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Object> pets = sqlSession.selectList("PetMapper.selectPetAll");
for (Object pet : pets) {
System.out.println(pet.toString());
}
d. 释放资源
sqlSession.close();
完整Demo源码:
package com.laccoliths;
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;
import java.util.List;
/**
* @author Laccoliths
*/
public class MyBatisDemo {
public static void main(String[] args) throws IOException {
// 加载Mybatis核心配置文件,获取SqlSessionFactory
String resources = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取SqlSession对象,用它来执行Sql语句
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Object> pets = sqlSession.selectList("PetMapper.selectPetAll");
for (Object pet : pets) {
System.out.println(pet.toString());
}
sqlSession.close();
}
}