在pom.xml文件中粘贴下列,加载maven依赖
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.easy</groupId>
<artifactId>EasySpringB</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.6.6</version>
</parent>
<dependencies>
<!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.16</version>
</dependency>
<!--Lombok引入-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.14</version>
</dependency>
</dependencies>
</project>
在application.yml文件中,添加下列代码
#注释
#yml语法
#配置mybatis的数据源DataSource
spring:
datasource:
username:root
password:123456
driver-class-name:com.mysql.cj.jdbc.Driver
url:jdbc:mysql://localhost:3306/easydata
#配置mapper文件地址
mybatis:
mapper-locations:classpath:mapper/*.xml
logging:
level:
com.easy.dao:debug
在com.easy文件夹中创建启动类Easy SpringApplication
package com.easy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EasySpringApplication{
public static void main(String[]args){
SpringApplication.run(EasySpringApplication.class,args);
}
}
创建一个dao包,再包中创建IStaffDao接口
packagecom.easy.dao;
import com.easy.bean.Staff;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IStaffDao{
String getNow();
int addStaff(Staffstaff);
int delStaff(intid)
int editStaff(Staffstaff);
}
创建一个bean包,创建Staff类写出get、set方法
package com.easy.bean;
import java.math.BigDecimal;
public class Staff{
private int id;
private String code;
private String name;
private BigDecimal salary;
private String username;
private String userpass;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getCode(){
return code;
}
public void setCode(String code){
this.code=code;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public BigDecimalgetSalary(){
return salary;
}
public void setSalary(BigDecimal salary){
this.salary=salary;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
public String getUserpass(){
return userpass;
}
public void setUserpass(String userpass){
this.userpass=userpass;
}
}
创建一个controller包,EasyController类
packagecom.easy.controller;
importcom.easy.bean.Department;
importcom.easy.bean.Staff;
importcom.easy.dao.IDepartmentDao;
importcom.easy.dao.IStaffDao;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.*;
importjava.math.BigDecimal;
@RestController
publicclassEasyController{
@Autowired
IStaffDaodao;
@GetMapping("now")
publicStringnow(){
returndao.getNow();
}
@PostMapping("staff")
publicStringaddStaff(Staffstaff){
dao.addStaff(staff);
return"successstaff";
}
//删除
@DeleteMapping("staff/{id}")
publicStringdelStaff(@PathVariableintid){
dao.delStaff(id);
return"successdelete";
}
@PutMapping("staff")
publicStringeditStaff(Staffstaff){
dao.editStaff(staff);
return"successedit";
}
}
Staff_Mapper.xml中的代码
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC"-//mybatis.org//DTDMapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.easy.dao.IDepartmentDao">
<!--在这里写SQL语句-->
<selectid="getNow"resultType="String">
selectnow()
</select>
<insertid="addStaff">
insertintostaff(code,name,salary,username,userpass)
value(#{code},#{name},#{salary},#{username},#{userpass})
</insert>
<deleteid="delStaff">
deletefromstaffwhereid=#{id}
</delete>
<updateid="editStaff">
updatestaffsetname=#{name},salary=#{salary},
username=#{username},userpass=#{userpass}
whereid=#{id}
</update>
</mapper>
最后运行启动类 EasySpringApplication类