目录
打开pom.xml文件,修改parent中的version为2.0.5 RELEASE
目前只用到前两张表
添加记录
将resources目录下的application.properties改为application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/devicemanage?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
# 如果pom.xml中mysql的版本达到8.0.15,则包名必须为com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
# mybatis必须顶格写,否则变成是spring.datasource的一个属性了
mybatis:
# mapper-locations的内容表示工程运行时,所有的mapper接口的xml文件放在类路径的mapping目录下。这样Springboot在实例化mapper接口时,可以在这个位置找到相同接口名的xml文件
mapper-locations: classpath:mapping/*.xml
# type-aliases-package的内容表示pojo类的包名。这样Springboot可以将zjc.devicemanage.entity包下的所有pojo类进行实例化
type-aliases-package: zjc.devicemanage.entity
# server.port指定运行时的端口号
server:
port: 8080
# logging.level属性中填写mapper接口的包路径,并设置级别为debug,这样就可以实现showSql的功能,即mybatis所做的数据库操作都可以在控制台显示出来
logging:
level:
zjc:
demo:
mapper : debug
新建java类DeviceClass
public class DeviceClass {
private Integer DeviceClassID;
private String DeviceClassName;
public Integer getDeviceClassID() {
return DeviceClassID;
}
public void setDeviceClassID(Integer deviceClassID) {
DeviceClassID = deviceClassID;
}
public String getDeviceClassName() {
return DeviceClassName;
}
public void setDeviceClassName(String deviceClassName) {
DeviceClassName = deviceClassName;
}
}
新建接口文件DeviceClassMapper
@Repository
public interface DeviceClassMapper {
public List<DeviceClass> findAllDeivceClass();
public DeviceClass findDeviceClass(int deviceClassId);
}
在resources的mapping包中新建DeviceClassMapper.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">
<!-- 1. xml文件名必须和接口名同名,否则springboot找不到zjc.devicemanage.mapper.DeviceClassMapper接口对应的xml文件 -->
<!-- 2. 必须设置namespace属性,属性值必须是同名的接口名,这样xml文件名DeviceClassMapper才能
和接口全称zjc.devicemanage.mapper.DeviceClassMapper配对 -->
<mapper namespace="zjc.devicemanage.mapper.DeviceClassMapper">
<!-- 3. id值必须是接口zjc.devicemanage.mapper.DeviceClassMapper中的函数名,resultType表示表user中的所有列名
和pojo类zjc.devicemanage.entity.DeviceClass中的所有属性名相同
这样就可以自动把查询出来的每一列对应到DeviceClass对象的每一个属性中 -->
<select id="findDeviceClass" resultType="zjc.devicemanage.entity.DeviceClass">
select * from deviceclass where id = #{deviceClassId}
</select>
<!-- 4. findAllUser的返回值是数组,resultType既可以是第8行中单个对象返回值,也可以是第12行中的数组返回值。
表示反向工程时,表中的列和对象中的属性名相同 -->
<select id="findAllDeviceClass" resultType="zjc.devicemanage.entity.DeviceClass">
select * from deviceclass
</select>
</mapper>
在service包下新建类DeviceClassService
@Service
public class DeviceClassService {
@Autowired
DeviceClassMapper deviceClassMapper;
public List<DeviceClass> findAllDeivceClass(){
return deviceClassMapper.findAllDeivceClass();
}
public DeviceClass findDeviceClass(int deviceClassId){
return deviceClassMapper.findDeviceClass(deviceClassId);
}
}
在controller包下新建类DeviceClassController
@RestController
@RequestMapping("/test")
public class DeviceClassController {
@Autowired
DeviceClassService deviceClassService;
@RequestMapping("/getAllDeviceClass")
public List<DeviceClass> GetAllDeviceClass(){
return deviceClassService.findAllDeivceClass();
}
}
运行后报错
在DevicemanageApplication中添加代码
此时
显示无法找到findAllDeviceClass方法
mapper中的方法名写错了
使用refactor重构
Service中的方法也需要重构
成功!
添加查找单个设备功能
@RequestMapping("/getDeviceClass/{DeviceClassID}")
public DeviceClass GetUser2(@PathVariable("DeviceClassID") int deviceClassId){
return deviceClassService.findDeviceClass(deviceClassId);
}
此时运行后会报错
最后发现是xml文件写错了,修改id为DeviceClassID
成功!