赞
踩
application.yml
server: port: 8081 spring: mvc: path match: matching-strategy: ant_path_matcher datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://localhost:3306/management?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8 mybatis: type-aliases-package: com.xqh.entity mapper-locations: classpath:/mapper/*.xml
UserMapper.java
package com.xqh.mapper; import com.xqh.entity.User; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { //查出所有数据 List<User>findAll(); int insert(User user); int delete(@Param("id") Integer id); int update(User user); }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xqh.mapper.UserMapper"> <insert id="insert" parameterType="User"> INSERT into user(username,password,nickname,email,phone,address) VALUES (#{username},#{password},#{nickname},#{email},#{phone},#{address}) </insert> <delete id="delete" parameterType="int"> delete from user where id=#{id} </delete> <!-- 要写动态sql,因为很多情况是只修改其中几项,而不是全部修改。如果传过来的参数不为空才修改,为空就保持原样--> <update id="update" parameterType="User"> update 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> <select id="findAll" resultType="User"> select * from user </select> </mapper>
UserService.java
package com.xqh.service; import com.xqh.entity.User; import com.xqh.mapper.UserMapper; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User>findAll(){ return userMapper.findAll(); }; public int delete(@Param("id") int id){ return userMapper.delete(id); }; public int save(User user){ if (user.getId()==null){ //user没有id,则表示是新增,否则为更新 return userMapper.insert(user); }else { return userMapper.update(user); } } }
UserController.java
package com.xqh.controller; import com.xqh.entity.User; import com.xqh.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController //返回json @RequestMapping("/user") //加前缀,这下面的所有接口都要加/user前缀 public class UserController { @Autowired private UserService userService; //查询所有 @GetMapping public List<User> index(){ return userService.findAll(); } //插入和修改操作 , 需要外界发送数据,用post @PostMapping public Integer save(@RequestBody User user){ return userService.save(user); } //删除 @DeleteMapping("/{id}") public Integer delete(@PathVariable Integer id){ return userService.delete(id); } }
运行主程序,打开swagger页面 localhost:8081/swagger-ui.html ,测试接口,增删改查,成功操作数据库即完成
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。