springboot + mybatis 增删改查和动态查询

这是我参与更文挑战的第21天,活动详情查看: 更文挑战

MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

  1. 依赖
1
2
3
4
5
xml复制代码<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

其他依赖,web,lombok,mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
xml复制代码<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
  1. 数据库建表如下,配置连接
    id设为自增
    在这里插入图片描述

配置文件properties

1
2
3
4
ini复制代码#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=

3.创建mvc三层(domain和dao,service,ctrl)

domain

1
2
3
4
5
6
7
8
9
java复制代码@Data
public class Test {
private int id;
private String name;
private int age;
//格式化查询日期
@JsonFormat(pattern = "yyyy-MM-dd")
private Date workDate;
}

dao,增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
java复制代码//增加方法,传入的是对象 , 也可以使用@Param接收
@Insert("insert into mybatis (name,age,work_date) values(#{name},#{age},#{workDate})")
void insert(Test test);
// @Insert("insert into mybatis (name,age,work_date) values(#{test.name},#{test.age},#{test.workDate})")
// void insert(@Param("test") Test test);

//删除方法,通过id删除
@Delete("delete from mybatis where id = #{id}")
void delete(int id);

//修改方法,通过id修改
@Update("update mybatis set age = #{age} where id =#{id}")
void update(int id,int age);

//查询方法,所有查询
@Select("select * from mybatis")
List<Test> listTest();

4.动态查询详解
1)传多个参数,参数使用包装类型Integer传递

1
2
3
4
5
6
7
8
9
swift复制代码@Select("<script>select * from mybatis m "
+" where 1 = 1"
+"<if test='name!=null and name !=\"\"'>"
+"and m.name = #{name,jdbcType=VARCHAR}"
+"</if>"
+"<if test='age!=null and age !=\"\"'>"
+"and m.age = #{age,jdbcType=INTEGER}"
+"</if> </script>")
List<Test> listTestQuery(String name,Integer age);

2)传对象,和新增时传对象一样

1
2
3
4
5
6
7
8
9
10
swift复制代码@Select("<script>select * from mybatis m "
+" where 1 = 1"
+"<if test='name!=null and name !=\"\"'>"
+"and m.name = #{name,jdbcType=VARCHAR}"
+"</if>"
+"<if test='age!=null and age !=\"\"'>"
+"and m.age = #{age,jdbcType=INTEGER}"
+"</if> "
+"</script>")
List<Test> listTestQuery2(Test test);

5.分页
1)sql分页
传入当前页和分页大小,调用时传入的currIndex-1

1
2
java复制代码@Select("select * from mybatis limit #{currIndex},#{pageSize}")
List<Test> listTest(int currIndex,int pageSize);

2)数组分页,查询所有返回部分

1
2
3
4
5
java复制代码    @RequestMapping("/findall")
public List<Test> findall(int currIndex,int pageSize) {
List<Test> tests = testDao1.listTest();
return tests.subList(currIndex-1,pageSize);
}

6.其他:
接收对象时注意使用@RequestBody 接收
mybatis配置

1
2
3
ini复制代码#mybatis 驼峰映射 、显示sql(com.mybatis.demo.dao 需要显示的包)
mybatis.configuration.map-underscore-to-camel-case=true
logging.level.com.mybatis.demo.dao=debug

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%