springboot 整合 MyBatis 使用generator自动生成代码

目录结构:

springboot 整合 MyBatis 使用generator自动生成代码_第1张图片


application.properties

server.port=8080

spring.datasource.name=mysql_test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#druid相关配置
#监控统计拦截的filters
spring.datasource.druid.filters=stat
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://192.168.3.41:3306/naxiaoxin-saladb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=abcd-1234
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=select 'x'
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20

mybatis.mapper-locations=classpath:mapper/*.xml

generatorConfig.xml

xml version="1.0" encoding="UTF-8"?>
generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


    resource="application.properties" />
    location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\6.0.6\mysql-connector-java-6.0.6.jar" />
    id="Tables" targetRuntime="MyBatis3">
        
        
            
            name="suppressDate" value="true"/>
            
            name="suppressAllComments" value="true"/>
        

        
                        driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://192.168.3.41:3306/naxiaoxin-saladb?useSSL=false&nullNamePatternMatchesAll=true"
                userId="root"
                password="abcd-1234">
        

        
        
        
            
            name="forceBigDecimals" value="false" />
        

        
        targetPackage="com.demo_mybatis.model" targetProject="src/main/java">
            
            name="trimStrings" value="true" />
            
            name="enableSubPackages" value="false" />
        

        
        targetPackage="mapper"  targetProject="src/main/resources">
            
            name="enableSubPackages" value="false" />
        

        
        targetPackage="com.demo_mybatis.test.dao" targetProject="src/main/java" type="XMLMAPPER">
            
            name="enableSubPackages" value="false" />
        

        
        

        schema="naxiaoxin-saladb" tableName="product_info"
               domainObjectName="ProductInfo" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        

pom.xml

xml version="1.0" encoding="UTF-8"?>
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">
   4.0.0

   com.demo_mybatis
   test
   0.0.1-SNAPSHOT
   jar

   test
   Demo project for Spring Boot

   
      org.springframework.boot
      spring-boot-starter-parent
      2.0.3.RELEASE
       
   

   
      UTF-8
      UTF-8
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-jdbc
      
      
         org.springframework.boot
         spring-boot-starter-web
      
      
         org.mybatis.spring.boot
         mybatis-spring-boot-starter
         1.3.2
      

      
         mysql
         mysql-connector-java
         runtime
      
      
         org.springframework.boot
         spring-boot-starter-test
         test
      

      
         org.apache.commons
         commons-lang3
         3.4
      
      
         com.fasterxml.jackson.core
         jackson-core
      
      
         com.fasterxml.jackson.core
         jackson-databind
      
      
         com.fasterxml.jackson.datatype
         jackson-datatype-joda
      
      
         com.fasterxml.jackson.module
         jackson-module-parameter-names
      
      
      
         com.github.pagehelper
         pagehelper-spring-boot-starter
         1.2.5
      
      
      
         com.alibaba
         druid-spring-boot-starter
         1.1.9
      

      
      
      
         org.mybatis.generator
         mybatis-generator-core
         1.3.6
      
   

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
         
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.2
            
               src/main/resources/generatorConfig.xml 
               true
               true
            
         
      
   




运行:

springboot 整合 MyBatis 使用generator自动生成代码_第2张图片


或者

springboot 整合 MyBatis 使用generator自动生成代码_第3张图片


然后补齐service层和controller层

package com.demo_mybatis.controller;

import com.demo_mybatis.model.ProductInfo;
import com.demo_mybatis.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;

    @ResponseBody
    @PostMapping(value = "/addProductInfo")
    public int addProductInfo(ProductInfo productInfo){
        /*ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId("1");
        productInfo.setProductName("张亮麻辣烫20元套餐");
        productInfo.setProductPrice(20.00);
        productInfo.setProductStock(20);
        productInfo.setProductDescription("张亮麻辣烫20元套餐张亮麻辣烫20元套餐张亮麻辣烫20元套餐张亮麻辣烫20元套餐张亮麻辣烫20元套餐张亮麻辣烫20元套餐张亮麻辣烫20元套餐");
        productInfo.setProoductIcon("1111");
        productInfo.setCategoryType(1);*/
        return productInfoService.insert(productInfo);
    }
}
package com.demo_mybatis.service.impl;

import com.demo_mybatis.mapper.ProductInfoMapper;
import com.demo_mybatis.model.ProductInfo;
import com.demo_mybatis.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Override
    public int insert(ProductInfo record) {
        return productInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(ProductInfo record) {
        return 0;
    }
}
package com.demo_mybatis.service;

import com.demo_mybatis.model.ProductInfo;

public interface ProductInfoService {
    int insert(ProductInfo record);

    int insertSelective(ProductInfo record);
}

运行:

springboot 整合 MyBatis 使用generator自动生成代码_第4张图片



你可能感兴趣的:(spring,boot)