diff --git a/README.md b/README.md index 84dd5e3ffdcbac7009fec8064f386c2eff6681c2..2d83f23b85f2886478664220acd79051edf2e459 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ close=")">#{item} ``` ![例子](https://gitee.com/vonchange/spring-data-mybatis-mini/raw/master/mini.png) -== 新增融合mybatis-spring-boot实现 只简化mybatis动态sql写法和sql写在markdown文件里 +== 新增mybatis-spring-boot实现只简化mybatis动态sql写法和sql写在markdown文件里 + ``` -- 依赖 详情见mybatis-sql-extend-test模块 @@ -44,16 +45,35 @@ close=")">#{item} ``` ``` - public class SimpleLanguageDriver extends XMLLanguageDriver implements LanguageDriver { - @Override +// mybatis plus 扩展 MybatisXMLLanguageDriver 配置 mybatis-plus.configuration.default-scripting-language +public class SimpleLanguageDriver extends XMLLanguageDriver implements LanguageDriver { + @Override public SqlSource createSqlSource(Configuration configuration, String script, Class parameterType) { - String sqlInXml = MybatisSqlLanguageUtil.sqlInXml("mapper",script,new MySQLDialect()); return super.createSqlSource(configuration, sqlInXml, parameterType); } } ``` +``` + @Select("@UserMapper.findList") + List findList(@Param("userName") String userName, + @Param("createTime") LocalDateTime createTime); +``` +> UserMapper.md 文件 +``` +-- findList +select * from user_base +[@sql findListWhereSql] +``` +> sql 片段 +``` +-- findListWhereSql + +[@@and user_name like userName] +[@and create_time < createTime] + +``` ``` -- 配置 mybatis: @@ -110,11 +130,11 @@ mybatis mini in Java: Add the Maven dependency: ``` - + com.vonchange.common spring-data-mybatis-mini - 2.3.8 + ${spring.mybatis.mini} @@ -135,11 +155,11 @@ Add the Maven dependency: ``` - + com.vonchange.common spring-data-mybatis-mini-low - 1.9.6 + ${spring.mybatis.mini} ``` diff --git a/README_en.md b/README_en.md index e1872dbf87bee8c2c886feee8ddf217e83f7cbed..376fe7ead67e8f36b4a766e160d4750b7fcf98f5 100644 --- a/README_en.md +++ b/README_en.md @@ -18,14 +18,62 @@ mybatis) eg: ``` -[@and id in idList] 等于 +[@and id in idList] equal and id in #{item} ``` ![例子](mini.png) +== mybatis-sql-extend extend mybatis-spring-boot only Simplify the +dynamic SQL usage of mybatis and SQL written in markdown file easier to +write and read +``` +-- depend see mybatis-sql-extend-test + + com.vonchange.common + mybatis-sql-extend + ${spring.mybatis.mini} + +``` +``` +public class SimpleLanguageDriver extends XMLLanguageDriver implements LanguageDriver { + @Override + public SqlSource createSqlSource(Configuration configuration, String script, Class parameterType) { + String sqlInXml = MybatisSqlLanguageUtil.sqlInXml("mapper",script,new MySQLDialect()); + return super.createSqlSource(configuration, sqlInXml, parameterType); + } +} +``` +``` + @Select("@UserMapper.findList") + List findList(@Param("userName") String userName, + @Param("createTime") LocalDateTime createTime); +``` +> UserMapper.md +``` +-- findList +select * from user_base +[@sql findListWhereSql] +``` + +> sql +``` +-- findListWhereSql + +[@@and user_name like userName] +[@and create_time < createTime] + +``` +``` +-- config +mybatis: + default-scripting-language-driver: com.vonchange.mybatis.test.config.SimpleLanguageDriver + configuration: + map-underscore-to-camel-case: true + +``` == Getting Started 1. Add the Maven dependency diff --git a/mybatis-sql-extend-test/pom.xml b/mybatis-sql-extend-test/pom.xml index 988910231791200b3004eee5f1d779a8997b9b78..071ed8f1e786ea6f0c4950c832a752949358c6b2 100644 --- a/mybatis-sql-extend-test/pom.xml +++ b/mybatis-sql-extend-test/pom.xml @@ -115,6 +115,14 @@ false + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + org.springframework.boot spring-boot-maven-plugin diff --git a/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/DemoApplication.java b/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/MybatisSqlExtendTestApplication.java similarity index 74% rename from mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/DemoApplication.java rename to mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/MybatisSqlExtendTestApplication.java index 6b4fbef1326cce2ba40a7a7d680e33ec7df5ad49..43189e3705c96481b16d13dd56e3ed516386caf6 100644 --- a/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/DemoApplication.java +++ b/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/MybatisSqlExtendTestApplication.java @@ -6,11 +6,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; //@SpringBootApplication(exclude={JdbcConfiguration.class}) @SpringBootApplication //@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) -public class DemoApplication { +public class MybatisSqlExtendTestApplication { public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(MybatisSqlExtendTestApplication.class, args); } } diff --git a/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/test/config/SimpleLanguageDriver.java b/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/test/config/SimpleLanguageDriver.java index 8722542d8603bc0298677c8c057a21d93c439225..ae0364a8370e45ddccf08386ae948896710036f6 100644 --- a/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/test/config/SimpleLanguageDriver.java +++ b/mybatis-sql-extend-test/src/main/java/com/vonchange/mybatis/test/config/SimpleLanguageDriver.java @@ -8,11 +8,10 @@ import org.apache.ibatis.scripting.LanguageDriver; import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver; import org.apache.ibatis.session.Configuration; +// mybatis plus 扩展 MybatisXMLLanguageDriver 配置 mybatis-plus.configuration.default-scripting-language public class SimpleLanguageDriver extends XMLLanguageDriver implements LanguageDriver { - @Override public SqlSource createSqlSource(Configuration configuration, String script, Class parameterType) { - String sqlInXml = MybatisSqlLanguageUtil.sqlInXml("mapper",script,new MySQLDialect()); return super.createSqlSource(configuration, sqlInXml, parameterType); } diff --git a/mybatis-sql-extend-test/src/main/resources/application-h2.yml b/mybatis-sql-extend-test/src/main/resources/application-h2.yml new file mode 100644 index 0000000000000000000000000000000000000000..5a80f6f01442f2450202cb3621f1a72c94d3ac64 --- /dev/null +++ b/mybatis-sql-extend-test/src/main/resources/application-h2.yml @@ -0,0 +1,22 @@ +spring: + datasource: + hikari: + driver-class-name: org.h2.Driver + #jdbc-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 + jdbc-url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM 'classpath:db-init.sql' + username: root + password: 123456 + +mybatis: + default-scripting-language-driver: com.vonchange.mybatis.test.config.SimpleLanguageDriver + configuration: + map-underscore-to-camel-case: true + + + + + + + + + diff --git a/mybatis-sql-extend-test/src/main/resources/application.yml b/mybatis-sql-extend-test/src/main/resources/application.yml index 431a361a2c60e3bcea466009541b229fd872b37f..8a60ffc99ab152619666d2f6f34f7f2af229b7c4 100644 --- a/mybatis-sql-extend-test/src/main/resources/application.yml +++ b/mybatis-sql-extend-test/src/main/resources/application.yml @@ -1,8 +1,8 @@ spring: profiles: - active: local + active: h2 application: - name: demo + name: mybatis-sql-eztend datasource: hikari: driver-class-name: com.mysql.cj.jdbc.Driver @@ -24,11 +24,6 @@ mybatis: -logback: - aliyun: - endpoint: cn-hangzhou.log.aliyuncs.com - - diff --git a/mybatis-sql-extend-test/src/main/resources/db-init.sql b/mybatis-sql-extend-test/src/main/resources/db-init.sql new file mode 100644 index 0000000000000000000000000000000000000000..4e30b84dc88f3a0511d6d7e26689bab0b170eab0 --- /dev/null +++ b/mybatis-sql-extend-test/src/main/resources/db-init.sql @@ -0,0 +1,19 @@ +SET MODE=MySQL; +SET FOREIGN_KEY_CHECKS=0; +drop table if exists `user_base`; +CREATE TABLE IF NOT EXISTS `user_base` ( + `id` bigint(13) NOT NULL AUTO_INCREMENT COMMENT 'id序列,自增', + `code` varchar(36) DEFAULT NULL COMMENT '编码', + `user_name` varchar(30) DEFAULT NULL COMMENT '用户名', + `mobile_phone` varchar(13) DEFAULT NULL COMMENT '手机号', + `address` varchar(20) DEFAULT NULL COMMENT 'address', + `is_delete` tinyint(1) DEFAULT '0' COMMENT '是否已删除', + `create_time` datetime DEFAULT NULL COMMENT '创建时间', + `update_time` datetime DEFAULT NULL COMMENT '更新时间', + `head_image_data` blob DEFAULT NULL COMMENT '头像', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + insert into user_base(id,code,user_name,mobile_phone,address,create_time) values (1,UUID(),'test','120','xxx',now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('李4','110','xxx额',now()); + insert into user_base(user_name,mobile_phone,address,create_time,update_time) values ('张三日子','911','xxx是啥',now(),now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('test','333','ufo',now()); \ No newline at end of file diff --git a/mybatis-sql-extend-test/src/test/java/com/vonchange/mybatis/test/DemoApplicationTests.java b/mybatis-sql-extend-test/src/test/java/com/vonchange/mybatis/test/DemoApplicationTests.java deleted file mode 100644 index 3ac114bbdbabc631233225ba13bce46fabd1fd20..0000000000000000000000000000000000000000 --- a/mybatis-sql-extend-test/src/test/java/com/vonchange/mybatis/test/DemoApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.vonchange.mybatis.test; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class DemoApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/recommend.md b/recommend.md index af5b114de64f7c8667caec5dbfde98f32704ffbd..75b8829eb1019bf429a7dda4b841158bee6a74b4 100644 --- a/recommend.md +++ b/recommend.md @@ -26,6 +26,58 @@ collection="idList" index="index" item="item" open="(" separator="," close=")">#{item} ``` ![例子](https://gitee.com/vonchange/spring-data-mybatis-mini/raw/master/mini.png) + + + +== 新增mybatis-spring-boot实现只简化mybatis动态sql写法和sql写在markdown文件里 + +``` +-- 依赖 详情见mybatis-sql-extend-test模块 + + com.vonchange.common + mybatis-sql-extend + ${spring.mybatis.mini} + +``` +``` +// mybatis plus 扩展 MybatisXMLLanguageDriver 配置 mybatis-plus.configuration.default-scripting-language +public class SimpleLanguageDriver extends XMLLanguageDriver implements LanguageDriver { + @Override + public SqlSource createSqlSource(Configuration configuration, String script, Class parameterType) { + String sqlInXml = MybatisSqlLanguageUtil.sqlInXml("mapper",script,new MySQLDialect()); + return super.createSqlSource(configuration, sqlInXml, parameterType); + } +} +``` +``` + @Select("@UserMapper.findList") + List findList(@Param("userName") String userName, + @Param("createTime") LocalDateTime createTime); +``` +> UserMapper.md 文件 +``` +-- findList +select * from user_base +[@sql findListWhereSql] +``` + +> sql 片段 +``` +-- findListWhereSql + +[@@and user_name like userName] +[@and create_time < createTime] + +``` +``` +-- 配置 +mybatis: + default-scripting-language-driver: com.vonchange.mybatis.test.config.SimpleLanguageDriver + configuration: + map-underscore-to-camel-case: true + +``` + == why not spring data jdbc,jpa,hibernate,mybaits,mybatis-plus等 1. 基于spring data jdbc理念但扩展使用mybatis动态sql能力 对于复杂点查询支持更好 diff --git a/spring-data-mybatis-mini-low-test/pom.xml b/spring-data-mybatis-mini-low-test/pom.xml index ad8217127417de75ca90ce688a3d1d96fe4e660a..6bd3c62f0bec8338a7db8c9dded4e805f87763e7 100644 --- a/spring-data-mybatis-mini-low-test/pom.xml +++ b/spring-data-mybatis-mini-low-test/pom.xml @@ -144,6 +144,14 @@ false + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + org.springframework.boot spring-boot-maven-plugin diff --git a/spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java b/spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/MybatisMiniLowTestApplication.java similarity index 84% rename from spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java rename to spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/MybatisMiniLowTestApplication.java index 1e990147e1443ca21ca3bc0d066ff1fd0726892c..47b1dd9fd5914a2d8fb7a586768df2e14413d542 100644 --- a/spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java +++ b/spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/MybatisMiniLowTestApplication.java @@ -10,11 +10,11 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; //@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @EnableMybatisMini(basePackages ="com.vonchange.nine.demo.dao") @EnableJpaRepositories(basePackages = "com.vonchange.nine.demo.jpa") -public class DemoApplication { +public class MybatisMiniLowTestApplication { public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(MybatisMiniLowTestApplication.class, args); } } diff --git a/spring-data-mybatis-mini-low-test/src/test/resources/application-test.yml b/spring-data-mybatis-mini-low-test/src/main/resources/application-h2.yml similarity index 91% rename from spring-data-mybatis-mini-low-test/src/test/resources/application-test.yml rename to spring-data-mybatis-mini-low-test/src/main/resources/application-h2.yml index af4a00b68e952f3387e008768db1825cca810a10..f1bd96d77df4defc2d12d9560f888145531dac03 100644 --- a/spring-data-mybatis-mini-low-test/src/test/resources/application-test.yml +++ b/spring-data-mybatis-mini-low-test/src/main/resources/application-h2.yml @@ -13,6 +13,6 @@ spring: hikari: driver-class-name: org.h2.Driver #jdbc-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 - url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM './src/test/resources/db-init.sql' + url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM 'classpath:db-init.sql' username: root password: 123456 diff --git a/spring-data-mybatis-mini-low-test/src/main/resources/application-local.yml b/spring-data-mybatis-mini-low-test/src/main/resources/application-local.yml new file mode 100644 index 0000000000000000000000000000000000000000..abcd8afc50f66c5edc381166e50d7f928da6eaaa --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/main/resources/application-local.yml @@ -0,0 +1,17 @@ +server: + port: 9001 + +mybatis-mini: + isReadAllScopeOpen: false + logRead: true + logWrite: true + logFullSql: true + +spring: + datasource: + hikari: + driver-class-name: com.mysql.cj.jdbc.Driver + jdbc-url: jdbc:mysql://127.0.0.1:3306/test_b?autoReconnect=true&rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT + username: root + password: 123456 +# \ No newline at end of file diff --git a/spring-data-mybatis-mini-low-test/src/main/resources/application.yml b/spring-data-mybatis-mini-low-test/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..85275326cec2bde945f870f22dc87950cbd33529 --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/main/resources/application.yml @@ -0,0 +1,34 @@ +spring: + profiles: + active: h2 + application: + name: demo + datasource: + hikari: + driver-class-name: com.mysql.cj.jdbc.Driver + jdbc-url: jdbc:mysql://127.0.0.1:3306/nine_user?autoReconnect=true&rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT + username: root + password: 123456 + connection-timeout: 20000 + minimum-idle: 5 + maximum-pool-size: 500 + idle-timeout: 60000 + max-lifetime: 600000 + leak-detection-threshold: 20000 + jpa: + database: MySQL + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect + show-sql: true + hibernate: + ddl-auto: none + + +logback: + aliyun: + endpoint: cn-hangzhou.log.aliyuncs.com + + + + + + diff --git a/spring-data-mybatis-mini-low-test/src/main/resources/db-init.sql b/spring-data-mybatis-mini-low-test/src/main/resources/db-init.sql new file mode 100644 index 0000000000000000000000000000000000000000..4e30b84dc88f3a0511d6d7e26689bab0b170eab0 --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/main/resources/db-init.sql @@ -0,0 +1,19 @@ +SET MODE=MySQL; +SET FOREIGN_KEY_CHECKS=0; +drop table if exists `user_base`; +CREATE TABLE IF NOT EXISTS `user_base` ( + `id` bigint(13) NOT NULL AUTO_INCREMENT COMMENT 'id序列,自增', + `code` varchar(36) DEFAULT NULL COMMENT '编码', + `user_name` varchar(30) DEFAULT NULL COMMENT '用户名', + `mobile_phone` varchar(13) DEFAULT NULL COMMENT '手机号', + `address` varchar(20) DEFAULT NULL COMMENT 'address', + `is_delete` tinyint(1) DEFAULT '0' COMMENT '是否已删除', + `create_time` datetime DEFAULT NULL COMMENT '创建时间', + `update_time` datetime DEFAULT NULL COMMENT '更新时间', + `head_image_data` blob DEFAULT NULL COMMENT '头像', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + insert into user_base(id,code,user_name,mobile_phone,address,create_time) values (1,UUID(),'test','120','xxx',now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('李4','110','xxx额',now()); + insert into user_base(user_name,mobile_phone,address,create_time,update_time) values ('张三日子','911','xxx是啥',now(),now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('test','333','ufo',now()); \ No newline at end of file diff --git a/spring-data-mybatis-mini-low-test/src/main/resources/logback-spring.xml b/spring-data-mybatis-mini-low-test/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000000000000000000000000000000000..adbb0447b0d3009155b27418d53bc297d964735d --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/main/resources/logback-spring.xml @@ -0,0 +1,49 @@ + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss SSS}|${appDev}|${appName}|%-5level|%ipandhostname|[%thread]| %logger{50}| %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-mybatis-mini-low-test/src/main/resources/sql/UserBaseRepository.md b/spring-data-mybatis-mini-low-test/src/main/resources/sql/UserBaseRepository.md new file mode 100644 index 0000000000000000000000000000000000000000..2f791bda9ecea009314b5fc13e4f083951a2ad13 --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/main/resources/sql/UserBaseRepository.md @@ -0,0 +1,141 @@ +> 查询用户列表 +``` +-- findListX + +select * from user_base +where user_name = #{userName} +and create_time <= #{createTime} +``` + +``` +-- findListBase +select * from user_base where user_name = #{userName} +``` + + +``` +-- findOne +select * from user_base +where user_name = #{userName} +``` +> 查询用户列表 含sql 片段 + +``` +-- findList +select * from user_base +where [@sql findListWhereSql] +``` + +``` +-- findListVo +select * from user_base +where [@sql findListWhereSql] +``` + +``` +-- findListByBean +select * from user_base + +[@and user_name like param.userName] +[@and user_name like param.userName%] +[@and create_time <= param.createTime] + +``` + + + + +> sql 片段 +``` +-- findListWhereSql +user_name = #{userName} and 1=1 +[@and create_time < createTime] +``` + +> 查询用户名 返回1个字段的情况 比如查询行数等 +``` +-- findUserName +SELECT user_name FROM user_base +WHERE user_name = #{userName} +``` + + +> 根据Id列表查询列表 +``` +-- findListByIdsx +SELECT * FROM user_base + + and user_name <> #{userName} + and id in #{item} + and create_time < #{createTime} + + +``` + +> [@and create_time < createTime] + +> 根据Id列表查询列表 简写if 和in查询 可混用 +``` +-- findListByIds +SELECT * FROM user_base + +[@and id in #{idList:in} and user_name like #{userName:like}] +[@and user_name like userName%] +[@and id in idList] + and create_time < #{createTime} + +``` + +> 更新方法 update 开头 + +``` +-- updateIsDelete +update user_base set is_delete = #{isDelete} where id =#{id} +``` + +``` +-- batchUpdate +update user_base set is_delete = IFNULL(#{isDelete},is_delete),user_name =#{userName} where id =#{id} +``` + + +``` +-- batchInsert +insert into user_base(`user_name`,`mobile_phone`,create_time) values +(#{userName},#{mobilePhone},#{createTime}) +``` + +``` +-- updateTest + +insert into user_base(`user_name`,`mobile_phone`) values (#{item.userName},#{item.firstPhone}); + + +``` + +``` +-- insertBatchNormal +insert into user_base(`user_name`,`mobile_phone`,create_time) values + +(#{item.userName},#{item.mobilePhone},#{item.createTime}) + + +``` + +``` +-- insertBatchNormalX +insert into user_base(`id`,`code`,`user_name`,`mobile_phone`,`is_delete`,`create_time`,`update_time`,`head_image_data`) values + +(IFNULL(#{item.id},`id`),IFNULL(#{item.code},`code`),IFNULL(#{item.userName},`user_name`),IFNULL(#{item.mobilePhone},`mobile_phone`) +,IFNULL(#{item.isDelete},`is_delete`),IFNULL(#{item.createTime},now()),IFNULL(#{item.updateTime},now()),IFNULL(#{item.headImageData},`head_image_data`)) + +``` + + +``` +-- findBigData +select * from user_base + +[@and user_name like userName] + +``` \ No newline at end of file diff --git a/spring-data-mybatis-mini-low-test/src/test/resources/application-dev.yml b/spring-data-mybatis-mini-low-test/src/test/resources/application-dev.yml deleted file mode 100644 index 8b137891791fe96927ad78e64b0aad7bded08bdc..0000000000000000000000000000000000000000 --- a/spring-data-mybatis-mini-low-test/src/test/resources/application-dev.yml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/spring-data-mybatis-mini-low-test/src/test/resources/application-h2.yml b/spring-data-mybatis-mini-low-test/src/test/resources/application-h2.yml new file mode 100644 index 0000000000000000000000000000000000000000..f1bd96d77df4defc2d12d9560f888145531dac03 --- /dev/null +++ b/spring-data-mybatis-mini-low-test/src/test/resources/application-h2.yml @@ -0,0 +1,18 @@ +server: + port: 9001 + + +mybatis-mini: + dialect: com.vonchange.nine.demo.config.H2Dialect + logRead: true + logWrite: true + logFullSql: true + +spring: + datasource: + hikari: + driver-class-name: org.h2.Driver + #jdbc-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 + url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM 'classpath:db-init.sql' + username: root + password: 123456 diff --git a/spring-data-mybatis-mini-low-test/src/test/resources/application.yml b/spring-data-mybatis-mini-low-test/src/test/resources/application.yml index 466b6eabc64d622c34b1d42f1197b39ff0bc4610..85275326cec2bde945f870f22dc87950cbd33529 100644 --- a/spring-data-mybatis-mini-low-test/src/test/resources/application.yml +++ b/spring-data-mybatis-mini-low-test/src/test/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: test + active: h2 application: name: demo datasource: diff --git a/spring-data-mybatis-mini-low-test/src/test/resources/logback-spring.xml b/spring-data-mybatis-mini-low-test/src/test/resources/logback-spring.xml index 64951a56ed0adae9afefedc809d82f714c775ee4..adbb0447b0d3009155b27418d53bc297d964735d 100644 --- a/spring-data-mybatis-mini-low-test/src/test/resources/logback-spring.xml +++ b/spring-data-mybatis-mini-low-test/src/test/resources/logback-spring.xml @@ -23,7 +23,7 @@ - + diff --git a/spring-data-mybatis-mini-test/pom.xml b/spring-data-mybatis-mini-test/pom.xml index e8d539564aecf77fcc436327ea1171362b4b2d59..266318f92ca183ed3cc95cdd1ea4c9260fcba6f5 100644 --- a/spring-data-mybatis-mini-test/pom.xml +++ b/spring-data-mybatis-mini-test/pom.xml @@ -143,13 +143,23 @@ false + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + org.springframework.boot spring-boot-maven-plugin - app + + + diff --git a/spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java b/spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/MybatisMiniTestApplication.java similarity index 85% rename from spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java rename to spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/MybatisMiniTestApplication.java index 1e990147e1443ca21ca3bc0d066ff1fd0726892c..f95405ca9fd75528a116ce75d4a1caeb842796a4 100644 --- a/spring-data-mybatis-mini-low-test/src/main/java/com/vonchange/nine/demo/DemoApplication.java +++ b/spring-data-mybatis-mini-test/src/main/java/com/vonchange/nine/demo/MybatisMiniTestApplication.java @@ -10,11 +10,11 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; //@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @EnableMybatisMini(basePackages ="com.vonchange.nine.demo.dao") @EnableJpaRepositories(basePackages = "com.vonchange.nine.demo.jpa") -public class DemoApplication { +public class MybatisMiniTestApplication { public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(MybatisMiniTestApplication.class, args); } } diff --git a/spring-data-mybatis-mini-test/src/main/resources/application-dev.yml b/spring-data-mybatis-mini-test/src/main/resources/application-dev.yml deleted file mode 100644 index 8b137891791fe96927ad78e64b0aad7bded08bdc..0000000000000000000000000000000000000000 --- a/spring-data-mybatis-mini-test/src/main/resources/application-dev.yml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/spring-data-mybatis-mini-test/src/test/resources/application-test.yml b/spring-data-mybatis-mini-test/src/main/resources/application-h2.yml similarity index 90% rename from spring-data-mybatis-mini-test/src/test/resources/application-test.yml rename to spring-data-mybatis-mini-test/src/main/resources/application-h2.yml index 344248389a4d94cdb7cee4f98870d29906f1451a..1b3bcb09937b9a7fcb38e3cd482a5a59e08e0e95 100644 --- a/spring-data-mybatis-mini-test/src/test/resources/application-test.yml +++ b/spring-data-mybatis-mini-test/src/main/resources/application-h2.yml @@ -13,6 +13,6 @@ spring: hikari: driver-class-name: org.h2.Driver #jdbc-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 - jdbc-url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM './src/test/resources/db-init.sql' + jdbc-url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM 'classpath:db-init.sql' username: root password: 123456 diff --git a/spring-data-mybatis-mini-test/src/main/resources/application-local.yml b/spring-data-mybatis-mini-test/src/main/resources/application-local.yml index 3945d2240403d8380d5d214e2a3a13320f59c76a..abcd8afc50f66c5edc381166e50d7f928da6eaaa 100644 --- a/spring-data-mybatis-mini-test/src/main/resources/application-local.yml +++ b/spring-data-mybatis-mini-test/src/main/resources/application-local.yml @@ -2,7 +2,6 @@ server: port: 9001 mybatis-mini: - isReadExcludePrimary: false isReadAllScopeOpen: false logRead: true logWrite: true diff --git a/spring-data-mybatis-mini-test/src/main/resources/application.yml b/spring-data-mybatis-mini-test/src/main/resources/application.yml index 107711e2be248bed96e53b053cc4146f9694d938..85275326cec2bde945f870f22dc87950cbd33529 100644 --- a/spring-data-mybatis-mini-test/src/main/resources/application.yml +++ b/spring-data-mybatis-mini-test/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: local + active: h2 application: name: demo datasource: @@ -15,7 +15,12 @@ spring: idle-timeout: 60000 max-lifetime: 600000 leak-detection-threshold: 20000 - + jpa: + database: MySQL + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect + show-sql: true + hibernate: + ddl-auto: none logback: @@ -26,3 +31,4 @@ logback: + diff --git a/spring-data-mybatis-mini-test/src/main/resources/db-init.sql b/spring-data-mybatis-mini-test/src/main/resources/db-init.sql new file mode 100644 index 0000000000000000000000000000000000000000..4e30b84dc88f3a0511d6d7e26689bab0b170eab0 --- /dev/null +++ b/spring-data-mybatis-mini-test/src/main/resources/db-init.sql @@ -0,0 +1,19 @@ +SET MODE=MySQL; +SET FOREIGN_KEY_CHECKS=0; +drop table if exists `user_base`; +CREATE TABLE IF NOT EXISTS `user_base` ( + `id` bigint(13) NOT NULL AUTO_INCREMENT COMMENT 'id序列,自增', + `code` varchar(36) DEFAULT NULL COMMENT '编码', + `user_name` varchar(30) DEFAULT NULL COMMENT '用户名', + `mobile_phone` varchar(13) DEFAULT NULL COMMENT '手机号', + `address` varchar(20) DEFAULT NULL COMMENT 'address', + `is_delete` tinyint(1) DEFAULT '0' COMMENT '是否已删除', + `create_time` datetime DEFAULT NULL COMMENT '创建时间', + `update_time` datetime DEFAULT NULL COMMENT '更新时间', + `head_image_data` blob DEFAULT NULL COMMENT '头像', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + insert into user_base(id,code,user_name,mobile_phone,address,create_time) values (1,UUID(),'test','120','xxx',now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('李4','110','xxx额',now()); + insert into user_base(user_name,mobile_phone,address,create_time,update_time) values ('张三日子','911','xxx是啥',now(),now()); + insert into user_base(user_name,mobile_phone,address,create_time) values ('test','333','ufo',now()); \ No newline at end of file diff --git a/spring-data-mybatis-mini-test/src/main/resources/logback-spring.xml b/spring-data-mybatis-mini-test/src/main/resources/logback-spring.xml index 64951a56ed0adae9afefedc809d82f714c775ee4..50d9060adaeebafb51f3e23090dd843927cff37e 100644 --- a/spring-data-mybatis-mini-test/src/main/resources/logback-spring.xml +++ b/spring-data-mybatis-mini-test/src/main/resources/logback-spring.xml @@ -15,7 +15,7 @@ - + @@ -23,7 +23,7 @@ - + diff --git a/spring-data-mybatis-mini-test/src/main/resources/sql/UserBaseRepository.md b/spring-data-mybatis-mini-test/src/main/resources/sql/UserBaseRepository.md index 67fc87adfbcb2cdc0ee5291bf1498b2c3c8e242e..1ff44fa7b44bf98580a28cff18b66f4de786a45b 100644 --- a/spring-data-mybatis-mini-test/src/main/resources/sql/UserBaseRepository.md +++ b/spring-data-mybatis-mini-test/src/main/resources/sql/UserBaseRepository.md @@ -7,14 +7,48 @@ where user_name = #{userName} and create_time <= #{createTime} ``` +``` +-- findListBase +select * from user_base where user_name = #{userName} +``` + + +``` +-- findOne +select * from user_base +where user_name = #{userName} +``` > 查询用户列表 含sql 片段 ``` -- findList select * from user_base +where +user_name = #{userName} +[@@and is_delete = isDelete.value] +[@@and create_time < createTime] + +``` + +``` +-- findListVo +select * from user_base where [@sql findListWhereSql] ``` +``` +-- findListByBean +select * from user_base + +[@@and user_name like param.userName] +[@and user_name like param.userName%] +[@and create_time <= param.createTime] + +``` + + + + > sql 片段 ``` -- findListWhereSql @@ -25,14 +59,14 @@ user_name = #{userName} and 1=1 > 查询用户名 返回1个字段的情况 比如查询行数等 ``` -- findUserName -SELECT first_phone FROM user_base +SELECT user_name FROM user_base WHERE user_name = #{userName} ``` > 根据Id列表查询列表 ``` --- findListByIds +-- findListByIdsx SELECT * FROM user_base and user_name <> #{userName} @@ -42,11 +76,14 @@ SELECT * FROM user_base ``` +> [@and create_time < createTime] + > 根据Id列表查询列表 简写if 和in查询 可混用 ``` -- findListByIds SELECT * FROM user_base +[@@and id in #{idList:in} and user_name like #{userName:like}] [@and user_name like userName%] [@and id in idList] and create_time < #{createTime} @@ -65,10 +102,59 @@ update user_base set is_delete = #{isDelete} where id =#{id} update user_base set is_delete = IFNULL(#{isDelete},is_delete),user_name =#{userName} where id =#{id} ``` + +``` +-- batchInsert +insert into user_base(`user_name`,`mobile_phone`,create_time) values +(#{userName},#{mobilePhone},#{createTime}) +``` + ``` -- updateTest -insert into user_base(`user_name`,`first_phone`) values (#{item.userName},#{item.firstPhone}); +insert into user_base(`user_name`,`mobile_phone`) values (#{item.userName},#{item.firstPhone}); +``` + +``` +-- insertBatchNormal +insert into user_base(`user_name`,`mobile_phone`,create_time) values + +(#{item.userName},#{item.mobilePhone},#{item.createTime}) + + +``` + +``` +-- insertBatchNormalX +insert into user_base(`id`,`code`,`user_name`,`mobile_phone`,`is_delete`,`create_time`,`update_time`,`head_image_data`) values + +(IFNULL(#{item.id},`id`),IFNULL(#{item.code},`code`),IFNULL(#{item.userName},`user_name`),IFNULL(#{item.mobilePhone},`mobile_phone`) +,IFNULL(#{item.isDelete},`is_delete`),IFNULL(#{item.createTime},now()),IFNULL(#{item.updateTime},now()),IFNULL(#{item.headImageData},`head_image_data`)) + +``` + + +``` +-- findBigData +select * from user_base + +[@and user_name like userName] + +``` + + +``` +-- findLongList +select id from user_base +``` + +``` +-- findInList + +select * from user_base +where 1=1 +[@@and user_name in userNames] +[@@and is_delete in isDeletes] ``` \ No newline at end of file diff --git a/spring-data-mybatis-mini-test/src/test/java/com/vonchange/nine/demo/DemoApplicationTests.java b/spring-data-mybatis-mini-test/src/test/java/com/vonchange/nine/demo/DemoApplicationTests.java deleted file mode 100644 index 11e13592f32043ae426c78a02e6709ad824c67da..0000000000000000000000000000000000000000 --- a/spring-data-mybatis-mini-test/src/test/java/com/vonchange/nine/demo/DemoApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.vonchange.nine.demo; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class DemoApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/spring-data-mybatis-mini-test/src/test/resources/application-dev.yml b/spring-data-mybatis-mini-test/src/test/resources/application-dev.yml deleted file mode 100644 index 8b137891791fe96927ad78e64b0aad7bded08bdc..0000000000000000000000000000000000000000 --- a/spring-data-mybatis-mini-test/src/test/resources/application-dev.yml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/spring-data-mybatis-mini-test/src/main/resources/application-test.yml b/spring-data-mybatis-mini-test/src/test/resources/application-h2.yml similarity index 66% rename from spring-data-mybatis-mini-test/src/main/resources/application-test.yml rename to spring-data-mybatis-mini-test/src/test/resources/application-h2.yml index 38ffe5f88e55fbb1e816296688bcd349e7588547..1b3bcb09937b9a7fcb38e3cd482a5a59e08e0e95 100644 --- a/spring-data-mybatis-mini-test/src/main/resources/application-test.yml +++ b/spring-data-mybatis-mini-test/src/test/resources/application-h2.yml @@ -1,14 +1,18 @@ server: port: 9001 + mybatis-mini: - isReadExcludePrimary: false + dialect: com.vonchange.nine.demo.config.H2Dialect + logRead: true + logWrite: true + logFullSql: true spring: datasource: hikari: driver-class-name: org.h2.Driver #jdbc-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 - jdbc-url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM './src/test/resources/db-init.sql' + jdbc-url: jdbc:h2:mem:db_users;MODE=MYSQL;INIT=RUNSCRIPT FROM 'classpath:db-init.sql' username: root password: 123456 diff --git a/spring-data-mybatis-mini-test/src/test/resources/application.yml b/spring-data-mybatis-mini-test/src/test/resources/application.yml index b37e08536d7980ede1b38c2d5e38fd31ffd9e424..85275326cec2bde945f870f22dc87950cbd33529 100644 --- a/spring-data-mybatis-mini-test/src/test/resources/application.yml +++ b/spring-data-mybatis-mini-test/src/test/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: local + active: h2 application: name: demo datasource: diff --git a/spring-data-mybatis-mini-test/src/test/resources/logback-spring.xml b/spring-data-mybatis-mini-test/src/test/resources/logback-spring.xml index 41e98d2dda51e97dd9ff7a19ee572d040820f837..50d9060adaeebafb51f3e23090dd843927cff37e 100644 --- a/spring-data-mybatis-mini-test/src/test/resources/logback-spring.xml +++ b/spring-data-mybatis-mini-test/src/test/resources/logback-spring.xml @@ -23,7 +23,7 @@ - +