数据库在此处不过多讨论,按以下描述新建数据库即可
项目搭建的完整思路如下
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.8.RELEASEversion>
<relativePath/>
parent>
<groupId>com.mybatisgroupId>
<artifactId>MybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>Mybatisname>
<description>Spring Boot Mybatisdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.mybatis.entity;
public class User {
private int id;
private String name;
private String profession;
public User() {
}
public User(String name, String profession) {
this.name = name;
this.profession = profession;
}
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 getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
}
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.mybatis.entity.User;
@Mapper
public interface UserDao {
// 新增
int insert(User user);
// 删除
int delete(int id);
// 修改
int update(User user);
// 查询
List<User> select();
}
package com.mybatis.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mybatis.dao.UserDao;
import com.mybatis.entity.User;
@Service
public class UserService {
@Autowired
private UserDao userDao;
// 新增业务
public int insert(User user) {
return userDao.insert(user);
}
// 删除业务
public int delete(int id) {
return userDao.delete(id);
}
// 修改业务
public int update(User user) {
return userDao.update(user);
}
// 查询业务
public List<User> select(){
return userDao.select();
}
}
package com.mybatis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mybatis.entity.User;
import com.mybatis.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping(value = "insert", method = RequestMethod.POST)
public int insert() {
// 返回新增成功的行数
return userService.insert(new User("钟力", "Java高级工程师"));
}
@ResponseBody
@RequestMapping(value = "delete", method = RequestMethod.DELETE)
public int delete() {
// 返回删除成功的行数
return userService.delete(1);
}
@ResponseBody
@RequestMapping(value = "update", method = RequestMethod.PUT)
public int update() {
User user = new User("钟力", "Java架构师");
user.setId(1);
// 返回修改成功的行数
return userService.update(user);
}
@ResponseBody
@RequestMapping(value = "select", method = RequestMethod.GET)
public List<User> select() {
return userService.select();
}
}
<mapper namespace="com.mybatis.dao.UserDao">
<insert id="insert" parameterType="User">
insert into user (name, profession) values (#{name}, #{profession})
insert>
<delete id="delete">
delete from user where id = #{id}
delete>
<update id="update" parameterType="User">
update user set name = #{name}, profession = #{profession} where id = #{id}
update>
<select id="select" resultType="User">
SELECT * FROM user
select>
mapper>
spring.datasource.url=jdbc:mysql://localhost:3306/local?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=zl0418
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.mybatis.entity
springboot-mybatis