1.pom.xml
<!-- 连接数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- web开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis整合,包含jdbc包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 配置文件提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 日志、getset方法等 -->
<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>
</dependency>
<!-- druid数据源 -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.0.18</version>-->
<!-- </dependency>-->
<!-- 官方的数据域,监控中心、druid数据域 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!-- 引入静态模板引擎,才能访问templates下面的静态页面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.bean
@Data
public class GasTest {
String isLeak;
int id;
String time;
float pressure;
//需要开启驼峰命名规则
float flowRate;
float velocity;
}
@Data:lombok的jar包,封装get、set方法
3.resources目录截图
建议先学SpringBoot的静态资源和动态资源目录
4.application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/graduationproject
username: root
password: 113846
driver-class-name: com.mysql.jdbc.Driver #5.x版本mysql
#配置mybatis规则
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml #全局只允许一个配置文件,这是开启驼峰命名规则的配置文件
mapper-locations: classpath:mybatis/mapper/*.xml
configuration: #指定mybatis全局配置文件中的配置项目,那就不需要mybatis-config.xml了
map-underscore-to-camel-case: true #开启驼峰命名规则
5.整合mybatis
5.1使用配置文件方式整合mybatis,查询数据库
(1).mapper
@Mapper
public interface GasTestMapper {
public GasTest getIdInfo(int id);
}
@Mapper:声明这是一个bean。在连接数据库中加上@Mapper注解,使用mybatis配置文件mapper.xml,mapper.xml中的namespace指向mapper,会动态生成bean。在服务实现层serviceImpl中能够@Autowired注解动态注入该bean。
(2).mybatis\mapper\GasTestMapper.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="com.example.mapper.GasTestMapper">
<select id="getIdInfo" resultType="com.example.bean.GasTest">
select * from gastest where id=#{id}
</select>
</mapper>
namespace:命名空间,mapper接口类的路径
id:mapper中的接口方法
resultType:返回数据集的数据类型
(3).service和serviceimpl
@Service
public interface GasTestService {
public GasTest getIdInfo(int id);
}
@Service
public class GasTestServiceImpl implements GasTestService {
@Autowired
GasTestMapper gasTestMapper;
@Override
public GasTest getIdInfo(int id) {
return gasTestMapper.getIdInfo(id);
}
}
(4)controller
@Slf4j
@RestController
public class GasTestController {
@Autowired
GasTestService gasTestService;
//使用配置文件方式整合mybatis,查询数据库,方式1
//注解RequestParam,路径传参,参为id,方式1
@RequestMapping("/gas")
public GasTest getIdInfo(@RequestParam("id") int id){
return gasTestService.getIdInfo(id);
}
}
(5)接口测试结果展示
5.2使用纯注解方式整合mybatis,查询数据库
(1).mapper
@Mapper
public interface GasTest2Mapper {
//方式2
@Select("select * from gastest where id=#{id}")
public GasTest2 getIdInfo(int id);
}
(2).service和serviceimpl
@Service
public interface GasTest2Service {
public GasTest2 getIdInfo(int id);
}
@Service
public class GasTest2ServiceImpl implements GasTest2Service {
@Autowired
GasTest2Mapper gasTest2Mapper;
public GasTest2 getIdInfo(int id){
return gasTest2Mapper.getIdInfo(id);
}
}
(3).controller
@Autowired
GasTest2Service gasTest2Service;
//使用纯注解整合mybatis,查询数据库,方式2
//注解RequestParam,路径传参,参为id,方式2
@RequestMapping("/gas2")
public GasTest2 getIdInfo2(@RequestParam("id") int id){
return gasTest2Service.getIdInfo(id);
}
(4).接口测试展示结果
5.3使用注解+配置文件方式整合mybatis,查询数据库
(1).mapper或mapper
@Mapper
public interface GasTest2Mapper {
//方式3
//也可以用配置文件方式mybatis\mapper\GasTest2Mapper.xml
@Insert("insert into gastest(`isleak`,`time`,`pressure`,`flow_rate`,`velocity`) values(#{isLeak},#{time},#{pressure},#{flowRate},#{velocity})")
//开启将没有表单传参的自增id返回,Options注解方式,
@Options(useGeneratedKeys = true,keyProperty = "id")
public void insert(GasTest2 gasTest2);
}
(2).mybatis\mapper\GasTest2Mapper.xml或mybatis\mapper\GasTest2Mapper.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="com.example.mapper.GasTest2Mapper">
<!-- useGeneratedKeys="true" keyProperty="id": 开启将没有表单传参的自增id返回 -->
<!-- <insert id="insert" useGeneratedKeys="true" keyProperty="id">-->
<!-- insert into gastest(`isleak`,`time`,`pressure`,`flow_rate`,`velocity`) values(#{isLeak},#{time},#{pressure},#{flowRate},#{velocity})-->
<!-- </insert>-->
</mapper>
(3).service和serviceImpl
@Service
public interface GasTest2Service {
public void insert(GasTest2 gasTest2);
}
@Service
public class GasTest2ServiceImpl implements GasTest2Service {
@Autowired
GasTest2Mapper gasTest2Mapper;
public void insert(GasTest2 gasTest2){
gasTest2Mapper.insert(gasTest2);
}
}
(4).controller
//方式3
//也可以用配置文件方式mybatis\mapper\GasTest2Mapper.xml
@Insert("insert into gastest(`isleak`,`time`,`pressure`,`flow_rate`,`velocity`) values(#{isLeak},#{time},#{pressure},#{flowRate},#{velocity})")
//post请求。insert这个方法既可以1.使用配置方式 2.也可以使用注解方式 3.或配置+注解方式一块用
@PostMapping("/gas3")
public GasTest2 insertGasTestInfo(GasTest2 gasTest2){
log.info("gasTest2={}",gasTest2);
gasTest2Service.insert(gasTest2);
return gasTest2;
}