一、前言
上一篇博文我简单陈述了SpringBoot,并写了入门程序,启动类加载运行,访问成功。
本文主要是结合SpringBoot+Mybatis+MySql来实现数据库的CRUD操作。
二、代码
1、创建SpringBoot项目(或者Maven项目)
2、添加Web、Mysql、Mybatis的依赖
3、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.google</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mybatis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、启动类
package com.google;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
到这里,完整的SpringBoot项目就完成了,接下来,就可以整理MyBatis的东西了。
5、填写配置文件:application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=1111【注】:此处设置了该项目端口,默认为8080
6、实体类:User.java
package com.google.entity;
/**
* Created by 郭子旭 on 2018/4/12
*/
public class User {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
7、接口(映射器类):UserMapper.java
该类主要是用于CRUD操作的,一般有两种方式实现数据库的增删改查。
方式一:注解: @Insert @Delete @Update @Select
方式二:xml配置文件
package com.google.dao;
import com.google.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* Created by 郭子旭 on 2018/4/12
*/
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
public User selectUserById(int id);
@Select("select * from user where name=#{name}")
public List<User> selectUserByName(String name);
@Insert("insert into user(name,address) values(#{name},#{address})")
public void addUser(User user);
@Delete("delete from user where id=#{id}")
public void deleteUser(int id);
@Update("update user set name=#{name},address=#{address}")
public void updateUser(User user);
}
8、控制层:UserController.java
package com.google.controller;
import com.google.dao.UserMapper;
import com.google.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by 郭子旭 on 2018/4/12
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value = "/selectUserById",method = RequestMethod.POST)
public User selectUserById(String id){
User user = userMapper.selectUserById(Integer.parseInt(id));
return user;
}
@RequestMapping(value = "/selectUserByName",method = RequestMethod.POST)
public List<User> selectUserByName(String name){
return userMapper.selectUserByName(name);
}
@RequestMapping(value = "/addUser",method = RequestMethod.POST)
public void addUser(User user){
userMapper.addUser(user);
}
@RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
public void deleteUser(String id){
userMapper.deleteUser(Integer.parseInt(id));
}
@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
public void updateUser(User user){
userMapper.updateUser(user);
}
}
9、测试
这里测试我用了PostMan(一款Http请求模拟工具来测试API接口)发送get/post请求。
post请求后会在mysql数据库中看到相应数据的改变。