SpringBoot+Mybatis+MySql 自動生成代碼 自動分頁

1、配置文件

<!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.5</version>
        </dependency>
        <!-- pagehelper 分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
<!-- 自動生成代碼 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.34</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</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>
spring:
  profiles:
    active: dev

logging:
  config: classpath:xml/logback-boot.xml

mybatis:
  # type-aliases掃描路徑
  type-aliases-package: com.czhappy.wanmathapi.entity
  # mapper xml實現掃描路徑
  mapper-locations: classpath:mapper/*.xml
  property:
    order: BEFORE

#mappers 多個接口時逗號隔開
mapper:
  mappers: com.czhappy.wanmathapi.config.MyMapper
  not-empty: false
  identity: mysql

#pagehelper分頁配置 第二種和第三種不須要 重點講的第一種須要
pagehelper:
  helperDialect: mysql
  reasonable: false
  supportMethodsArguments: true
  params: count=countSql

 

2、Dao配置

自定義MyMapper,做爲dao的父類,此文件不要和其餘dao文件放一塊兒,防止掃描時有影響,單獨建個文件夾。java

package com.czhappy.wanmathapi.config;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {

}

3、service配置

package com.czhappy.wanmathapi.service;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface IService<T> {

    List<T> selectAll();

    T selectByKey(Object key);

    int save(T entity);

    int delete(Object key);

    int updateAll(T entity);

    int updateNotNull(T entity);

    List<T> selectByExample(Object example);
}
package com.czhappy.wanmathapi.service.impl;

import com.czhappy.wanmathapi.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public abstract class BaseService<T> implements IService<T> {

    @Autowired
    protected Mapper<T> mapper;

    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public List<T> selectAll() {
        //說明:查詢全部數據
        return mapper.selectAll();
    }

    @Override
    public T selectByKey(Object key) {
        //說明:根據主鍵字段進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public int save(T entity) {
        //說明:保存一個實體,null的屬性也會保存,不會使用數據庫默認值
        return mapper.insert(entity);
    }

    @Override
    public int delete(Object key) {
        //說明:根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int updateAll(T entity) {
        //說明:根據主鍵更新實體所有字段,null值會被更新
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateNotNull(T entity) {
        //根據主鍵更新屬性不爲null的值
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<T> selectByExample(Object example) {
        //說明:根據Example條件進行查詢
        //重點:這個查詢支持經過Example類指定查詢列,經過selectProperties方法指定查詢列
        return mapper.selectByExample(example);
    }
}

4、自動生成代碼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="mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.czhappy.wanmathapi.config.MyMapper"/>
            <!--caseSensitive默認false,當數據庫表名區分大小寫時,能夠將該屬性設置爲true-->
            <property name="caseSensitive" value="false"/>
        </plugin>

        <!-- 阻止生成自動註釋 -->
        <commentGenerator>
            <property name="javaFileEncoding" value="UTF-8"/>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--數據庫連接地址帳號密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/wanmath"
                        userId="root" password="root">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成Model類存放位置-->
        <javaModelGenerator targetPackage="com.czhappy.wanmathapi.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

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

        <!--生成Dao類存放位置-->
        <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
                type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象
                type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.czhappy.wanmathapi.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--生成對應表及類名去掉Mybatis Generator生成的一堆 example-->
        <table tableName="sys_dict" domainObjectName="SysDict" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="mysql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

5、目錄結構

雙擊生成實體類、mapper、dao相關代碼:mysql

相關文章
相關標籤/搜索