整合
引入依赖
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
配置文件增加mybatis配置
- 设置数据源相关配置 地址,端口,库,用户名,密码等
- 设置mybatis相关配置
# 数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://118.89.196.179:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#mybatis配置
# 扫描的mapper.xml文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
# 开启驼峰转下划线
mybatis.configuration.map-underscore-to-camel-case=true
# 打印sql语句
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 开启包别名 自动把全类名映射为别名 Student -> student
mybatis.type-aliases-package=com.example.mybatis.entity
定义实体类
- 实体类就是普通的类,不用像JPA那样标注一些数据库相关的注解
- 如果使用mybatisplus 是需要在类上标注注解
编写对应的mapper接口
- 然后利用idea的mybatisx插件自动生成对应的xml文件,如果没有插件自己在对应的位置写也可以
//mapper接口的示例
public interface UserMapper {
User getUserById(@Param("id") Integer id);
}
<?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字段对应的是mapper接口的全类名-->
<mapper namespace="com.example.demo.mappers.UserMapper">
<select id="getUserById" resultType="com.example.demo.beans.User">
select * from test.user where id = #{id}
</select>
</mapper>
在启动类上标注@mapperScan注解
- 在springBoot的主程序上标注@mapperScan注解来告知spring自动扫描mapper接口
@MapperScan(basePackages = "com.example.demo.mappers")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
动态sql
标签
if
- <if test="表达式"> SQL语句</if>
<if test="student.name != null and student.name != ''">
name = #{student.name},
</if>
choose-when-otherwise
- 类似于switch的东西,只会选择一个满足条件的语句
<choose>
<when test="name != null and name != ''">
name like concat('%',#{name},'%')
</when>
<when test="address != null and address != ''">
address like concat('%',#{address},'%')
</when>
<otherwise>
age > 0
</otherwise>
</choose>
foreach
- 对集合进行遍历,例如批量插入
- collection 指定需要遍历的集合
- item 当前遍历的元素
- separator 分隔符
<insert id="addStudentList">
insert into student(name,sex,address,age,birth,height,weight)
values
<foreach collection="students" item="student" separator=",">
(#{student.name},#{student.sex},#{student.address},#{student.age},#{student.birth},#{student.height},#{student.weight})
</foreach>
</insert>
trim
- 用于去除前后多余的 and,逗号,之类的东西
- prefix、prefixOverrides、suffix、suffixOverrides属性
- prefix 在语句前添加值
- prefixOverrides 去除语句前的前缀
- suffix 在语句后添加后缀
- suffixOverrides 去除语句后的后缀
set
- 用于update语句时 自动自动生成set且去除前后多余的逗号
- 如果没有语句不会生成set
where
- 自动生成where 且 去除开头多余的and或者or
- 如果里面没有任何条件语句,则不会生成where
SELECT
属性
- id 对应的mapper中的方法名 必须
- resultType 返回值类型,值是类型的全类名 必须
- parameterType 参数类型 可忽略 mybatis会自动推断
- resultMap 复杂映射和resultType二选一
- useCache 是否使用二级缓存 默认 false
- timeout 查询超时时间
- statementType 语句类型
- STATEMENT 简单语句 无参数
- PREPARED 预编译语句 默认值
- CALLABLE 存储过程
- databaseId 多数据库支持 同一个id 不同的数据库根据databaseId执行不同的sql
- databaseId="oracle"
- databaseId="mysql"
- 还有一堆其他的 应该用不到
<select id="getStudentsBynNameOrAddress" resultType="com.cpz.mybatistest.entity.Student" >
select * from student
<where>
<choose>
<when test="name != null and name != ''">
name like concat('%',#{name},'%')
</when>
<when test="address != null and address != ''">
address like concat('%',#{address},'%')
</when>
<otherwise>
age > 0
</otherwise>
</choose>
</where>
</select>
UPDATE
属性
- 除了和SELECT中重复的外
- keyColumn 数据库主键列名
- keyProperty 主键属性名(用于获取自增ID)
- useGeneratedKeys 是否使用自增主键 默认值false 一般是insert语句中使用
<update id="updateStudent" keyColumn="id" keyProperty="id" parameterType="com.cpz.mybatistest.entity.Student">
update student
<set>
<if test="student.name != null and student.name != ''">
name = #{student.name},
</if>
<if test="student.sex != null and student.sex != ''">
sex = #{student.sex},
</if>
<if test="student.address != null and student.address != ''">
address = #{student.address},
</if>
<if test="student.age != null and student.age > 0">
age = #{student.age},
</if>
<if test="student.birth != null">
birth = #{student.birth},
</if>
<if test="student.height != null and student.height > 0">
height = #{student.height},
</if>
<if test="student.weight != null and student.weight > 0">
weight = #{student.weight},
</if>
</set>
<where>
id = #{student.id}
</where>
</update>
DELETE
INSERT
属性
- keyColumn 数据库主键列名
- keyProperty 主键属性名(用于获取自增ID)
- useGeneratedKeys 是否使用自增主键 默认值false
<insert id="addStudent" parameterType="com.cpz.mybatistest.entity.Student" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into student(name,sex,address,age,birth,height,weight) values(#{name},#{sex},#{address},#{age},#{birth},#{height},#{weight})
</insert>
复杂结果映射
缓存