1、该项目引入的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.oopsguy.kaptcha</groupId>
<artifactId>kaptcha-spring-boot-starter</artifactId>
<version>1.0.0-beta-2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.demo.DemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2、引入配置文件
(1)yml的
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
username: 你的数据库名称
password: 你的数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
configuration: # 配置打印 MyBatis⽇志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/**Mapper.xml
(2)properties的
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?
characterEncoding=utf8&useSSL=false
#连接数据库的⽤⼾名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=root
# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml
3、我们设置的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="com.bite.mybatis.mapper.UserInfoXmlMapper">
</mapper>
4、查找操作(只有查询需要写 resultType="com.example.demo.UserInfo")
(1)各个文件存放的位置
(2)接口代码
package com.example.demo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXmlMapper {
List<UserInfo> queryUserList();
}
(3)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="com.example.demo.UserInfoXmlMapper">
<select id="queryUserList" resultType="com.example.demo.UserInfo">
select * from userinfo
</select>
</mapper>
(4)测试部分代码(@SpringBootTest记得这个注解一定要加)
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class UserInfoXmlMapperTest {
@Autowired
private UserInfoXmlMapper userInfoXmlMapper;
@Test
void queryUserList() {
userInfoXmlMapper.queryUserList();
}
}
5、插入操作
(1)接口部分代码
Integer insert(UserInfo userInfo);
(2)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="com.example.demo.UserInfoXmlMapper">
<select id="queryUserList" resultType="com.example.demo.UserInfo">
select * from userinfo
</select>
<insert id="insert">
insert into
userinfo
(username,password,age,gender,phone)
values
(#{username},#{password},#{age},#{gender},#{phone})
</insert>
</mapper>
(3)测试部分代码
@Test
void insert() {
UserInfo userInfo=new UserInfo();
userInfo.setUsername("wanwu");
userInfo.setAge(17);
userInfo.setPassword("147258369");
userInfo.setGender(1);
userInfoXmlMapper.insert(userInfo);
}
6、删除操作
(1)接口部分代码
Integer delete(Integer id);
(2)XML部分代码
<delete id="delete">
delete from userinfo where id=#{id}
</delete>
(3)测试部分代码
@Test
void delete() {
userInfoXmlMapper.delete(14);
}
7、改操作
(1)接口部分代码
Integer update(String password,Integer id);
(2)XML部分代码(idea创建的项目)
<update id="update">
update userinfo set
password=#{password}
where id=#{id}
</update>
(2)XML部分代码(阿里云创建的项目)
<update id="update">
update userinfo set
password=#{param1}
where id=#{param2}
</update>
(3)测试部分代码
@Test
void update() {
userInfoXmlMapper.update("999999",15);
}
8、mybatis赋值失败原因
(1)起别名
<select id="queryUserList" resultType="com.example.demo.UserInfo">
select delete_flag as deleteFlag from userinfo
</select>
(2)通过resultmap方式(要记得把对象写全,id username等都映射上)
<resultMap id="base" type="com.example.demo.UserInfo">
<result column="delete_flag" property="deleteFlag"></result>
</resultMap>
<select id="queryUserList" resultMap="base">
select * from userinfo
</select>
(3)配置yml文件(驼峰命名)
mybatis:
configuration:
map-underscore-to-camel-case: true
9、返回自增主键
其他不变,和普通插入操作一样,就是在xml地方增加了一些设置
(1)xml地方代码
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into
userinfo
(username,password,age,gender,phone)
values
(#{username},#{password},#{age},#{gender},#{phone})
</insert>