`

springboot 集成mybaties

阅读更多
前情: 项目DB访问主要是JPA。 但因为前期需求的不确定等原因,导致entity的关联关系等不确定。 明显感觉JPA在多条件查询方面要弱于mybaties。所以集成mybaties到项目, 数据访问层,混用JPA和mybaties。 其中mybaties主要用于特殊查询:

1.pom依赖:
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>

<!-- mybatis pagehelper -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.1.6</version>
</dependency>

<!-- mybatis generator -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
</plugin>


2.代码生成配置(resources目录下):
<?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">

<!-- 使用 mvn mybatis-generator:generate -->
<generatorConfiguration>
    <properties resource="application-dev.yml"/>
	<classPathEntry location="E://maven_repository/mysql/mysql-connector-java/5.1.42/mysql-connector-java-5.1.42.jar" />
    <context id="tables" targetRuntime="MyBatis3" defaultModelType="flat">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
	        connectionURL="jdbc:mysql://192.168.0.103:3306/db_petstore?useUnicode=yes&amp;characterEncoding=UTF-8"
	        userId="xxx"
	        password="xxx">
        </jdbcConnection>
        <javaModelGenerator targetPackage="api.xxx.com.petstore.vo"
                            targetProject="src/main/java">
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="sqlMapperXml"
                         targetProject="src/main/resources">
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="api.xxx.com.petstore.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

		<!-- schema:指定dao文件所属分类文件夹| doaminObjectName:指定生成对象名  -->
        <table schema="" tableName="sales_order" domainObjectName="SalesOrder" selectByExampleQueryId="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false"
               enableSelectByExample="false">
            <generatedKey column="id" sqlStatement="assigned" identity="true"/>
        </table>
    </context>
</generatorConfiguration>


3. mybatis配置
#mybatis
mybatis:
  type-aliases-package: api.qooco.com.petstore.entity
  mapper-locations: classpath*:/sqlMapperXml/*.xml
  configuration:
    map-underscore-to-camel-case: true
    use-generated-keys: true
    default-fetch-size: 100
    default-statement-timeout: 25000
    cache-enabled: true
    aggressive-lazy-loading: true
    lazy-loading-enabled: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl


4.分页配置:
@Configuration
@EnableJpaAuditing
@EnableTransactionManagement
@EnableSpringDataWebSupport
@MapperScan("api.xxx.com.petstore.mapper")
public class AppConfig {

    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("dialect", "mysql");
        p.setProperty("supportMethodsArguments", "true");
        p.setProperty("autoRuntimeDialect", "true");
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        p.setProperty("returnPageInfo", "always");
        p.setProperty("params", "count=countSql");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics