创建数据库表对应的实体类
@Data
public class Template {
private int id;
private String name;
private String type;
private int productId;
private Timestamp createTime;
private Timestamp updateTime;
private Timestamp deleteTime;
}
创建 TemplateMapper.java
@Mapper
public interface TemplateMapper {
List<Template> getTemplateList(String productId);
}
创建 TemplateMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vazquez.devops.mapper.TemplateMapper">
<resultMap id="TemplateMap" type="com.vazquez.devops.model.Template">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="type" property="type"/>
<result column="product_id" property="productId"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="delete_ime" property="deleteTime"/>
</resultMap>
<select id="getTemplateList" resultMap="TemplateMap">
select * from template where product_id = #{productId}
</select>
</mapper>
创建 TemplateService.java
public interface TemplateService {
Response getTemplateList(String productId);
}
创建 TemplateServiceImpl.java
@Service
public class TemplateServiceImpl implements TemplateService {
@Autowired
TemplateMapper templateMapper;
@Override
public Response getTemplateList(String productId) {
List<Template> templateList = templateMapper.getTemplateList(productId);
return Response.success(HttpStatus.OK.toString(), "查询所有模板成功", templateList);
}
}
创建 TemplateController.java
@RestController
@RequestMapping("/api/template")
public class TemplateController {
@Autowired
TemplateService templateService;
@GetMapping("/getTemplateList/{productId}")
public Response getTemplateList(@PathVariable String productId) {
return templateService.getTemplateList(productId);
}
}
创建 mybaits 的配置文件 mybatisConf.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启驼峰命名,不然会查询数据为空-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
创建统一返回对象
public class Response<T> {
private String status;
private String message;
private T data;
public Response(String status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> Response<T> success(String code, String message, T data) {
return new Response<>(code, message, data);
}
}
修改 application.yml
具体的 springboot 整合 mybatis 参考历史文章
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx
username: root
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# 全局配置文件
config-location: classpath:mybatis/mybatisConf.xml
# sql 映射文件
mapper-locations: classpath:mybatis/mapper/*.xml