1、配置环境
(1)引入 pom 依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
这些基本都可以通过代码生成器生成,主要介绍一些注意点:
2、实体类
实体类:
- 默认表名 = 类名 字段名 = 属性名
- 使用
@Table(name = "表名")
进行表名指定 - 使用
@Column(name = "字段名")
进行字段指定 - 使用
@Transient
不进行字段映射
@Table(name = "tb_user")//表名与类不一致时
public class User1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//自动转换下换线到驼峰命名user_name -> userName
//@Column(name = "user_name")
private String userName;
private Integer age;
@Transient//不进行字段映射
private String info;
}
3、mapper 接口
dao:继承Mapper
接口,不要忘记指定 实体对象
继承 Mapper 后,就继承了 Mapper 的通用 crud 方法
public interface UserMapper extends Mapper<User> {
public List<User> findByUser(User user);
}
4、映射文件
有时候面对复杂的场景,需要自己编写 映射文件,tk-mybatis 同样支持
<?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.xj0927.dao.UserMapper">
<select id="findByUser" resultType="com.xj0927.bean.User1">
SELECT * FROM tb_user
<where>
<if test="name != null">
name like '%${name}%'
</if>
<if test="note != null">
or note like '%${note}%'
</if>
</where>
</select>
</mapper>
5、启动类
此时 @SpringBootApplication 使用 tx mybatis 的构件:tk.mybatis.spring.annotation.MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.xj0927.dao") //扫描接口
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
6、测试
为了更好看见所执行的 sql 语句,可以在配置文件添加如下配置:
(1)添加
@Test
public void insert() {
Brand brand = new Brand();
brand.setName("测试1");
brandMapper.insert(brand);
brandMapper.insertSelective(brand);
}
insert 和 insertSelective 区别:
前提Goods商品表里面有三个字段:id,name,price
1.此时我只设置了一个字段名字:
Goods g = new Goods();
g.setName("手机");
insertSelective(g);
insertSelective执行对应的sql语句的时候,只插入对应的name字段;
(主键是自动添加的,默认插入为空)insert into tb_goods (id,name) value (null,"手机");
注意:此时是没有price什么事的
2、如果使用insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个。
Goods g=new Goods();
g.setName("冰箱");
insert(g)
insert执行对应的sql语句的时候,统一都要添加一遍;
insert into tb_goods (id,name,price) value (null,"冰箱",null);
注意:price也在哦!!
insert 和 insertSelective 插入数据库后,在数据库中的效果是一样的,只是 sql 语句不同
insert 就是把所有值插入,此时数据库中有 default 值,default 值就不起作用了
insertSelective 不会忽略 default 值
(2)删除
@Test
public void insert() {
//1.根据主键值删除元素
brandMapper.deleteByPrimaryKey(23);
//2.传入实体对象删除
Brand brand = new Brand();
brand.setId(23L);
brandMapper.delete(brand);
//3.创建 Example 对象,添加限制条件,再传入对象进行删除
Example example = new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id","23");
brandMapper.deleteByExample(example);
}
(3)修改
@Test
public void insert() {
Brand brand = new Brand();
brand.setId(22l);
brand.setName("测试1");
Example example = new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id","22");
//2.传入实体类+添加example查询条件
brandMapper.updateByExample(brand, example);
brandMapper.updateByExampleSelective(brand,example);
//1.根据传入实体类的id进行修改
brandMapper.updateByPrimaryKey(brand);
brandMapper.updateByPrimaryKeySelective(brand);
}
updateByPrimaryKey 和 updateByPrimaryKeySelective 区别:
updateByPrimaryKey 更新的时候不会对字段进行 null 判断.如果为 null,这个字段就被更新为 null
updateByPrimaryKeySelective 更新的时候会对字段进行 null 判断,如果为 null,就不更新这个字段
updateByExampleSelective传入参数的含义:
xxxMapper.updateByExampleSelective(参数一,参数二);
第一个参数 是要修改的部分值组成的对象,其中有些属性为null则表示该项不修改。
第二个参数 是一个对应的查询条件的类, 通过这个类可以实现 order by 和一部分的where 条件。
(4)查询
@Test
public void insert() {
Brand brand = new Brand();
brand.setId(22l);
brand.setName("测试2");
//1.根据主键进行查询
Brand brand1 = brandMapper.selectByPrimaryKey(1);
//2.根据实体对象查询符合条件的:结果为List
List<Brand> select = brandMapper.select(brand);
//3.查询单个数据:结果为单个对象
Brand brand2 = brandMapper.selectOne(brand);
//4.查询所有
List<Brand> brands = brandMapper.selectAll();
//5.根据(属性条件)查询符合条件的总条数
int i = brandMapper.selectCount(brand);
//6.根据Example对象添加限制条件查询
Example example = new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id","22");
List<Brand> brands1 = brandMapper.selectByExample(example);
}