增删改查代码编写
UserController.java
package com.zj.demo.controller;
import com.zj.demo.entity.User;
import com.zj.demo.mapper.UserMapper;
import com.zj.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
// 新增和修改
@PostMapping
public Integer save(@RequestBody User user) {
// 新增或者更新
return userService.save(user);
}
// 查询所有数据
@GetMapping
public List<User> index() {
List<User> all = userMapper.findAll();
return all;
}
@DeleteMapping("/{id}")
public Integer delete(@PathVariable Integer id) {
return userMapper.deleteById(id);
}
}
UserMapper.java
package com.zj.demo.mapper;
import com.zj.demo.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * from sys_user")
List<User> findAll();
@Insert("INSERT into sys_user(username, password,nickname,email,phone,address) VALUES (#{username}, #{password}," +
" #{nickname}, #{email},#{phone}, #{address})")
int insert(User user);
int update(User user);
@Delete("delete from sys_user where id = #{id}")
Integer deleteById(@Param("id") Integer id);
}
UserService.java
package com.zj.demo.service;
import com.zj.demo.entity.User;
import com.zj.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int save(User user) {
// user没有id,则表示是新增
// 否则为更新
if (user.getId() == null) {
return userMapper.insert(user);
} else {
return userMapper.update(user);
}
}
}
postman测试后台接口
下载地址:
https://app.getpostman.com/app/download/win64
postman测试接口
动态SQL编写:
1、update如果写成下面这个方式,用户提交的每个请求都会反映到数据库,不太好
如果有字段不需要更新就很麻烦
所以需要用动态SQL【相关代码见下】
记得IDEA安装MybatisX插件
User.xml
<?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.qingge.springboot.mapper.UserMapper">
<update id="update">
update sys_user
<set>
<if test="username != null">
username = #{username},
</if>
<!-- <if test="password != null">-->
<!-- password = #{password}-->
<!-- </if>-->
<if test="nickname != null">
nickname = #{nickname},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="phone != null">
phone = #{phone},
</if>
<if test="address != null">
address = #{address}
</if>
</set>
<where>
id = #{id}
</where>
</update>
</mapper>
application.yml
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/qing?serverTimezone=GMT%2b8
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的xml文件
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
一些补充:
MyBatis动态SQL详解
MyBatis大部分参数解释以及示例
MyBatis学习:动态SQL中<foreach>标签的使用
MyBatis映射文件中resultType和parameterType是干啥的?
parameterType用来指定输入参数的数据类型, resultTyp用来指定输出结果的数据类型。