SpringBoot构建电商基础秒杀项目

Posted ZhangJiQun.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot构建电商基础秒杀项目相关的知识,希望对你有一定的参考价值。

目录

什么是 spring boot

mysql 依赖

数据库 ‘连接池’

新建maven 执行mubatis  generate 生成文件

什么是 MyBatis?

mybatis-generator.xml

pom.xml

application.properties 


什么是 spring boot

mysql 依赖

数据库 ‘连接池’


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

  <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>
                        <scope>runtime</scope>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <scope>runtime</scope>
                        <version>8.0.11</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <id>mybatis generator</id>
                        <phase> package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

<configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
</configuration>

<?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>
    <!--    <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->


    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 数据库链接地址账号密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306
                        /miaosha"
                        userId="root"
                        password="root">
        </jdbcConnection>


        <!--        <javaTypeResolver >-->
        <!--            <property name="forceBigDecimals" value="false" />-->
        <!--        </javaTypeResolver>-->
        <!-- 生成 DataObject 类存放位置 -->
        <javaModelGenerator targetPackage="com.example.demo.dataObject" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成映射文件存放位置 -->
        <sqlMapGenerator targetPackage="mapping"  targetProject="src/main/res
        ources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成Dao类存放位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.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">
            <!--            去除复杂操作语句-->
            <!--            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>

新建maven 执行mubatis  generate 生成文件

  • Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project demo: Access denied for user 'root'@'localhost' (using password: YES)

接入mabatis 对应mysql 数据源

什么是 MyBatis?

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

mybatis-generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN">

<generatorConfiguration>
    <!--    <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->


    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 数据库链接地址账号密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha?serverTimezone=Asia/Shanghai"

                        userId="root"
                        password="123456">
        </jdbcConnection>


        <!--        <javaTypeResolver >-->
        <!--            <property name="forceBigDecimals" value="false" />-->
        <!--        </javaTypeResolver>-->
        <!-- 生成 DataObject 类存放位置 -->
        <javaModelGenerator targetPackage="com.example.demo.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类存放位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.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">
            <!--            去除复杂操作语句-->
            <!--            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>

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.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <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>
                        <scope>runtime</scope>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <scope>runtime</scope>
                        <version>8.0.25</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>v
                        <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>
        </plugins>
    </build>

</project>

application.properties 


server.port=8090
mybatis.mapper-locations=classpath:mapping/*.xml

spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456


spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

https://www.imooc.com/learn/1079

以上是关于SpringBoot构建电商基础秒杀项目的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目

SpringBoot构建电商基础秒杀项目总结-商品模块开发

SpringBoot构建电商基础秒杀项目总结-商品列表