一,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/mybatistest
2,功能:演示了基于mysql数据库做sql测试
3,项目结构:如图:
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,配置文件说明:
1,pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis begin--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>2.1.3</version> <scope>test</scope> </dependency> <!--mybatis end--> <!--mysql begin--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--mysql end--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2, application-dev.yml
#error server: error: include-stacktrace: always #errorlog logging: level: org.springframework.web: trace #mysql spring: datasource: url: jdbc:mysql://127.0.0.1:3306/store?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: lhddemo driver-class-name: com.mysql.cj.jdbc.Driver maximum-pool-size: 12 minimum-idle: 10 idle-timeout: 500000 max-lifetime: 540000 #mybatis mybatis: mapper-locations: classpath:/mapper/*Mapper.xml type-aliases-package: com.example.demo.mapper configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三,java代码说明
1,pojo/Goods.java
public class Goods { //商品id Long goodsId; public Long getGoodsId() { return this.goodsId; } public void setGoodsId(Long goodsId) { this.goodsId = goodsId; } //商品名称 private String goodsName; public String getGoodsName() { return this.goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } //商品标题 private String subject; public String getSubject() { return this.subject; } public void setSubject(String subject) { this.subject = subject; } //商品价格 private BigDecimal price; public BigDecimal getPrice() { return this.price; } public void setPrice(BigDecimal price) { this.price = price; } //库存 int stock; public int getStock() { return this.stock; } public void setStock(int stock) { this.stock = stock; } @Override public String toString(){ return " Goods:goodsId=" + goodsId +" goodsName=" + goodsName+" subject=" + subject+" price=" + price+" stock=" + stock; } }
2,mapper/GoodsMapper.java
@Repository @Mapper public interface GoodsMapper { Goods selectOneGoods(Long goodsId); int insertOneGoods(Goods goods); int updateOneGoods(Goods goods); }
3,mapper/GoodsMapper.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.mybatistest.demo.mapper.GoodsMapper"> <select id="selectOneGoods" parameterType="long" resultType="com.mybatistest.demo.pojo.Goods"> select * from goods where goodsId=#{goodsId} </select> <update id="updateGoodsStock"> UPDATE goods SET stock = stock+#{changeAmount,jdbcType=INTEGER} WHERE goodsId = #{goodsId,jdbcType=BIGINT} </update> <insert id="insertOneGoods" parameterType="com.mybatistest.demo.pojo.Goods" useGeneratedKeys="true" keyProperty="goodsId" > insert into goods(goodsName,subject,price,stock) values( #{goodsName},#{subject},#{price},#{stock} ) </insert> </mapper>
4,mapper/GoodsMapperTest.java
@ActiveProfiles("dev") @MybatisTest() @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class GoodsMapperTest { @Resource private GoodsMapper goodsMapper; @Test @DisplayName("读取一条记录") void selectOneGoods() { Goods goodsOne = goodsMapper.selectOneGoods(3L); System.out.println(goodsOne); assertThat(goodsOne.getGoodsId(), equalTo(3L)); } @Test @DisplayName("插入一条记录并读取") void insertOneGoods() { Goods goodsOne = new Goods(); //goodsOne.setGoodsId(13L); goodsOne.setGoodsName("商品名称xy"); goodsOne.setSubject("商品描述"); goodsOne.setPrice(new BigDecimal(101)); goodsOne.setStock(13); int insNum = goodsMapper.insertOneGoods(goodsOne); assertThat(insNum, equalTo(1)); Long goodsId = goodsOne.getGoodsId(); //assertThat(goodsId, equalTo(14L)); Goods goods = goodsMapper.selectOneGoods(goodsId); System.out.println(goods); //Goods goodsRet = goodsService.getOneGoodsById(14L); assertThat(goods.getGoodsName(), equalTo("商品名称xy")); } }
5,其他相关代码可访问github
四,测试效果
五,备注
1,测试前需要启动mysql数据库
2,测试前需要准备好测试数据
3,注解不需要再写:@Transactional
六,查看spring boot的版本:
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.4)