spring boot 2.X mybites 实战 自动生成+分页+查询使用

pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yousesky</groupId>
    <artifactId>boot-game</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>boot-game</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.7</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>game-admin</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>

mybites 生成代码配文件 generatorConfig.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>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry
            location="D:\Program Files\apache-maven-3.5.3\repo\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.0:3306/viewmode"
                        userId="root" password="pass123">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.yousesky.bootgame.entity"
                            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的包名和位置-->
        <!-- XMLMAPPER生成xml映射文件, ANNOTATEDMAPPER生成的dao采用注解来写sql -->
        <javaClientGenerator type="ANNOTATEDMAPPER"
                             targetPackage="com.yousesky.bootgame.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="users_gamebetdetail" domainObjectName="UsersGameBetDetail" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

运行插件

我是点击idea maven按钮的

添加配置 application.yml

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://192.168.1.12/jpa?characterEncoding=utf-8
    username: root
    password: pass
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    #不加这句则默认为myisam引擎
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

实战查询

单元测试,好习惯

/**
 * @Description:
 * @Author: rongyu
 * @CreateDate: 2018/10/10$ 14:37$
 * @Remark:
 */
@Slf4j
public class Test01 extends BootGameApplicationTests {
    @Resource
    private UsersGameBetDetailService usersGameBetDetailService;

    @Test
    public void test(){
        UsersGameBetDetailExample usersGameBetDetailExample = new UsersGameBetDetailExample();
        PageParameter pageParameter = new PageParameter();
        pageParameter.setPageNum(0);
        pageParameter.setPageSize(10);
        String date = "2018-09-20 18:51:55";
        Instant instant = Instant.ofEpochMilli(1537468800000L);
        Date from = Date.from(instant);
        // Mybatis查询方法
        usersGameBetDetailExample.createCriteria().andBettimeLessThan(from);
        PageInfo<UsersGameBetDetail> usersGameBetDetailPageInfo = usersGameBetDetailService.pageQuery(usersGameBetDetailExample, pageParameter);
        //UsersGameBetDetail usersGameBetDetail = usersGameBetDetailMapper.selectByPrimaryKey(1925881);
        log.info("");
    }
}

实战使用 见代码
github https://github.com/ronnierry/boot2.0-mybatis-demo

上一篇:mysql 8.0 yum 安装


下一篇:Java框架(SpringMvc)学习笔记