社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
电商秒杀项目介绍
以下流程为课程操作,实际也可以在IDEA进行Spring Initial快速配置SpringBoot
new->project->maven项目->选择maven-archetype-quickstart
新建一个resources目录,作为资源文件目录,点击右键,指定为Resource root
引入SpringBoot依赖包实现简单的Web项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在pom.xml中引入依赖,启动App,访问localhost:8080
在SpringBoot的默认配置文件application.properties可以配置数据库连接,引入Mybatis,使用druid数据源
#引入Mybatis
mybatis.mapper-locations=classpath:mapping/*.xml
#数据库配置
spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=yourpassword
#使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
<!--自动生成工具,生成数据库文件的映射-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许自动覆盖文件(生产环境中千万不要这样做)-->
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
Mybatis generator自动生成数据库对应的映射文件
引入官方配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/miaosha?serverTimezone=GMT%2B8"
userId="root"
password="sxy123">
</jdbcConnection>
<!-- 生成DataObject类存放位置 -->
<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>
<!-- 生成Dao类存放位置 -->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
type="ANNOTATIONDMAPPER",生成Java Model和基于注解的Mapper 对象
type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML 文件和独立的Mapper接口
-->
<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>
<table tableName="item" domainObjectName="ItemDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="item_stock" domainObjectName="ItemStockDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="order_info" domainObjectName="OrderDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="sequence_info" domainObjectName="SequenceDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
<table tableName="promo" domainObjectName="PromoDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
—————————————————————————————————
【系列笔记已经更新完毕】
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!