springboot+mybatis实现数据增删改查
上一篇讲到使用idea创建springboot项目 那么下面我们来讲讲使用mybatis实现增删改查(curd)吧。
1、数据库创建
2、pom.xml文件 加入依赖mysql 等等
<!--mysql依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3、在配置文件(application.yml)中加入数据库的配置 注意用户名、密码、库名、包名路径改成自己的
server:
port: 8111
servlet:
context-path: /
spring:
datasource:
username: root
password: '*******'
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/lzw?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
mybatis:
mapper-locations: classpath:./mapper/*Mapper.xml
type-aliases-package: com.example.bys.entity
4、在src文件夹下新建UserEntity类 与数据库的字段对应
package com.example.bys.entity;
public class UserEntity {
private int userid;
private String username;
private String password;
private String email;
public UserEntity(String username, String password, String email) {
this.userid = userid;
this.username = username;
this.password = password;
this.email = email;
}
public UserEntity() {
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5、新建mapper文件夹并新建UserMapper接口
package com.example.bys.mapper;
import com.example.bys.entity.UserEntity;
import org.apache.catalina.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
/**
*
* @return 查询所有用户
*/
List<UserEntity> selectAllUser();
/**
*
* @param id 用户id
* @return 查询单个用户
*/
UserEntity selectOneUser(int id);
/**
*
* @param userEntity 插入用户
* @return 插入的用户
*/
void insertUser(UserEntity userEntity);
/**
*
* @param id 删除用户的id
*/
void deleteUser(int id);
/**
*
* @param userEntity 更新用户的数据
*/
void updateUser(UserEntity userEntity);
}
6、新建service文件夹并新建UserServiceImpl类 去实现上面的mapper接口
package com.example.bys.service;
import com.example.bys.entity.UserEntity;
import com.example.bys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl {
@Autowired
UserMapper userMapper;
public List<UserEntity> selectAllUser() {
return userMapper.selectAllUser();
}
public UserEntity selectOneUser(int id) {
return userMapper.selectOneUser(id);
}
public void insertUser(UserEntity userEntity){
userMapper.insertUser(userEntity);
}
public void deleteUser(int id){
userMapper.deleteUser(id);
}
public void updateUser(UserEntity userEntity){
userMapper.updateUser(userEntity);
}
}
7、在controller文件夹下新建UserController类 在里面写对应的逻辑
package com.example.bys.controller;
import com.example.bys.entity.UserEntity;
import com.example.bys.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@RequestMapping("/selectAllUser")
public List<UserEntity> selectAllUser(){
return userService.selectAllUser();
}
@RequestMapping("/selectOneUser")
public UserEntity selectOneUser(int id){
return userService.selectOneUser(id);
}
@PostMapping("/insertUser")
public void insertUser(@RequestBody UserEntity userEntity){
userService.insertUser(userEntity);
System.out.println("insert成功");
}
@RequestMapping("/deleteUser")
public void deleteUser(int id){
userService.deleteUser(id);
System.out.println("delete成功");
}
@PostMapping("/updateUser")
public void updateUser(@RequestBody UserEntity userEntity){
userService.updateUser(userEntity);
System.out.println("update成功");
}
}
8、在resource文件夹下新建mapper文件夹并新建映射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.example.bys.mapper.UserMapper">
<select id="selectAllUser" resultType="com.example.bys.entity.UserEntity">
select * from user
</select>
<select id="selectOneUser" resultType="com.example.bys.entity.UserEntity">
select * from user where userid = #{id}
</select>
<insert id="insertUser" parameterType="com.example.bys.entity.UserEntity">
insert into user (username,password,email) values (#{username},#{password},#{email})
</insert>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where userid = #{id}
</delete>
<update id="updateUser" parameterType="com.example.bys.entity.UserEntity">
update user set username=#{username},password=#{password},email=#{email} where userid=#{userid}
</update>
</mapper>
9、最后在启动类中加上一句扫描mapper包的位置 然后直接run起来就好了
@MapperScan("com.example.bys.mapper") //扫描mapper包
10、好啦到这里已经基本完成啦。。。下面用postman测试工具来测试(蛮好用的工具大家可以下载一个来玩一下)
测试查找全部用户 注意路径和Usercontroller里面的要一致
测试查找单个用户
测试新增用户
测试删除用户
测试修改用户
工程代码已上传至Github 大家自行下载吧。。。。。。。
https://github.com/Goodnamelzw/springboot-mybatis