搭建SpringBoot-MyBatis-MyBatisGenerator项目

搭建SpringBoot-MyBatis-MyBatisGenerator web 项目

以往使用tomcat下部署SSM项目, 习惯了老套路. 费尽一番周折终于把项目起来了, 当然这只是第一步…闲话少说进入正题:

问题1:相关jar包引入

jar包引入出错, 很可能会报各种class not found

pom.xml

<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.0modelVersion>
    <groupId>io.banjuergroupId>
    <artifactId>kindle-noteartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.6.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <start-class>com.practice.Appstart-class>
        <java.version>1.8java.version>
        <lombok.version>1.14.8lombok.version>
        <log4jdbc.log4j2.version>1.16log4jdbc.log4j2.version>
        <rest.assured.version>2.3.3rest.assured.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.8version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.3version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.3version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.26version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>com.h2databasegroupId>
            <artifactId>h2artifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>${lombok.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2groupId>
            <artifactId>log4jdbc-log4j2-jdbc4.1artifactId>
            <version>${log4jdbc.log4j2.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>com.jayway.restassuredgroupId>
            <artifactId>rest-assuredartifactId>
            <version>${rest.assured.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.21version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>

        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>4.1.6version>
        dependency>
    dependencies>
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.3version>
                <configuration>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <dependencies>
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.38version>
                    dependency>
                dependencies>
            plugin>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>springloadedartifactId>
                        <version>${spring-loaded.version}version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

MBG配置




<generatorConfiguration>
    
    <properties resource="generatorConfig.properties" />

    <context id="iokindle" targetRuntime="MyBatis3">
        
        <property name="javaFileEncoding" value="UTF-8"/>
        
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />

        
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="${driver}" connectionURL="${url}"
            userId="${username}" password="${password}">
        jdbcConnection>

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

        <javaModelGenerator targetPackage="io.kindle.dao.po"
            targetProject="src/main/java">
            <property name="constructorBased" value="true" />
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        <sqlMapGenerator targetPackage="io.kindle.dao.sqlmap"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="io.kindle.dao.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>

        
        <table tableName="account" domainObjectName="Account" alias="ac">
            
            <columnOverride column="type" 
                javaType="io.kindle.utils.em.AccountType" typeHandler="io.kindle.utils.handler.UniversalEnumHandler" /> 
        table>
        <table tableName="account_user" domainObjectName="AccountUser" alias="au">
        table>
        <table tableName="book" domainObjectName="Book" alias="bk">
        table>
        <table tableName="friend_msg" domainObjectName="FriendMsg" alias="fm">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />   
        table>
        <table tableName="login_log" domainObjectName="LoginLog" alias="ll">
            <columnOverride column="client_type" 
                javaType="io.kindle.utils.em.ClientType" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />   
        table>
        <table tableName="msg_log" domainObjectName="MsgLog" alias="ml">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="account_type" 
                javaType="io.kindle.utils.em.AccountType" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="msg_type" 
                javaType="io.kindle.utils.em.SendMsgType" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />   
        table>
        <table tableName="note" domainObjectName="Note" alias="no">
            <columnOverride column="share_state" 
                javaType="io.kindle.utils.em.ShareState" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />   
        table>
        <table tableName="sys_code" domainObjectName="SysCode" alias="sc">
        table>
        <table tableName="tag" domainObjectName="Tag" alias="tg">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />   
        table>
        <table tableName="user" domainObjectName="User" alias="ur">
        table>
        <table tableName="user_book" domainObjectName="UserBook" alias="ub">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="source" 
                javaType="io.kindle.utils.em.NoteSource" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="user_comment" domainObjectName="UserComment" alias="uc">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="user_friend" domainObjectName="UserFriend" alias="uf">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="user_info" domainObjectName="UserInfo" alias="ui">
            <columnOverride column="gender" 
                javaType="io.kindle.utils.em.Gender" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="education" 
                javaType="io.kindle.utils.em.Education" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="id_type" 
                javaType="io.kindle.utils.em.IdType" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="user_log" domainObjectName="UserLog" alias="ul">
        table>
        <table tableName="user_tag" domainObjectName="UserTag" alias="ut">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="user_note" domainObjectName="UserNote" alias="un">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
            <columnOverride column="source" 
                javaType="io.kindle.utils.em.NoteSource" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
        <table tableName="note_model" domainObjectName="NoteModel" alias="nm">
            <columnOverride column="state" 
                javaType="io.kindle.utils.em.State" 
                typeHandler="io.kindle.utils.handler.UniversalEnumHandler" />
        table>
    context>
generatorConfiguration>

附上我的枚举handler类(资料来自网络, 记不清哪里看得了,感谢大神)

/** 
 *@Project:K2N(Kindle笔记转存)
 *@FileName: BaseEnum.java
 *@Date: 2017年3月16日  
 *@Attention:本内容仅属于banjuer个人所属,禁止外泄以及用于其他的商业目的 
 */
package io.kindle.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;  
import org.apache.ibatis.type.JdbcType;

import io.kindle.utils.em.parent.BaseEnum;

/**
 * @ClassName: UniversalEnumHandler
 * @Description: 枚举万能转换器
 * @author banjuer
 * @date 2017年3月16日 下午11:03:42
 */
public class UniversalEnumHandler<E extends BaseEnum> extends BaseTypeHandler<E> {

    private Class type;
    private E[] enums;

    /** 
     * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现 
     * @param type 配置文件中设置的转换类 
     */  
    public UniversalEnumHandler(Class type) {  
        if (type == null)  
            throw new IllegalArgumentException("Type argument cannot be null");  
        this.type = type;  
        this.enums = type.getEnumConstants();  
        if (this.enums == null)  
            throw new IllegalArgumentException(type.getSimpleName()  
                    + " does not represent an enum type.");  
    }  

    @Override  
    public void setNonNullParameter(PreparedStatement ps, int i, E parameter,  
            JdbcType jdbcType) throws SQLException {  
        //BaseTypeHandler已经帮我们做了parameter的null判断  
        ps.setObject(i,(String)parameter.getValue(), jdbcType.TYPE_CODE);   
    }  

    @Override  
    public E getNullableResult(ResultSet rs, String columnName)  
            throws SQLException {  
        // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型  
        String i = rs.getString(columnName);  
        if (rs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的value值,定位State子类  
            return locateEnumStatus(i);  
        }  
    }  

    @Override  
    public E getNullableResult(ResultSet rs, int columnIndex)  
            throws SQLException {  
        // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型  
        String i = rs.getString(columnIndex);  
        if (rs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的value值,定位State子类  
            return locateEnumStatus(i);  
        }  
    }  

    @Override  
    public E getNullableResult(CallableStatement cs, int columnIndex)  
            throws SQLException {  
        // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型  
        String i = cs.getString(columnIndex);  
        if (cs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的value值,定位State子类  
            return locateEnumStatus(i);  
        }  
    }  

     /** 
     * 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷 
     * @param value 数据库中存储的自定义value属性 
     * @return value对应的枚举类 
     */  
    private E locateEnumStatus(String value) {  
        for(E e : enums) {  
            if(e.getValue().equals(value)) {  
                return e;  
            }  
        }  
        throw new IllegalArgumentException("未知的枚举类型:" + value + ",请核对" + type.getSimpleName());  
    }  

}

问题2:generator工具的使用

对于generator插件的使用, 正确用法是(不是generator-source):
搭建SpringBoot-MyBatis-MyBatisGenerator项目_第1张图片

备注:用mybatis-generator时发现一个小的问题, 当修改generator.xml生成文件位置后, 最好删除原来已生成的相关文件, 有可能resultMap标签所指向的type文件位置并未改变, 此时会报**PO class not found

到此,可以生成dao层相关文件, 进行service层开发

问题3:启动一个spring boot工程

由于是ssm出身, 在使用的时候, 习惯性启动tomcat n次, 可是死活就是进不去controller, 输入url全是404.
痛定思痛仔细读相关资料, 这货类似纯java工程而非web项目, 启动的时候使用main方法, 同理debug…

/** 
 *@Project:K2N(Kindle笔记转存)
 *@FileName: Application.java
 *@Date: 2017年3月18日  
 *@Attention:本内容仅属于banjuer个人所属,禁止外泄以及用于其他的商业目的 
 */
package io.kindle;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/** 
 * @ClassName: Application 
 * @Description: SpringBoot启动器
 * @author banjuer
 * @date 2017年3月18日 下午11:57:47 
 */
@SpringBootApplication 
public class Application {

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

}

怎么debug这货, 也是废了一番周折,在stackoverflow上找到答案, 瞬间感觉自己智商为0…
搭建SpringBoot-MyBatis-MyBatisGenerator项目_第2张图片
因为我以前debug的习惯是启动tomcat啊!!而这货不需要单独服务器部署..

以后开发继续跟进吧…

你可能感兴趣的:(javaweb)