1. Create a Maven project and configure pom.xml
<dependencies>
<!-- 添加MyBatis框架3.4.6版本 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version> <!-- 版本号视情况修改 -->
</dependency>
<!-- 添加MySql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
2. Create myBatis-config.xml to configure MyBatis
<?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.jdbc.Driver" /> <!-- 驱动类型 -->
<property name="url" value="jdbc:mysql://localhost:3306/sam?characterEncoding=utf-8" /> <!-- 连接字符串 -->
<property name="username" value="root" /> <!-- 用户名 -->
<property name="password" value="" /> <!-- 密码 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="DeptMapper.xml" /> <!-- 映射SQL语句的XML文件 -->
</mappers>
</configuration>
3. Create myBatis-config.xml mapping DeptMapper.xml SQL statement
<?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">
<mapper namespace="Dept">
<!-- 插入单个部门信息 -->
<insert id="InsertDept">
INSERT INTO DEPT (DNAME,LOC)
VALUES (#{DName},#{Loc})
</insert>
</mapper>
4. Create an entity class
package org.sang.entity;
/**
* @author: wangxiaobo
* @create: 2020-08-25 22:51
**/
public class Dept {
//部门名称
private String DName;
//部门位置
private String Loc;
public String getDName() {
return DName;
}
public void setDName(String dName) {
DName = dName;
}
public String getLoc() {
return Loc;
}
public void setLoc(String loc) {
Loc = loc;
}
5. Create the Main function
package org.sang.chenyanbin;
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.sang.entity.Dept;
import java.io.IOException;
import java.io.InputStream;
/**
* @author: wangxiaobo
* @create: 2020-08-25 22:52
**/
public class TestMain {
public static void main(String[] args) throws IOException {
//创建实体类
Dept dept = new Dept ();
dept.setDName ("上海事业部");
dept.setLoc ("上海1");
//加载XML文件
InputStream is = Resources.getResourceAsStream ("myBatis-config.xml");//加载MyBatis的配置文件
//初始化SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder ().build (is);
SqlSession session = factory.openSession ();
session.insert("InsertDept", dept);
session.commit ();
session.close ();
}
6.Analysis of MyBatis framework execution process
- Save the sql statement and database configuration information in the configuration file
- When MyBatis is running, the configuration information is stored in the Configuration object
- When creating a SqlSession object, provide attributes
- Configuration object
- Dirty: true->After the sql statement is executed, the transaction can be committed; false->sql statement execution sends an error, and the transaction is rolled back
- Executor executor object: Create a Statement object, rely on the MapperStatement object to bind the assignment content with the SQL placeholder during the creation process
- SqlSession.commit(): Absolutely commit and roll back according to the dirty attribute at this time
- SqlSession.close():