一、前言
上一篇博文我簡單陳述了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數據庫中看到相應數據的改變。