Skip to content

SpringBoot and Mybatis

Baoying Wang edited this page Feb 6, 2019 · 6 revisions

DB connection configuration

Same with others, e.g. hibernate/ JdbcType template, etc

Mybatis files

Mybatis包含一些自己概念,这里可以与Spring做一些映射

  1. 声明@MapperScan而是的mybatis的mapper被Spring识别 @MapperScan(basePackages = {"com.xxx.dao", "com.yyy.dao"})

btw:Mapper - 可以是XML+Interface(without sql),也可以是Annotation+ SQL (without xml),两者还可以混用。 对于xml情况,需要在Spring中配置mapper xml位置即可 mybatis.mapper-locations=classpath:/xxx/mapper/*.xml

  1. Entity -在xml中,有Entity类的定义,通过Aliase可以短一些。Spring里这样配置 #use aliase to short name in xml. note: xml mapper and annotation can work together mybatis.type-aliases-package=com.xxx.hubdb.entity

这里一个例子/网上Mybatis的例子不计其数,这里这是简单提一下

@SpringBootApplication
@ComponentScan(basePackages={"com.xxx"})
@EnableScheduling
@Configuration
@MapperScan(basePackages = {"com.xxx.dao", "com.yyy.dao"})
@EnableTransactionManagement
public class Application {

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

//一个Annotation Mapper(记得把package目录假如Mapperscan的列表中)
public interface AddressDAO {

    @Select(value="select * from ${addressDataTableName} " +
            " where addr=#{address}")
    Address getAddressByAddress(@Param("addressDataTableName") String tableName,
                                         @Param("address") String mainAddress);
}

获取db生成的key/自增主键

  • 在insert方法加上@Options(useGeneratedKeys = true)
  • insert后的Entity的id那个field就是其id
  • 注意insert的返回值不是其id,而是影响(插入)了多少行

https://stackoverflow.com/questions/12241260/get-auto-genearated-key-for-the-inserted-record-in-mybatis 譬如

Mapper/DAO
    String insert="    insert into addr_task (id, s_applyaddr_message_id, track_request_id, " +
            "      tx_hash, block_hash, block_height ";
    @Insert(insert)
    @Options(useGeneratedKeys = true)
    int insert(SomeMessage message);

调用者
    @Autowired
    private YourDAO dao;

    public void yourMethod(){
        SomeMessage message = new SomeMessage();
        //set some value except id
        dao.insert(message); //注意,这个insert方法的返回值一般为1,表示插入了一条记录。这个不是!不是!不是!插入记录的id。
        
        int idOfInsertedRecord = message.getId();
    }

other configurations 其他配置

参数列表,见官方网站 http://www.mybatis.org/mybatis-3/configuration.html

print mybatis debug logs / 打印debug logs

打印mybatis的sql - 把mapper所在的package配置为DEBUG logging.level.com.xxx.dao=DEBUG https://stackoverflow.com/questions/41001188/spring-boot-with-spring-mybatis-how-to-force-it-to-logging-all-sql-queries

Other configurations

mybatis.cacheEnabled=false mybatis.localCacheScope=STATEMENT #其实我没太明白。但是运行时,一些query可能返回过期结果,所以我总是把这个关掉。

#自动映射时,数据库一般以下划线间隔,通过下面这个配置可以自动将下划线字段名映射到驼峰命名的字段。而不必自己再写一遍映射 mybatis.configuration.mapUnderscoreToCamelCase=true

create mapper etc

参考这个项目,关于使用MBG - https://github.com/cjbi/wetech-admin . 这个项目还使用了其他技术(如shiro,hibernate validator等)用于其用户资源管理。

create a Gradle file like below and run with "gradle -b build.mybatis.entity.gradle mbGenerator"


buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
        //添加maven仓库 mybatis-generetor
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

        // mybatis-generator 插件路径mybatis-generetor
        classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
    }
}
//配置从阿里云源下载依赖
allprojects {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
//引入 mybatis-generator 插件mybatis-generetor
apply plugin: "com.arenagod.gradle.MybatisGenerator"

group = 'com.swpu'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    //这里需要使用 MyBatis Generator,MySQL 驱动,以及 MyBatis Mapper.
    //由于代码生成单独运行即可,不需要参与到整个项目的编译,因此在 build.gradle 中添加配置:
    mybatisGenerator
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')

    // mybatis
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')

    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.45'

    // https://mvnrepository.com/artifact/com.alibaba/fastjson
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'

    //mybatis-geerator
    // https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core
    compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.6'

}


//this task is to create entity java file
mybatisGenerator {
    verbose = true
    //configFile = '/Users/wangbaoying/ws/xxx/src/main/resources/mybatis-generator.xml'
    configFile = '/Users/wangbaoying/ws/xxx/src/main/resources/mybatis-generator.xml'
}

An example of maybatis-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="DB2Tables"    targetRuntime="MyBatis3">
    <commentGenerator>
      <property name="suppressDate" value="true"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/sidechain_hub?characterEncoding=utf8" userId="root" password="Blockchain@123">
    </jdbcConnection>

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

    <javaModelGenerator targetPackage="com.xxx.entity" targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
      <property name="trimStrings" value="true"/>
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/not_used">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.mapper" targetProject="src/main/resources/not_used">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>

    <table tableName="sidechain_applyaddr_message" domainObjectName="SideChainApplyAddrMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="sidechain_assignaddr_task" domainObjectName="SideChainAssignAddrTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="sidechain_assignaddr_address" domainObjectName="SideChainAssignAddrAddress" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>



    <table tableName="mainchain_deposit_message" domainObjectName="MainDepositMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="sidechain_issue_task" domainObjectName="SideChainIssueTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="side_chain_retire_message" domainObjectName="SideChainRetireMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="mainchain_withdraw_task" domainObjectName="MainChainWithdrawTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="sidechain_feedback_task" domainObjectName="SideChainFeedbackTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    <table tableName="sidechain_rollback_task" domainObjectName="SideChainRollbackTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>


    <table tableName="task_option_record" domainObjectName="TaskOptionRecord" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>


  </context>

</generatorConfiguration>

Clone this wiki locally