注解版
先看一下项目目录
引入项目相关依赖(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<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>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application配置文件
#mysql驱动
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
#驱动路径,关联相关的数据库
spring.datasource.url =jdbc:mysql://localhost:3306/myemployees?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong
#数据库账号
spring.datasource.username = root
#数据库密码
spring.datasource.password = 123
#开启驼峰匹配原则
mybatis.configuration.map-underscore-to-camel-case=true
数据库相关的表,我用的是jobs这张表,表内容如下
项目实体类,这个要根据数据库对应的表来设计
package com.example.demo.entity;
import lombok.Data;
import java.io.Serializable;
/**
* Created by 张靖奇 on 2020/1/31 19:10
*/
@Data//引入lombok插件,可以省略setget方法
public class myemployees implements Serializable {
private static final long serialVersionUID = 8655851615465363473L;
private String jobId;
private String jobTitle;
private int minSalary;
private int maxSalary;
}
mapper层
package com.example.demo.dao;
import com.example.demo.Bean.myemployees;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by 张靖奇 on 2020/1/31 19:15
*/
@Mapper//不加也可以,最好在启动类中用mapperscan扫描
@Repository//如果不加这个注解service层会有警告
public interface myemployeesmapper {
/*
和配置文件中开启驼峰匹配规则作用相同
@Results(
{@Result(column = "job_id",property = "jobId"),
@Result(column = "job_title",property = "jobTitle"),
@Result(column = "min_salary",property = "minSalary"),
@Result(column = "max_salary",property = "maxSalary")
}
)
*/
@Select("select * from jobs where job_id=#{jobId}")
public List<myemployees> getMyemployees ( @Param("jobId") String jobId);
}
service层
package com.example.demo.Service;
/**
* Created by 张靖奇 on 2020/1/31 19:43
*/
import com.example.demo.dao.myemployeesmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service//代表是处理业务逻辑层,必须加
public class myemployees {
@Autowired//自动注入,必须加
myemployeesmapper mapper;
public List<com.example.demo.Bean.myemployees> getMyemployees (String jobId){
return mapper.getMyemployees(jobId);
}
}
controller层
package com.example.demo.controller;
import com.example.demo.Service.myemployees;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by 张靖奇 on 2020/1/31 19:30
*/
@RestController
@RequestMapping
public class controller {
@Autowired//必须要自动注入进去,不然会报500错误
myemployees myemployees;
@GetMapping("/select/{jobId}")
public List<com.example.demo.Bean.myemployees> getMyemployees (@PathVariable("jobId") String jobId){
return myemployees.getMyemployees(jobId);
}
}
启动类
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.dao")//扫描相关的包
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
最后进行测试