Start your learning diary today, starting with the Learning Mall project (www.macrozheng.com/#/README).

1. Initialize the database

Create database mall-demo, run SQL file mall-demo. The content of the document is as follows:

SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for pms_brand -- ---------------------------- DROP TABLE IF EXISTS `pms_brand`; CREATE TABLE `pms_brand` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(64) DEFAULT NULL, 'first_letter' varchar(8) DEFAULT NULL COMMENT 'first ',' sort 'int(11) DEFAULT NULL, 'factory_status' int(1) DEFAULT NULL COMMENT' 1-> yes, 'show_status' int(1) DEFAULT NULL,' product_count 'int(11) DEFAULT NULL COMMENT' product_count ', 'product_comment_count' int(11) DEFAULT NULL COMMENT 'product COMMENT quantity ',' logo' varchar(255) DEFAULT NULL COMMENT 'brand logo', 'big_pic' varchar(255) DEFAULT NULL COMMENT '主 体 ',' brand_story 'text COMMENT' 主 体 ', PRIMARY KEY (' id ') ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8 COMMENT=' 10 '; -- ---------------------------- -- Records of pms_brand -- ---------------------------- INSERT INTO `pms_brand` VALUES ('1', 'wanwo ', 'W', '0', '1', '1', '100', '100', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (5). JPG', ' ', 'Victoria \' s the story of the Secret '); INSERT INTO ` pms_brand ` VALUES (' 2 ', 'samsung', 'S', '100', '1', '1', '100', '100', http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (1). JPG, null, 'the story of samsung'); INSERT INTO ` pms_brand ` VALUES (' 3 ', 'huawei', 'H', '100', '1', '1', '100', '100', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg', Null, 'Victoria\'s Secret story '); INSERT INTO ` pms_brand ` VALUES (' 4 ', 'gree', 'G', '30', '1', '1', '100', '100', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/dc794e7e74121272bbe3ce9bc41ec8c3_222_222.jpg', Null, 'Victoria\'s Secret story '); INSERT INTO ` pms_brand ` VALUES (' 5 ', 'square too', 'F', '20', '1', '1', '100', '100', '(4) http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg. JPG, null,' Victoria \ 's the story of the Secret'); INSERT INTO ` pms_brand ` VALUES (' 6 ', 'millet', 'M', '500', '1', '1', '100', '100', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180518/5afd7778Nf7800b75.jpg', 'the story of millet mobile phone); INSERT INTO `pms_brand` VALUES ('21', 'OPPO', 'O', '0', '1', '1', '88', '500', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(6).jpg', '', 'string'); INSERT INTO ` pms_brand ` VALUES (' 49 ', 'septwolves',' S ', '200', '1', '1', '77', '400', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/18d8bc3eb13533fab466d702a0d3fd1f40345bcd.jpg', Null, 'BOOB's Story '); INSERT INTO ` pms_brand ` VALUES (' 50 ', 'sea rings home', 'H', '200', '1', '1', '66', '300', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/99d3279f1029d32b929343b09d3c72de_222_222.jpg', '', 'The Story of Hailan House '); INSERT INTO ` pms_brand ` VALUES (' 51 ', 'apple', 'A', '200', '1', '1', '55', '200', "Http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg", null, 'the story of the apple'); INSERT INTO `pms_brand` VALUES ('58', 'NIKE', 'N', '0', '1', '1', '33', '100', 'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/timg (51). JPG', ' ', 'the story of the NIKE');Copy the code

2. Initialize the SpringBoot project

Create a New SpringBoot project with Spring Initializr:

Add the SpringBoot universal dependency module:

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>Copy the code

Modify MallDemoApplicationTests. Java error:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class MallDemoApplicationTests {

    @Test
    void contextLoads() {
    }

}
Copy the code

Above, the project can run normally.

3. The integrated MyBatis

Add MyBatis dependency module:

<! <dependency> <groupId>com.github. Pagehelper </groupId> < artifactId > pagehelper - spring - the boot - starter < / artifactId > < version > 1.2.10 < / version > < / dependency > <! Alibaba </groupId> <artifactId>druid-spring-boot-starter</artifactId> The < version > 1.1.10 < / version > < / dependency > <! MyBatis --> <dependency> <groupId>org.mybatis. Generator </groupId> <artifactId> The < version > 1.3.3 < / version > < / dependency > <! --> <dependency> <groupId> Mysql </groupId> <artifactId>mysql-connector-java</artifactId> The < version > 8.0.15 < / version > < / dependency >Copy the code

The configuration application. Yml

server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mall-demo? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: mybatis: mapper-locations: - classpath:mapper/*.xml - classpath*:com/**/mapper/*.xmlCopy the code

Configuration of the generator. The properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/mall-demo? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.userId=root jdbc.password=Copy the code

Configuration generatorConfig. XML

<? The 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> <properties resource="generator.properties"/> <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat"> <property  name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <! - generated serialization method for model - > < plugin type = ". Org. Mybatis generator. Plugins. SerializablePlugin "/ > <! - create a toString method for the generated Java model - > < plugin type = ". Org. Mybatis generator. Plugins. ToStringPlugin "/ > <! - can customize the generated model code comments -- > < commentGenerator type = mentGenerator "com.arielyu.mall.mbg.Com" > <! -- Whether to remove automatically generated comments true: False: no --> < Property name="suppressAllComments" value="true"/> < Property name="suppressDate" value="true"/> < Property name="addRemarkComments" value="true"/> </commentGenerator> <! <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbC.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> <! <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <! Generation model of the specified path - - - > < javaModelGenerator targetPackage = "com. Arielyu. Mall. MBG. Model" targetProject="/Users/yuchu/Documents/Gitee/mall-demo/src/main/java"/> <! - specifies generated mapper. The path of the XML - > < sqlMapGenerator targetPackage = "com. Arielyu. Mall. MBG. Mapper" targetProject="/Users/yuchu/Documents/Gitee/mall-demo/src/main/resources"/> <! Generate mapper interfaces of specified path - - - > < javaClientGenerator type = "XMLMAPPER targetPackage" = "com. Arielyu. Mall. MBG. Mapper" targetProject="/Users/yuchu/Documents/Gitee/mall-demo/src/main/java"/> <! <table tableName=" pMS_brand "> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>Copy the code

Custom comment generator:

public class CommentGenerator extends DefaultCommentGenerator { private boolean addRemarkComments = false; / * * * * / set user configuration parameters @ Override public void addConfigurationProperties Properties (Properties) { super.addConfigurationProperties(properties); this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments")); Override public void addFieldComment(Field Field, IntrospectedTable, IntrospectedTable) IntrospectedColumn introspectedColumn) { String remarks = introspectedColumn.getRemarks(); / / according to the parameters and note information to determine whether to add remark information if (addRemarkComments && StringUtility. StringHasValue (few)) {addFieldJavaDoc (field, remarks); Private void addFieldJavaDoc(Field Field, Field Field) AddJavaDocLine ("/**"); String remarks) {// Document comments start field.addJavaDocLine("/**"); String[] remarkLines = Remarks. Split (system.getProperty ("line. Separator ")); for (String remarkLine : remarkLines) { field.addJavaDocLine(" * " + remarkLine); } addJavadocTag(field, false); field.addJavaDocLine(" */"); }}Copy the code

Generator.java

Public class Generator {public static void main(String[] args) throws Exception {// Warning message during MBG execution List<String> warnings = new ArrayList<String>(); Boolean overwrite = true; / / read our MBG configuration file InputStream is = the Generator. The class. The getResourceAsStream ("/generatorConfig. XML "); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); // Create MBG MyBatisGenerator MyBatisGenerator = new MyBatisGenerator(config, callback, warnings); / / execution code generation myBatisGenerator. Generate (null). For (String warning: warnings) {system.out.println (warning); }}}Copy the code

Execute Generator. Java to generate the Model and Mapper.