学习笔记,按照《深入浅出 Spring Boot 2.x》。<br>数据库设计:<br>SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for product -- ---------------------------- DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int(12) NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(255) NOT NULL COMMENT '产品名称', `stock` int(10) NOT NULL COMMENT '库存', `price` decimal(16,2) NOT NULL COMMENT '单价', `version` varchar(10) NOT NULL COMMENT '版本', `note` varchar(255) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for proecudrecode -- ---------------------------- DROP TABLE IF EXISTS `proecudrecode`; CREATE TABLE `proecudrecode` ( `id` int(12) NOT NULL AUTO_INCREMENT COMMENT '编号', `userid` int(12) NOT NULL, `productid` int(12) NOT NULL COMMENT '产品编号', `price` decimal(10,2) NOT NULL, `quantity` int(255) NOT NULL COMMENT '数量', `sum` decimal(10,2) NOT NULL COMMENT '总价', `purchar` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '购买日期', `note` varchar(255) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=601 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1;
数据库设计完毕后,我们去创建工程,这里用到mybatis,jpa,connect mysql等,pom.xml如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.product</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
我们来写dto层
@Mapper public interface ProductDao { public ProductPo getProduct(Long id); public int decreaseProduct(@Param("id") Long id, @Param("quantity") int quantity); }
@Mapper public interface Puchaesre { public int insertPurcha(PurchaseRecordPo purchaseRecordPo); }
写下po层
@Data public class ProductPo implements Serializable { private static final long serialVersionUID=328831147730635602L; private Long id; private String name; private int stock; private double price; private int version; private String note; }
@Data public class PurchaseRecordPo implements Serializable { private static final long serialVersionUID=-360816189433370174L; private long id; private long userid; private long productid; private double price; private int quantity; private double sum; private Timestamp purchar; private String note; }
这样我们去写mybatis的文件,如下
<?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.product.demo.dao.ProductDao"> <select id="getProduct" parameterType="long" resultType="ProductPo"> SELECT id,`name`,stock,price,version,note FROM product WHERE id=#{id} </select> <update id="decreaseProduct"> update product set stock=stock-#{quantity} where id=#{id} </update> </mapper>
<?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.product.demo.dao.Puchaesre"> <insert id="insertPurcha" parameterType="PurchaseRecordPo"> insert into proecudrecode(userid,productid,price,quantity,`sum`,purchar,note) values (#{userid}, #{productid},#{price},#{quantity},#{sum},now() ,#{note}) </insert> </mapper>
这里写完了,之后呢,我们就要去开发我们的业务模块了。
public interface PuseeSerice { public boolean purchase(Long userId,Long productid,int quantity); }
我们去实现下业务逻辑
@Service public class PuseeserimIMpl implements PuseeSerice { @Autowired private ProductDao productDao; @Autowired private Puchaesre puchaesre; @Override @Transactional public boolean purchase(Long userId, Long productid, int quantity) { ProductPo productPo=productDao.getProduct(productid); if (productPo.getStock()<quantity){ return false; } productDao.decreaseProduct(productid,quantity); PurchaseRecordPo purchaseRecordPo=initpush(userId,productPo,quantity); puchaesre.insertPurcha(purchaseRecordPo); return true; } private PurchaseRecordPo initpush(Long userid,ProductPo productPo,int quantity){ PurchaseRecordPo purchaseRecordPo=new PurchaseRecordPo(); purchaseRecordPo.setNote("购买时间,"+System.currentTimeMillis()); purchaseRecordPo.setPrice(productPo.getPrice()); purchaseRecordPo.setProductid(productPo.getId()); purchaseRecordPo.setQuantity(quantity); double sum=productPo.getPrice()*quantity; purchaseRecordPo.setSum(sum); purchaseRecordPo.setUserid(userid); return purchaseRecordPo; } }
实现后,我们去实现我们的api层
@RestController public class PurchaseCpntroller { @Autowired PuseeSerice puseeSerice; @PostMapping("/purchese") public Result oyrchase(Long userid,Long projectid,Integer quantity){ boolean sucse=puseeSerice.purchase(userid,projectid,quantity); String message=sucse? "抢购成功":"抢购失败"; Result result=new Result(sucse,message); return result; } } class Result{ public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } private boolean success=false; private String message=null; public Result(boolean success,String message){ this.message=message; this.success=success; } }
接下来我们去配置下,我们的启动类
package com.example.product.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.product.demo.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
我们配置了下application.yaml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/product?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: tomcat: max-active: 50 max-idle: 10 max-wait: 10000 initial-size: 5 default-transaction-isolation: 2 mybatis: type-aliases-package: com.example.product.demo.pojo mapper-locations: classpath:map/*.xml