一、步骤·
1.创建一个user表,添加数据
2.创建模板,导入.xml配置文件坐标
3.编写Mybatis核心配置文件
4.编写sql映射文件,统一管理sql语句,解决编码问题
5.编码
二、创建表、创建模块,导入坐标
建表语句:
drop table if exists tb_user;
create table tb_user(
id int primary key auto_increment,
username VARCHAR(20),
password VARCHAR(20),
gender CHAR(1),
addr VARCHAR(30)
);
INSERT INTO tb_user VALUES(1,'张三','123','男','北京');
INSERT INTO tb_user values(2,'张四','12','男','北京');
INSERT INTO tb_user values(3,'张五','1','男','北京');
创建的tb_user表:
注意创建数据库时,需要改编码格式为UTF-8,不然插入不了数据。
三、编写Mybatis核心配置文件
新建Maven项目,坐标参考Mybatis官网,可以直接去官网复制粘贴在pom.xml文件中
我的pom.xml文件:
<?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>myBatis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- mybati依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- 添加slf4j日志api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
四、编写Sql配置文件
<?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://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--sql映射表-->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
更改数据库连接信息,<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>其中mybatis为自己的数据库。如果报错的话就删除serverTimezone=UTC
<mappers> <!--sql映射表--> <mapper resource="UserMapper.xml"/> </mappers>
在mappers里面更改自己的映射文件,我的映射文件为UserMapper.xml
下图为mybatis官网的映射样板
配置好映射文件后,在UserMapper.xml映射文件中定义查询,更新等语句,这里仅定义了选择查询。resultType为返回值类型。
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 namespace="test">
<select id="selectAll" resultType="org.example.pojo.User">
select * from tb_user;
</select>
</mapper>
五 、编码
1.定义pojo类,编写User类,编写User类的构造方法并且重写toString方法返回User类的所有学习(格式化快捷键为alt+ctrl+L)
public class User {
private int id;
private String name;
private String password;
private String studentId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", studentId='" + studentId + '\'' +
'}';
}
}
2.加载Mybatis核心配置文件,获取SqlSessionFactory对象,通过SqlSessionFactory对象执行SQL语句
3.执行完成,释放资源
package org.example.pojo;
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;
public class MyBatisDemo {
public static void main(String[] args) throws IOException {
//加载核心配置文件,获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSess对象,用它执行sql语句
SqlSession sqlSession=sqlSessionFactory.openSession();
//执行sql语句
List<User> users = sqlSession.selectList("test.selectAll");
System.out.println(users);
//释放资源
sqlSession.close();
}
}