秒杀系统(二)——构建maven多模块项目及用户模块测试
作者:互联网
项目地址:https://github.com/shenweiquan/ms
后端整体项目结构:
步骤说明:
1):创建父工程
创建一个maven工程,填写基本信息。创建完成后可以删除src文件夹
2): 创建子工程
创建子工程的步骤基本一样,这里举例创建web子工程,我这里创建的是springboot项目。
同样填写你自己的信息。
创建好项目之后,可以看一下pom.xml文件看parent是否正确。
不正确的话可以相应修改。这里我原本是不正确的,parent是上图注释内容。后来我直接修改了。注意我这里修改后父工程中的
parent也进行了修改。下面是父工程的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 http://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.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xxx</groupId> <artifactId>ms-parent</artifactId> <packaging>pom</packaging> <version>1.0</version>
<!--子模块--> <modules> <module>ms-pojo</module> <module>ms-dao</module> <module>ms-service</module> <module>ms-web</module> <module>ms-common</module> </modules> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--<dependency>--> <!--<groupId>mysql</groupId>--> <!--<artifactId>mysql-connector-java</artifactId>--> <!--</dependency>--> <!--mysql驱动包--> <!--<dependency>--> <!--<groupId>mysql</groupId>--> <!--<artifactId>mysql-connector-java</artifactId>--> <!--<version>5.1.35</version>--> <!--</dependency>--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--druid连接池--> <!--<dependency>--> <!--<groupId>com.alibaba</groupId>--> <!--<artifactId>druid</artifactId>--> <!--<version>1.0.18</version>--> <!--</dependency>--> </dependencies> </project>
其余模块步骤基本一致。我这里除web模块是springboot工程之外,其余都是普通的maven工程。
3): 测试用户模块
pojo模块创建bean
package com.swq.pojo; /** * 商家实体类 * @date:5.11 * @author :swq * */ public class Merchant { private long id; //主键 private String merchantname; //商家名 private String merchantaccount;//商家账号 public String getMerchantpassword() { return merchantpassword; } public void setMerchantpassword(String merchantpassword) { this.merchantpassword = merchantpassword; } private String merchantpassword;//商家账号密码 public long getId() { return id; } public void setId(long id) { this.id = id; } public String getMerchantname() { return merchantname; } public void setMerchantname(String merchantname) { this.merchantname = merchantname; } public String getMerchantaccount() { return merchantaccount; } public void setMerchantaccount(String merchantaccount) { this.merchantaccount = merchantaccount; } @Override public String toString() { return "Merchant{" + "id=" + id + ", merchantname='" + merchantname + '\'' + ", merchantaccount='" + merchantaccount + '\'' + ", merchantpassword='" + merchantpassword + '\'' + '}'; } }
dao模块创建mapper接口文件
package com.swq.ms.dao.merchant; import com.swq.pojo.Merchant; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; /** * 商家mapper * @date: 5.11 * @author : swq */ @Mapper public interface MerchantMapper { void insertMerchant(Merchant merchant); //添加商家 Merchant selectMerchantById(long id); //根据id查询商家 List<Merchant> selectAllMerchant(); //查询所有商家 void updateMerchant(Merchant merchant); //更新商家信息 void deleteMerchant(long id);//删除商家 }
其次在dao模块下的resource文件夹中创建mapper文件夹,以及mapper.xml文件
MerchantMapper.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.swq.ms.dao.merchant.MerchantMapper"> <resultMap id="MerchantMap" type="Merchant"> <id property="id" column="id"/> <result property="merchantname" column="merchant_name"/> <result property="merchantaccount" column="merchant_account" /> <!--不需要密码--> </resultMap> <insert id="insertMerchant" parameterType="Merchant" > insert into merchant(merchant_name,merchant_account,merchant_password) values (#{merchantname},#{merchantaccount},#{merchantpassword}) </insert> <select id="selectMerchantById" parameterType="Long" resultMap="MerchantMap"> select * from merchant where id = #{id} </select> <select id="selectAllMerchant" resultMap="MerchantMap"> select * from merchant; </select> <update id="updateMerchant" parameterType="Merchant"> update merchant <trim prefix="set" suffixOverrides=","> <if test="merchantname != null"> merchant_name = #{merchantname}, </if> <if test=" merchantaccount != null "> merchant_account = #{merchantaccount}, </if> <if test=" merchantpassword != null "> merchant_password = #{merchantpassword}, </if> </trim> where id = #{id} </update> <delete id="deleteMerchant" parameterType="Long"> delete from merchant where id = #{id} </delete> </mapper>
注意dao模块需要导入pojo模块。
service模块创建service接口及实现,实现类记得加@service
package com.swq.ms.service.merchant; import com.swq.pojo.Merchant; import org.springframework.stereotype.Service; import java.util.List; public interface MerchantService { void insertMerchant(Merchant merchant); //添加商家 Merchant selectMerchantById(long id); //根据id查询商家 List<Merchant> selectAllMerchant(); //查询所有商家 void updateMerchant(Merchant merchant); //更新商家信息 void deleteMerchant(long id);//删除商家 }
package com.swq.ms.service.merchant.impl; import com.swq.ms.dao.merchant.MerchantMapper; import com.swq.ms.service.merchant.MerchantService; import com.swq.pojo.Merchant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class MerchantServiceImpl implements MerchantService { @Autowired MerchantMapper merchantMapper; @Override public void insertMerchant(Merchant merchant) { merchantMapper.insertMerchant(merchant); } @Override public Merchant selectMerchantById(long id) { return merchantMapper.selectMerchantById(id); } @Override public List<Merchant> selectAllMerchant() { return merchantMapper.selectAllMerchant(); } @Override public void updateMerchant(Merchant merchant) { merchantMapper.updateMerchant(merchant); } @Override public void deleteMerchant(long id) { merchantMapper.deleteMerchant(id); } }
最重要的是web模块
先看配置文件,重点是mybatis的配置
server: port: 8099 #配置数据源 spring: datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/swqms?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC username: root password: root # type: com.alibaba.druid.pool.DruidDataSource # #连接池配置 # # 初始化大小,最小等待连接数量,最大等待连接数量,最大连接数 # initialsize: 1 # minidle: 1 # maxidle: 5 # maxActive: 20 # #最长等待时间 # maxWait: 6000 # # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 # timeBetweenEvictionRunsMillis: 60000 # # 配置一个连接在池中最小生存的时间,单位是毫秒 # minEvictableIdleTimeMillis: 300000 # validationQuery: SELECT 1 FROM DUAL # testWhileIdle: true # testOnBorrow: true # testOnReturn: false # # 打开PSCache,并且指定每个连接上PSCache的大小 # poolPreparedStatements: false # maxPoolPreparedStatementPerConnectionSize: 20 # # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 # filters: stat,wall,log4j # # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 # connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # # 合并多个DruidDataSource的监控数据 # #spring.datasource.useGlobalDataSourceStat=true # # # #druid监控 # druid: # login: # username: root # password: root #mybatis配置 mybatis: mapper-locations: classpath*:/mapper/*.xml type-aliases-package: com.swq.pojo
然后是web模块的启动文件:
这里重要的是mapper扫描和service扫描。起初我sacnbasePackage扫描service,能扫描serivice但是无法扫描controller包下的内容。
后来干脆使用@ComponSacn显式扫描controller,注意不能使用scanbasePackage配置扫描service和controller,有人可以,但我这里尝试后不行。
package com.swq.ms.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @MapperScan("com.swq.ms.dao") //扫描mapper @SpringBootApplication @ComponentScan({"com.swq.ms.service.*","com.swq.ms.web.controller"}) //扫描服务类和接口 public class MsWebApplication { public static void main(String[] args) { SpringApplication.run(MsWebApplication.class, args); } }
最后使用postman测试成功。
标签:merchant,public,maven,秒杀,模块,swq,import,com,id 来源: https://www.cnblogs.com/swqblog/p/12871011.html