SpringBoot整合Mabatis
作者:互联网
1、导入 MyBatis 所需要的依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
2、配置数据库连接信息(延用不变,红色必要)
spring: datasource: username: root password: 123456 #?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3、创建mapper目录以及对应的 Mapper 接口
@Mapper //ibatis的注解,也可以在启动类上用mapperScan代替
@Repository
public interface TestCatMapper {
List<TestCat> getAllCat();
TestCat getCatById( Integer catId );
int addCat( TestCat testCat );
int updateCat( TestCat testCat );
int deleteCat( Integer catId );
}
4、对应的Mapper映射文件,用XML就不能在方法上直接写SQL了
<?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.hwl.swgger01.mapper.TestCatMapper"> <select id="getAllCat" resultType="TestCat"> select * from test_cat; </select> <select id="getCatById" resultType="TestCat" parameterType="int"> select * from test_cat where cat_id = #{catId}; </select> <insert id="addCat" parameterType="TestCat"> insert into test_cat(cat_name,cat_age,cat_color) value(#{catName},#{catAge},#{catColor}); </insert> <update id="updateCat" parameterType="TestCat"> update test_cat set cat_name = #{catName} where cat_id = #{catId}; </update> <delete id="delete" parameterType="int"> delete from test_cat where cat_id = #{catId}; </delete> </mapper>
5、编写Controller、service进行测试!
@RestController @RequestMapping("/cat2") public class TestCatController2 { @Autowired TestCatService testCatService; @GetMapping("/getAllCat") public List<TestCat> getAllCat(){ return testCatService.getAllCat(); } @GetMapping("/getCatById/{catId}") public TestCat getCatById( @PathVariable("catId") Integer catId ){ return testCatService.getCatById( catId ); } @GetMapping("/add") public int addCat(){ TestCat testCat = new TestCat("花花",2,"黑色"); return testCatService.addCat( testCat ); } @GetMapping("/update/{catId}") public int updateCat( @PathVariable("catId") Integer catId ){ TestCat testCat = new TestCat("修改猫",catId); return testCatService.updateCat( testCat ); } @GetMapping("/deleteCat/{catId}") public int deleteCat( @PathVariable("catId") Integer catId ){ return testCatService.deleteCat( catId ); } }
service:
@Service public class TestCatService { @Autowired TestCatMapper testCatMapper; public List<TestCat> getAllCat(){ return testCatMapper.getAllCat(); } public TestCat getCatById( Integer catId ){ return testCatMapper.getCatById( catId ); } public int addCat( TestCat testCat ){ return testCatMapper.addCat( testCat ); } public int updateCat( TestCat testCat ){ return testCatMapper.updateCat( testCat ); } public int deleteCat( Integer catId ){ return testCatMapper.deleteCat( catId ); } }
6、启动项目访问进行测试!
标签:Mabatis,return,SpringBoot,catId,cat,整合,testCat,public,TestCat 来源: https://www.cnblogs.com/haola/p/15366931.html