搭建一个spring boot + Mybatis + MVC的环境
此步骤省略
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
在resources
目录下创建application.yml
文件,设置tomcat端口,设置数据库连接信息。
server:
port: 8989
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # 这是mysql8.0.13版本的驱动。
url: jdbc:mysql://localhost:3306/rabbit?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: root
password: xxxxxxxxx
type: com.alibaba.druid.pool.DruidDataSource # 数据源类型:Druid
initialSize: 5
minIdle: 5
maxActive: 20
在启动类DataxApplication.java
里,添加扫描mapper的注解@MapperScan("top.yxdz.**.mapper")
。
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("top.yxdz.**.mapper")
@EnableSwagger2
public class DataxApplication {
public static void main(String[] args) {
SpringApplication.run(DataxApplication.class, args);
}
}
在controller目录下,添加类JobController.java
,调用service层的getJobById(String id)
方法。
@RestController
@RequestMapping("/api/job")
public class JobController {
@Autowired
IJobService iJobService;
@GetMapping("/{id}/info")
@ApiOperation(value = "通过作业id获取作业信息", response = AjaxResult.class)
public AjaxResult getJobById(@PathVariable("id") String id){
return iJobService.getJobById(id);
}
}
在service目录下,添加接口IJobService.java
,在该目录下的impl
添加该接口的实现类JobServiceImpl.java
IJobService.java
该接口定义一个方法getJobById(String id)
,实现通过id从数据库查找对应数据。
public interface IJobService {
/**
* 通过id获取作业内容
* @param id
* @return JobEntity
*/
AjaxResult getJobById(String id);
}
JobServiceImpl.java
该类是接口IJobService.java
的实现类。
@Service("JobServiceImpl")
public class JobServiceImpl implements IJobService {
private static Logger LOG = LogManager.getLogger(JobServiceImpl.class);
@Autowired
JobMapper jobMapper;
@Override
public AjaxResult getJobById(String id){
AjaxResult result = new AjaxResult();
try {
JobEntity jobEntity = jobMapper.getJobById(id);
result.setCode(Constants.SUCCESS);
result.setData(jobEntity);
result.setMsg("获取作业[" + id + "]信息成功");
}catch (Exception e){
result.setMsg("获取作业[" + id + "]信息失败");
LOG.error("获取作业[{}]失败:{}", id, e.getMessage());
}
return result;
}
}
在mapper目录下,分别创建文件JobMapper.java
、JobMapper.xml
。
JobMapper.java
该文件是一个接口文件,定义了通过mybatis访问数据库的函数。
public interface JobMapper {
/**
* 根据id获取作业信息
* @param id 作业id
* @return JobEntity
*/
JobEntity getJobById(String id);
}
JobMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.yxdz.datax.job.mapper.JobMapper">
<resultMap id="BaseResultMap" type="top.yxdz.datax.job.entity.JobEntity">
<result column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
id, name
</sql>
<select id="getJobById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tbl_datax_job
where id=#{id, jdbcType=VARCHAR}
</select>
</mapper>
在mapper目录下,创建数据库表对应的实体类JobEntity
。
public class JobEntity {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "JobEntity{" +
" id=" + id + "\n" +
" name" + name + "\n" +
"}";
}
}
以上配置完毕,开始测试。
启动项目。
在地址栏输入地址http://localhost:8989/swagger-ui.html
,然后找到刚才写的restful接口,将参数id的值da0c96dc-2c0b-429d-ab33-cb44ff7b6677
放进去执行一下查看返回的结果是否正确。
{
"code": "SUCCESS",
"data": {
"id": "da0c96dc-2c0b-429d-ab33-cb44ff7b6677",
"name": "mysql2mysql"
},
"msg": "获取作业[da0c96dc-2c0b-429d-ab33-cb44ff7b6677]信息成功"
}
在地址栏输入地址http://localhost:8989/api/job/da0c96dc-2c0b-429d-ab33-cb44ff7b6677/info
直接查看返回页面的内容是否正确(因为是测试的接口是GET请求,所以可以直接在地址栏运行。如果是其他类型请求,则需要使用postman模仿相关请求)。
{
"code": "SUCCESS",
"data": {
"id": "da0c96dc-2c0b-429d-ab33-cb44ff7b6677",
"name": "mysql2mysql"
},
"msg": "获取作业[da0c96dc-2c0b-429d-ab33-cb44ff7b6677]信息成功"
}
Invalid bound statement (not found)
错误日志
ERROR 45545 --- [nio-8989-exec-1] t.y.d.job.service.impl.JobServiceImpl : 获取作业[da0c96dc-2c0b-429d-ab33-cb44ff7b6677]失败:Invalid bound statement (not found): top.yxdz.datax.job.mapper.JobMapper.getJobById
原因
未将JobMapper.xml
的内容加载进去
解决方法
在pom.xml
文件中,将一下内容加进去
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
错误日志
Could not autowire. No beans of ‘jobMapper’ type found.
原因
解决方法
File
-Project Structure
页面 Facets
下删掉 Spring
。
原文:https://www.cnblogs.com/yxdz2018/p/10372078.html