Teach you step by step how to write a MyBatis entry program
1.Create file
Here I named it myself: Code_Book
and select the Maven project when building the system
2.Introduce related dependencies
pom.xml file
<?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>org.example</groupId>
<artifactId>Code_Book</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--引入Mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-parent</artifactId>
<version>37</version>
</dependency>
<!--引入测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.Create database
(1) Create a new database
(2) Create a new user table (remember the primary key to increase automatically)
(3) Enter data
4. Create database connection information configuration file
Create the db.properties file directly in the resources directory
mysql.driver = com.mysql.cj.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username = root
mysql.password = 123456
5. Create the core configuration file of Mybatis
Create the mybatis-config.xml file directly in the resources directory
<?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>
<!--
属性配置,一般用于配置数据库,resource用于引用数据库配置文件的位置,
配置好后可以使用${属性名}的形式使用
-->
<properties resource="db.properties"/>
<!--环境配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
</configuration>
6. Create entity classes (POJO entities)
(1) First create a new software package named: com
(2) Create a package in com as Study_1
(3) Create a pojo package; in the package, create a new entity class named User.
package com.Study_1.pojo;
public class User {
private int uid; //用户id
private String uname; //用户姓名
private int uage; //用户年龄
//写出Getter方法和Setter方法
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getUage() {
return uage;
}
public void setUage(int uage) {
this.uage = uage;
}
}
7. Create mapping files
(1) Create a mapper folder in the resources directory
(2) Create a mapping file in the mapper folder named: UserMapper.xml
<?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为映射的根节点-->
<mapper namespace="com.Study_1.pojo.User">
<!--id = 接口中的方法名
parameterType为传入的参数类型
resultType为返回实体类对象,使用包名、类名
-->
<select id="findById" parameterType="int" resultType="com.Study_1.pojo.User">
select * from users where uid = #{id}
</select>
</mapper>
8. Modify the mybatis-config.xml file
That is, add mapping file path configuration
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
The complete mybatis-config.xml file is as follows:
<?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>
<!--
属性配置,一般用于配置数据库,resource用于引用数据库配置文件的位置,
配置好后可以使用${属性名}的形式使用
-->
<properties resource="db.properties"/>
<!--环境配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<!--映射器-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
9.Write test classes
Create a Test package in com.Study_1
Then create the UserTest class in the Test package
package com.Study_1.test;
import com.Study_1.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLOutput;
public class UserTest {
@Test
public void userFindByIdTest(){
String resources = "mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resources);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlMapper.openSession();
User user = session.selectOne("findById",1);
System.out.println(user.getUname());
session.close();
}
}