本文章主要是结合SpringBoot+MyBatis+MySQL来实现数据库的CRUD操作(增删查改),Maven项目工程中主要包括以下6个文件:
注:其中src/main/resources在创建Maven Project时并没有,需要右键->New->Source Folder来添加
首先在MySQL中的Springdb模式下创建user表,包括4个属性:
id:int ,设置为[主键][6]且[自增][6]
userName:varchar
userAge:int
userAddress:varchar
<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.0modelVersion>
<groupId>com.spring.mybatisgroupId>
<artifactId>springMybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>springMybatisname>
<url>http://maven.apache.orgurl>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.1.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.21version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<skip>trueskip>
configuration>
plugin>
plugins>
build>
project>
package com.spring.mybatis.springMybatis.entity;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class User {
private int id;
private String userName;
private int userAge;
private String userAddress;
}
以上也许有人会奇怪,@Setter和@Getter两个注解是什么意思。顾名思义应该会想到这是getter和setter方法。那么为什么这样做呢?
因为每个实体类都应该包含getter和setter方法才有意义。但是getter和setter方法的代码过于冗余,属于模板类型代码,影响代码阅读。@Setter和@Getter两个注解是属于[lombok][6]的,在pom.xml文件中添加依赖引入lombok:
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.14.4version>
dependency>
这样在实体类中添加@Setter和@Getter注解,就可以省略getter和setter方法而达到同样的效果
如果你没有用过lombok可以上网查找相关资料学习一下,这个是很方便的。也十分易于学习理解
如果你不想使用lombok的话,直接在User.java中,右键->Source->Generate Getters and Setters来生产getter和setter方法。
这个文件是放在src/main/resources目录下,当项目启动时,该文件会自动加载,application.properties内容如下:
spring.datasource.url=jdbc:mysql://localhost:3306/springdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
该类主要是用于CRUD操作的,一般有两种方式实现与数据库实现CRUD:
本文中选择的是基于注解的
package com.spring.mybatis.springMybatis.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.spring.mybatis.springMybatis.entity.User;
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User selectUserById(int id);
@Select("select * from user where userName = #{userName}")
public List selectUserByName(String userName);
@Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
public void addUser(User user);
@Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
public void updateUser(User user);
@Delete("delete from user where id=#{id}")
public void deleteUser(int id);
}
package com.spring.mybatis.springMybatis.controller;
import java.util.List;
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 com.spring.mybatis.springMybatis.dao.UserMapper;
import com.spring.mybatis.springMybatis.entity.User;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
public User selectUserById(String id){
User user = userMapper.selectUserById(Integer.parseInt(id));
return user;
}
@RequestMapping(value={"/selectUserByName"}, method=RequestMethod.GET)
public List selectUserByName(String userName){
return userMapper.selectUserByName(userName);
}
@RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
public void addUser(User user){
userMapper.addUser(user);
}
@RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
public void updateUser(User user){
userMapper.updateUser(user);
}
@RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
public void deleteUser(String id){
userMapper.deleteUser(Integer.parseInt(id));
}
}
该类是作为请求入口的,通过get/post请求来实现CRUD。
本文采用google浏览器的postman插件来发送get/post请求。
post请求后会在mysql中看到数据的改变。