搭建环境
一般springboot整合mybatis多了一个mapper层。
dao层:对应底层的数据库。
mapper:对应数据库的sql操作,操作dao层。
service:写上自己的业务逻辑,操作mapper。
controller:写上路由,操作service。
数据库内容:
pom.xml文件如下:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
<groupId>spring-test</groupId>
<artifactId>mySpring-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
dao层:
package test.dao;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
mapper层:
package test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import test.dao.User;
public interface UserMapper {
@Select("select * from user")
List<User> getAll();
}
service层:
package test.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.dao.User;
import test.mapper.UserMapper;
@Service
public class MyService {
@Autowired
private UserMapper userMapper;
public List<User> findAll(){
return this.userMapper.getAll();
}
}
controller层:
package test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.dao.User;
import test.service.MyService;
@RestController
public class Mycontroller {
@Autowired
private MyService myService;
@RequestMapping("/test")
public List<User> test() {
List<User> findAll = this.myService.findAll();
return findAll;
}
}
启动:
package test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("test.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
访问结果:
@UpdateProvider
@UpdateProvider可以动态的更新sql语句的字段。测试如下:
数据库改成如下:
user模型添加:
package test.dao;
public class User {
private Integer id;
private String name;
private String address;
private Integer num;
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
mapper层新增update方法:
package test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import test.dao.User;
import test.mapper.sql.TestSql;
public interface UserMapper {
@Select("select * from user")
List<User> getAll();
@UpdateProvider(type = TestSql.class , method = "update1")
int update(User user);
}
里面的TestSql的代码如下:
package test.mapper.sql;
import org.apache.ibatis.jdbc.SQL;
import test.dao.User;
public class TestSql {
public String update1(User user) {
return new SQL() {
{
UPDATE("user");
if(user.getName()!=null) {
SET("name=#{name}");
}
if(user.getAddress()!=null) {
SET("address=#{address}");
}
if(user.getNum()!=null) {
SET("num=#{num}");
}
WHERE("id=#{id}");
}
}.toString();
}
}
service新增update方法:
package test.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.dao.User;
import test.mapper.UserMapper;
@Service
public class MyService {
@Autowired
private UserMapper userMapper;
public List<User> findAll(){
return this.userMapper.getAll();
}
public int update(User user) {
return this.userMapper.update(user);
}
}
controller新增上下文:
package test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.dao.User;
import test.service.MyService;
@RestController
public class Mycontroller {
@Autowired
private MyService myService;
@RequestMapping("/test")
public List<User> test() {
List<User> findAll = this.myService.findAll();
return findAll;
}
@RequestMapping("/update")
public int update() {
User user=new User();
user.setId(3123);
user.setNum(5);
user.setAddress("m");
return this.myService.update(user);
}
}
最后常规的App启动类就不多说了。
application.properties修改如下:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
logging.level.root=debug
运行结果:
如果把controller中的user去掉num,代码如下:
package test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.dao.User;
import test.service.MyService;
@RestController
public class Mycontroller {
@Autowired
private MyService myService;
@RequestMapping("/test")
public List<User> test() {
List<User> findAll = this.myService.findAll();
return findAll;
}
@RequestMapping("/update")
public int update() {
User user=new User();
user.setId(3123);
// user.setNum(5);
user.setAddress("XQ");
return this.myService.update(user);
}
}
测试结果如下:
上述测试证明@UpdateProvider可以动态的根据sql来调整更新语句。
项目链接:
https://download.csdn.net/download/qq_37822914/12414473