SpringBoot mybatis方式实现增删改查
首先看一下目录结构
application.yml文件
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/myDataBase
username: root
password: 1qaz2wsx
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.winter.model
Model 代码如下
package com.winter.model;
public class User {
private Integer userId;
private String userName;
private String userAge;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge == null ? null : userAge.trim();
}
}
ViewController 代码都加了注释
package com.winter.controller;
import com.winter.model.User;
import com.winter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
/*Get方式方便web测试*/
/*增加用户*/
// http://localhost:8080/user/addUser/?userName=Alex&userAge=20
@ResponseBody
@RequestMapping(value = "/addUser", produces = {"application/json;charset=UTF-8"})
public int addUser(User user){
return userService.addUser(user);
}
/*删除用户*/
// http://localhost:8080/user/deleteUser/?userId=2
@ResponseBody
@RequestMapping(value = "/deleteUser",produces = {"application/json;charset=UTF-8"})
public int deleteUser(User user){
return userService.deleteUser(user);
}
/*修改用户*/
// http://localhost:8080/user/updateUser/?userId=3&userName=刘牧师&userAge=50
@ResponseBody
@RequestMapping(value = "/updateUser",produces = {"application/json;charset=UTF-8"})
public int updateUser(User user){
return userService.updateUser(user);
}
/*根据UserId查找用户*/
// http://localhost:8080/user/getUserById/?userId=3
@ResponseBody
@RequestMapping(value = "/getUserById", produces = {"application/json;charset=UTF-8"})
public User getUserById(User user){
return userService.getUserById(user);
}
/*查询所有用户*/
// http://localhost:8080/user/getAllUsers
@ResponseBody
@RequestMapping(value = "/getAllUsers", produces = {"application/json;charset=UTF-8"})
public Object findAllUser(){
return userService.findAllUser();
}
}
UserService
package com.winter.service;
import com.winter.model.User;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUser(User user);
int updateUser(User user);
User getUserById(User user);
List<User> findAllUser();
}
UserServiceImpI
package com.winter.service;
import com.winter.mapper.UserMapper;
import com.winter.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int deleteUser(User user){
return userMapper.deleteUser(user);
}
@Override
public int updateUser(User user){
return userMapper.updateUser(user);
}
@Override
public User getUserById(User user){
return userMapper.getUserById(user);
}
@Override
public List<User> findAllUser() {
return userMapper.selectAllUser();
}
}
UserMapper
package com.winter.mapper;
import com.winter.model.User;
import java.util.List;
public interface UserMapper {
int addUser(User user);
int deleteUser(User user);
int updateUser(User user);
User getUserById(User user);
List<User> selectAllUser();
}
UserMapper.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.winter.mapper.UserMapper" >
<insert id="addUser" parameterType ="com.winter.model.User">
insert into tb_user(user_name,user_age)
values (#{userName},#{userAge})
</insert>
<delete id="deleteUser" parameterType ="com.winter.model.User">
delete from tb_user where user_id = #{userId}
</delete>
<update id="updateUser" parameterType ="com.winter.model.User">
update tb_user set user_name =#{userName},user_age = #{userAge} where user_id= #{userId}
</update>
<select id="getUserById" parameterType ="com.winter.model.User" resultType="com.winter.model.User">
select user_id userId,user_name userName,user_age userAge
from tb_user where user_id= #{userId}
</select>
<select id="selectAllUser" resultType="com.winter.model.User">
select user_id userId,user_name userName,user_age userAge
from tb_user
</select>
</mapper>