MyBatis学习一:快速入门
前言
公司要求没办法,前端也要了解一下后端知识,这里记录一下自己的学习
学习教程:黑马mybatis教程全套视频教程,2天Mybatis框架从入门到精通
文档:
https://mybatis.net.cn/index.html
MyBatis
快速入门:查询user表中所有数据
- 创建user表,添加数据
- 创建模块,导入坐标
- 编写mybatis核心配置文件 ---->替换连接信息,解决硬编码问题
- 编写sql映射文件—> 统一管理sql语句,解决硬编码问题
- 编码
- 定义POJO类,对应数据库的表
- 加载核心配置文件,获取SqlSessionFactory对象
- 获取SQLSession对象,执行sql语句
- 释放资源
创建模块
创建模块
File - > New -> Module
导入坐标
pom.xml
<dependencies>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- slf4j 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- logback 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- console表示当前日志信息是可以输出到控制台的-->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>【%level】 %cyan(【%thread】) %boldGreen(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<logger name="com.Carter_x" level="DEBUG" additivity="false">
<appender-ref ref="Console"/>
</logger>
</configuration>
核心配置文件
src/main/resources/mybatis-config.xml
<?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/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
sql映射文件
src/main/resources/UserMapper.xml
,表示是来操作user表的sql映射文件
<?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">
<!--
namespace: 命名空间
resultType:返回值的类型,一般是实体类
id:唯一标识
-->
<mapper namespace="test">
<select id="selectAllUser" resultType="com.example.pojo.User">
select * from user;
</select>
</mapper>
在src/main/java
下创建对于的实体类
package com.example.pojo;
public class User {
}
修改mybatis-config.xml
文件,指定刚才创建的sql映射文件
<mappers>
<!-- 加载sql映射文件-->
<mapper resource="userMapper.xml"/>
</mappers>
编码
修改之前创建的User
类,类的属性要与对应表里的字段对应
package com.example.pojo;
public class User {
private Integer id;
private String name;
private Integer age;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
编写测试类
编写一个测试类,用于测试代码
package org.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.example.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Main {
public static void main(String[] args) throws IOException {
// 1、加载mybatis的核心配置文件,获取SqlSessionFactory对象,下面的是从官网复制的
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3、执行sql语句,查询所有数据
// 指定要执行的sql语句,这里传入对应的标识,对应UserMapper.xml文件中<select id="selectAllUser" resultType="User">
List<User> userList = sqlSession.selectList("test.selectAllUser");
System.out.println(userList);
System.out.println("数据总条数:" + userList.size());
// 4、关闭资源
sqlSession.close();
}
}
执行结果