# Mybatis快速开箱
# 引入依赖
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--jdbc的驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
# 配置文件
以yaml文件为例
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://123.60.138.52:3306/user
username: adaker
password: 123456
# 实体类
Java类中的属性要和mysql中的字段一样 建议使用lombok
假设数据库是:
|id |image |box1
|-------|-----------|--------|
|1 |xxxxx |xxxxxx
@Data
public class Image {
int id;
String image;
String box1;
}
# Mapper层 {#Mapper层}
# 注解式
- @Mapper注解代表这个是一个
- @Select 查询语句
- @Insert 插入语句
- @Update 更新语句
- @Delete 删除语句
/*
create table Image (
id int primary key default 0 auto_increment,
image varchar(100),
box1 varchar(100)
);
*/
@Mapper
public interface ImageMapper {
@Select("select * from 'Image' where id = ${id}")
Image getImage(int id);
@Insert("insert into 'Image' value (null,'1','1')")
void addImage(String image, String box1);
@Update("update 'Image' set image = ${newImage} where id = &{id} ")
void updateImage(int id,String newImage);
@Delete("delete from 'Image' where id = ${id}")
int deleteImage(int id);
}
# 配置文件式
# 修改application配置 {#修改application配置}
mybatis:
# 指定主配置文件路径
config-location: classpath:/mybatis/mybatis-config.xml
# 指定映射文件路径
mapper-locations: classpath:/mybatis/mapper/*.xml
# classpath -> src/main/resources
# 添加MyBatis配置文件 {#添加Mybatis配置文件}
- mybatis-config.xml
- mapper文件
全局配置文件
<?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>
</configuration>
mapper文件
<?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.testpaperrgb.Mapper.ImageMapper">
<select id="getImage" resultType="com.example.testpaperrgb.Entity.Image">
SELECT * FROM Image WHERE id = #{id}
</select>
</mapper>
# 编写接口 {#编写接口}
@Mapper
public interface ImageMapper {
// @Select("select * from Image")
Image getImage(int id);
}
# 配置文件知识点
# yaml文件配置
用于配置Mybatis的额外功能
mybatis:
configuration:
map-underscore-to-camel-case: true # 开启下划线转驼峰
config-location: classpath:/mybatis-config.xml # 全局配置路径
mapper-locations: classpath:/mapper/*.xml # Mapper文件路径
# configuration文件配置
用于配置Mybatis的全局文件 有顺序要求
- typealiases
- package 设置实体类的根路径
- mappers
- resource 设置xml根路径
- package 设置Mapper类的根路径
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.example.testpaperrgb.Entity"/>
</typeAliases>
<mappers>
<package name="com.example.testpaperrgb.Mapper"/>
</mappers>
</configuration>
# mapper文件配置
用于配置Mapper文件如何映射
- select
- insert
- update
- delete
- resultMap
- result 适合用于Java属性和MySQL字段一致的情况
<?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="ImageMapper">
<resultMap id="imageMap" type="Image">
<result property="id" column="id"/>
<result property="image" column="image"/>
<result property="box1" column="box1" />
</resultMap>
<select id="getImage" resultType="Image">
SELECT * FROM Image WHERE id = #{id}
</select>
</mapper>