一、安装mybatisx插件
在postman删除数据
二、代码解释
1.控制器类代码
package com11.controller;
import com11.entity.User;
import com11.mapper.UserMapper;
import com11.servic.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
("/user")//统一给接口加前缀
public class UserController {
//注入其他类的注解
private UserMapper userMapper;
private UserService userService;
//新增接口、插入请求、返回数据库更改的条数 INtegrt类型、插入方法叫save()、insert()方法返回一个int值
//(@RequestBody User user)在前台传入jsion数据时,它可以把jsion数据映射成user对象,(也就是把前台的转换成java)用postmain验证。
// public Integer save(@RequestBody User user){
// return userMapper.insert(user);//把(user),传到inster中
// }
public Integer save( User user){
return userService.save(user);//新增或者更新
}
//返回类型为List<User> 查询
public List<User> index() {
List<User> all=userMapper.findAll();
return all;//用userMapper方法返回user表里面的数据
}
("/{id}")//@PathVariable表示url的注解,{id}和下面的id一致,一一对应
public Integer delete( Integer id){
return userMapper.deleteById(id);
}
}
2.数据Java代码
里面的参数一定要前后一致,否则利用postman测验时会出现405错误
package com11.entity;
import lombok.Data;
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private String photo;
private String address;
}
3.接口增删改
package com11.mapper;
import com11.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
//是接口,没有被Com11ht所管理
public interface UserMapper {
//mybasit提供的注解,(被谁管理)
("SELECT * FROM sys_user")
List<User> findAll();
//在写SQL语句之前在数据库中写sql,验证SQL语句起作用、验证完后、再写mybatis里的SQL语句
("insert into sys_user(username,password,nickname,email,photo,address) " +
"VALUES(#{username},#{password},#{nickname},#{email},#{photo},#{address})")
int insert(User user);//整个user对象是从外面传来,UserController中的userMapper.insert(user)接口传来
int update(User user);
("delete from sys_user where id = #{id}")
Integer deleteById(("id") Integer id);//传入@param参数,让框架知道参数叫什么名字
// @Update("update sys_user set username=#{username},password=#{password},nickname=#{nickname},email=#{email}," +
// "photo=#{photo},address=#{address} where id=#{id}")
// int update(User user);
}
3.1 动态更改数值
在这之前要安装mybatis插件
mapper namespace=“com11.mapper.UserMapper”
表示UserMapper所在的路径
set、为插件的语法
这种方式实现了更改一个人所有信息的指定信息
比如:如图。就只会改变第5个人的名字和地址,其他信息不变
<?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="com11.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="photo != null">
photo = #{
photo},
</if>
<if test="address != null">
address = #{
address}
</if>
</set>
<where>
id = #{
id}
</where>
</update>
</mapper>
3.2 application.properties的编写
1.以下表示在idea中显示数据库中的更改日志,如下图
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2. 以下可以解决500问题
mybatis.mapper-locations=classpath:mapper/*.xml
#扫描所有mybatis的xml文件
server.port=9090
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dzz?serverTimezone=GMT%2b8
spring.datasource.username=root
spring.datasource.password=qazwsx1120
mybatis.mapper-locations=classpath:mapper/*.xml
#扫描所有mybatis的xml文件
#以下表示在idea中显示数据库中的更改日志,如下图
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.判断新增和更改的数值
package com11.servic;
import com11.entity.User;
import com11.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//加注解Servic表示把整个类注入com11容器里面,
public class UserService {
//利用@Autowired引入UserMapper,通过UserMapper实现数据库的新增和更新
private UserMapper userMapper;
//怎样实现新增和更新,在方法里传入一个user对象
public int save(User user) {
//如果user没有id,代表是新增,然后利用 userMapper.insert实现新增
if (user.getId() == null) {
return userMapper.insert(user);
} else {
//否则为更新
return userMapper.update(user);
}
}
}
5 主类
package com11;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class Com11htApplication {
public static void main(String[] args) {
SpringApplication.run(Com11htApplication.class, args);}
}
三、405问题和500问题
405不允许访问
1.检查java各个包的数据库的字段变量 和 数据库里的 是否一致
2.在控制器类要加@PostMapping
500不允许访问
1.对application.properties进行编写,见3.2
为了书写方面和更为清晰,把application.properties后缀名改为yml,即application.yml
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