SpringBoot整合Mybatis,并利用mybatis-generator自动生成代码

一、在pom.xml中添加坐标和依赖:

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

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

        <!-- Spring Boot Mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- Spring Boot Mybatis 依赖 -->
        
        <!-- alibaba druid 依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
        <!-- alibaba druid 依赖 -->

        <!-- MySQL 连接驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- MySQL 连接驱动依赖 -->


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--Spring Boot Mybatis Generator依赖 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>
                        src/main/java/com/wondream/myframework/admin/mybatis/mybatis-generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.16</version>
                    </dependency>
                    <dependency>
                        <groupId>com.itfsw</groupId>
                        <artifactId>mybatis-generator-plugin</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!--Spring Boot Mybatis Generator依赖 -->
        </plugins>
    </build>

二、配置数据库连接选项,这里使用druid,数据库配置单独卸载application-db.yml中:

pagehelper:
  helperDialect:  mysql
  reasonable: true
  supportMethodsArguments:  true
  params: count=countSql

spring:
  datasource:
    druid:
      url:  jdbc:mysql://localhost:3306/javamall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false
      driver-class-name:  com.mysql.cj.jdbc.Driver
      username:  javamall
      password:  javamall123456
      initial-size:  10
      max-active:  50
      min-idle:  10
      max-wait:  60000
      pool-prepared-statements:  true
      max-pool-prepared-statement-per-connection-size:  20
      validation-query:  SELECT 1 FROM DUAL
      test-on-borrow:  false
      test-on-return:  false
      test-while-idle:  true
      time-between-eviction-runs-millis:  60000
      webStatFilter:
        enabled: true
      statViewServlet:
        enabled: false
      filter:
        stat:
          enabled: false

三、generatorConfig.xml的编写,与第一步中plugin中的configurationFile对应

<?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="mysqlgenerator" targetRuntime="MyBatis3">
        <property name="autoDelimitKeywords" value="true"/>
        <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 自动生成toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <!-- 自动生成equals方法和hashcode方法 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>

        <!-- 非官方插件 https://github.com/itfsw/mybatis-generator-plugin -->
        <!-- 查询单条数据插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
        <!-- 查询结果选择性返回插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
        <!-- Example Criteria 增强插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin"/>
        <!-- 数据Model属性对应Column获取插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
        <!-- 逻辑删除插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
            <!-- 这里配置的是全局逻辑删除列和逻辑删除值,当然在table中配置的值会覆盖该全局配置 -->
            <!-- 逻辑删除列类型只能为数字、字符串或者布尔类型 -->
            <property name="logicalDeleteColumn" value="deleted"/>
            <!-- 逻辑删除-已删除值 -->
            <property name="logicalDeleteValue" value="1"/>
            <!-- 逻辑删除-未删除值 -->
            <property name="logicalUnDeleteValue" value="0"/>
        </plugin>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!--<property name="suppressAllComments" value="true"/>-->
        </commentGenerator>

        <!--数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/litemall?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;verifyServerCertificate=false&amp;useSSL=false&amp;nullCatalogMeansCurrent=true"
                        userId="litemall"
                        password="litemall123456"/>

        <javaTypeResolver>
            <property name="useJSR310Types" value="true"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.wondream.myframework.admin.domain" targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.wondream.myframework.admin.dao" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.wondream.myframework.admin.dao"
                             targetProject="src/main/java"/>
        <!--表名-->
        <table tableName="litemall_ad">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_address">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_admin">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="role_ids" javaType="java.lang.Integer[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonIntegerArrayTypeHandler"/>
        </table>
        <table tableName="litemall_brand">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_cart">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="specifications" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>
        <table tableName="litemall_category">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_collect">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_comment">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="pic_urls" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>

        <table tableName="litemall_feedback">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="pic_urls" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>

        <table tableName="litemall_footprint">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_goods">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="gallery" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>
        <table tableName="litemall_goods_attribute">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_goods_specification">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_goods_product">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="specifications" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>
        <table tableName="litemall_issue">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_keyword">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_order">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_order_goods">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="specifications" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
            <columnOverride column="comments" javaType="java.lang.Integer[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonIntegerArrayTypeHandler"/>

        </table>
        <table tableName="litemall_region">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_search_history">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_storage">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_topic">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="goods" javaType="java.lang.Integer[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonIntegerArrayTypeHandler"/>
        </table>
        <table tableName="litemall_user">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_system">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

        <table tableName="litemall_groupon_rules">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_groupon">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_coupon">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="goods_value" javaType="java.lang.Integer[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonIntegerArrayTypeHandler"/>
        </table>
        <table tableName="litemall_coupon_user">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_role">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_permission">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_log">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_notice">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_notice_admin">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="litemall_aftersale">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <columnOverride column="pictures" javaType="java.lang.String[]"
                            typeHandler="com.wondream.myframework.admin.mybatis.JsonStringArrayTypeHandler"/>
        </table>
    </context>
</generatorConfiguration>

四、生成代码。

mvn mybatis-generator:generate

如果是在intellij 环境,直接鼠标点击即可

五、使用@MapperScan注解扫描mapper,不然会找不到dao

@SpringBootApplication
@MapperScan("com.wondream.myframework.admin.dao")
public class MallAdminApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallAdminApiApplication.class, args);
    }

}

最后附上与mybatis相关的路径

src/main/java/com/wondream/myframework/admin/mybatis/mybatis-generator/generatorConfig.xml

package com.wondream.myframework.admin.dao;
package com.wondream.myframework.admin.domain;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值