本篇主要是讲解如何使用Springboot+mybatis对mysql数据库进行增删改查操作,下图为本次项目的目录结构:
一、首先,创建maven项目,并添加依赖。下方为pom.xml文件内容。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.course.code</groupId>
<artifactId>Test190313</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
二、本次项目需要涉及到mysql数据库操作,因此先新建数据库、新建表,并添加表字段,如下图course数据库中新建了user表
下图为user表字段:
三、编写springboot启动类
package com.user;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(value = "com.user.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
四、在com.user.entity表下新建User实体类
package com.user.entity;
import lombok.Data;
@Data
public class User {
private int id;
private String userName;
private String password;
private String age;
private String sex;
private String permission;
private String isDelete;
}
该实体类中使用了lombok工具,在项目中使用Lombok可以减少很多重复代码的书写,减少代码冗余。比如说getter/setter/toString等方法的编写。
五、编写UserMapper接口
package com.user.dao;
import com.user.entity.User;
public interface UserMapper {
public User select(int id);
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(int id);
}
六、编写UserController类
package com.user.controller;
import com.user.dao.UserMapper;
import com.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping(value = "/select",method = RequestMethod.GET)
public User selectById(int id){
User user;
user = userMapper.select(id);
return user;
}
@RequestMapping(value = "/addUserInfo",method = RequestMethod.POST)
public String add(@RequestBody User user) {
userMapper.addUser(user);
return "新增成功";
}
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(@RequestBody User user){
userMapper.updateUser(user);
return "修改成功";
}
@RequestMapping(value = "/delete",method = RequestMethod.POST)
public String delete(int id){
userMapper.deleteUser(id);
return "删除成功";
}
}
七、编写application.properties文件
server.port=9797
spring.datasource.url=jdbc:mysql://localhost:3306/course
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations= classpath:mapper/*.xml
八、编写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.user.dao.UserMapper">
<select id="select" resultType="com.user.entity.User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="com.user.entity.User">
insert into user(id,userName,password,age,sex,permission,isDelete)
values(#{id},#{userName},#{password},#{age},#{sex},#{permission},#{isDelete})
</insert>
<update id="updateUser">
update user set userName=#{userName},age=#{age} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id =#{id}
</delete>
</mapper>
至此,Springboot+mybatis整合完成。