源码地址:
https://github.com/KiroScarlet/PromoProject
慕课网SpringBoot构建电商基础秒杀项目,课程地址:
https://www.imooc.com/learn/1079
电商秒杀应用简介
- 商品列表页获取秒杀商品列表
- 进入商品详情页获取秒杀商品详情
- 秒杀开始后进入下单确认页下单并支付成功
1.new->project->maven项目->选择maven-archetype-quickstart
这种方式是以jar包方式对外输出
稍等一会,可能会有点慢
2.新建一个resources目录,作为资源文件目录,点击右键,指定为Resource root
进入官方文档
https://spring.io/guides/gs/rest-service/
Building a RESTful Web Service
1.引入父pom
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
parent>
2.引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
3.maven Reimport刷新一下,会自动下载相应jar包(注:可以把idea设定为自动导入maven依赖)
4.SpringBoot的Web项目
@EnableAutoConfiguration
@RestController
public class App
{
@RequestMapping("/")
public String home() {
return "hello World!";
}
public static void main( String[] args )
{
System.out.println("Hello World!");
SpringApplication.run(App.class,args);
}
}
再次启动App,访问localhost:8080
1.SpringBoot的默认配置
在resources目录下新建SpringBoot的默认配置文件application.properties
通过一行简单的属性就能更改tomcat的端口
server.port=8090
2.配置pom文件
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
3.配置文件application.properties,设置
mybatis.mapper-locations=classpath:mapping/*.xml
然后在resources目录下新建mapping目录
4.自动生成工具,生成数据库文件的映射
引入插件
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.5version>
<dependencies>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.41version>
dependency>
dependencies>
<executions>
<execution>
<id>mybatis generatorid>
<phase>packagephase>
<goals>
<goal>generategoal>
goals>
execution>
executions>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
configurationFile>
configuration>
plugin>
1.新建文件src/main/resources/mybatis-generator.xml,从官网下载xml配置文件
http://www.mybatis.org/generator/configreference/xmlconfig.html
2.新建数据库
新建一个miaosha的数据库,并建立两张表,分别是user_info和user_password
3.修改配置文件
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/miaosha"
userId="root"
password="123456">
jdbcConnection>
<javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="user_info" domainObjectName="UserDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">table>
<table tableName="user_password" domainObjectName="userPasswordDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" >table>
context>
generatorConfiguration>
4.生成文件
在终端运行mvn mybatis-generator:generate
命令
5.接入mysql数据源
spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha
spring.datasource.username=root
spring.datasource.password=123456
#使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6.测试数据库
修改App类
@SpringBootApplication(scanBasePackages = {"com.miaoshaproject"})
//使用@SpringbootApplication注解 可以解决根类或者配置类头上注解过多的问题,一个@SpringbootApplication相当于
//@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他们的默认属性值
@RestController
@MapperScan("com.miaoshaproject.dao")
public class App {
@Autowired
private UserDOMapper userDOMapper;
@RequestMapping("/")
public String home() {
UserDO userDO = userDOMapper.selectByPrimaryKey(1);
if (userDO == null) {
return "用户对象不存在";
} else {
return userDO.getName();
}
}
}
启动测试
后续教程:
SpringBoot构建电商秒杀项目实战(二)
SpringBoot构建电商秒杀项目实战(三)
SpringBoot构建电商秒杀项目实战(四)
SpringBoot构建电商秒杀项目实战(五)