├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── readme └── init.sql └── src └── main ├── java └── com │ └── zhushoumao │ └── springboot │ ├── Application.java │ ├── config │ ├── DruidDataSourceConfig.java │ ├── DruidStatFilter.java │ ├── DruidStatViewServlet.java │ ├── MyBatisConfig.java │ └── SwaggerConfig.java │ ├── controller │ └── TestController.java │ ├── mapper │ └── CityMapper.java │ ├── pojo │ ├── City.java │ └── CityExample.java │ ├── service │ ├── CityService.java │ └── impl │ │ └── CityServiceImpl.java │ └── utils │ └── CityShardStrategyImpl.java └── resources ├── application.yml ├── com └── zhushoumao │ └── springboot │ └── mapper │ └── CityMapper.xml └── sharding_config.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # springBoot-swagger-mybatis-shardbatis 2 | springBoot-swagger-mybatis-shardbatis 3 | ##springBoot整合swagger,mybatis,druid,shardbatis,pageHelper 4 | 项目中用到的分表插件地址: https://github.com/puhaiyang/shardbatis.git 5 | 即以下依赖项目: 6 | 7 | 8 | org.shardbatis 9 | shardbatis 10 | 2.0.0B-NEW 11 | 12 | 13 | #部署安成后测试链接: 14 | http://localhost/swagger-ui.html 15 | http://localhost/druid/ 16 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.zhushoumao.springboot 5 | springBoot-swagger-mybatis-shardbatis 6 | 0.0.1-SNAPSHOT 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 1.4.0.RELEASE 11 | 12 | 13 | 3.3.1 14 | 1.2.4 15 | 4.1.1 16 | UTF-8 17 | src/main/java/ 18 | src/main/resources/ 19 | 20 | 21 | 22 | junit 23 | junit 24 | test 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-jdbc 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-aop 38 | 39 | 40 | 41 | org.mybatis 42 | mybatis 43 | ${mybatis.version} 44 | 45 | 46 | org.mybatis 47 | mybatis-spring 48 | ${mybatis.spring.version} 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-devtools 54 | true 55 | 56 | 57 | 58 | io.springfox 59 | springfox-swagger-ui 60 | 2.2.2 61 | 62 | 63 | 64 | io.springfox 65 | springfox-swagger2 66 | 2.2.2 67 | 68 | 69 | 70 | 71 | com.alibaba 72 | druid 73 | 1.0.26 74 | 75 | 76 | 77 | mysql 78 | mysql-connector-java 79 | 80 | 81 | 82 | com.github.pagehelper 83 | pagehelper 84 | ${pagehelper.version} 85 | 86 | 87 | 88 | org.shardbatis 89 | shardbatis 90 | 2.0.0B-NEW 91 | 92 | 93 | 94 | springboot 95 | 96 | 97 | org.mybatis.generator 98 | mybatis-generator-maven-plugin 99 | 1.3.5 100 | 101 | 102 | 103 | mysql 104 | mysql-connector-java 105 | 5.0.5 106 | 107 | 108 | org.mybatis 109 | mybatis-spring 110 | 1.2.2 111 | 112 | 113 | 114 | org.mybatis 115 | mybatis 116 | 3.2.4 117 | 118 | 119 | 120 | 121 | 122 | 123 | Generate MyBatis Artifacts 124 | package 125 | 126 | generate 127 | 128 | 129 | 130 | 131 | 132 | true 133 | 134 | true 135 | 136 | 137 | src/main/resources/mybatis-generator.xml 138 | 139 | 140 | 141 | maven-compiler-plugin 142 | 143 | UTF-8 144 | 1.7 145 | 1.7 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /readme/init.sql: -------------------------------------------------------------------------------- 1 | SET FOREIGN_KEY_CHECKS=0; 2 | 3 | -- ---------------------------- 4 | -- Table structure for city 5 | -- ---------------------------- 6 | DROP TABLE IF EXISTS `city`; 7 | CREATE TABLE `city` ( 8 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 9 | `name` varchar(255) DEFAULT NULL, 10 | `state` varchar(255) DEFAULT NULL, 11 | PRIMARY KEY (`id`) 12 | ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='市级信息'; 13 | 14 | -- ---------------------------- 15 | -- Records of city 16 | -- ---------------------------- 17 | INSERT INTO `city` VALUES ('1', '石家庄', '河北'); 18 | INSERT INTO `city` VALUES ('2', '邯郸', '河北'); 19 | INSERT INTO `city` VALUES ('10', '哈哈', '哈哈哈'); 20 | INSERT INTO `city` VALUES ('11', '嘻嘻', '嘻嘻嘻嘻嘻嘻'); 21 | INSERT INTO `city` VALUES ('12', '呵呵', '呵呵呵呵呵呵'); 22 | INSERT INTO `city` VALUES ('13', '嚯嚯', '嚯嚯嚯嚯嚯嚯'); 23 | 24 | 25 | -- ---------------------------- 26 | -- Table structure for city_2015 27 | -- ---------------------------- 28 | DROP TABLE IF EXISTS `city_2015`; 29 | CREATE TABLE `city_2015` ( 30 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 31 | `name` varchar(255) DEFAULT NULL, 32 | `state` varchar(255) DEFAULT NULL, 33 | PRIMARY KEY (`id`) 34 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='市级信息'; 35 | 36 | -- ---------------------------- 37 | -- Records of city_2015 38 | -- ---------------------------- 39 | INSERT INTO `city_2015` VALUES ('3', '仨仨', '仨仨仨仨'); 40 | INSERT INTO `city_2015` VALUES ('4', '四四', '四四四四'); 41 | 42 | 43 | -- ---------------------------- 44 | -- Table structure for city_2016 45 | -- ---------------------------- 46 | DROP TABLE IF EXISTS `city_2016`; 47 | CREATE TABLE `city_2016` ( 48 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 49 | `name` varchar(255) DEFAULT NULL, 50 | `state` varchar(255) DEFAULT NULL, 51 | PRIMARY KEY (`id`) 52 | ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='市级信息'; 53 | 54 | -- ---------------------------- 55 | -- Records of city_2016 56 | -- ---------------------------- 57 | INSERT INTO `city_2016` VALUES ('5', '五五', '五五五五'); 58 | INSERT INTO `city_2016` VALUES ('6', '六六', '六六六六'); 59 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot; 2 | 3 | import javax.servlet.MultipartConfigElement; 4 | 5 | import org.mybatis.spring.annotation.MapperScan; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.boot.web.servlet.MultipartConfigFactory; 9 | import org.springframework.boot.web.servlet.ServletComponentScan; 10 | import org.springframework.boot.web.support.SpringBootServletInitializer; 11 | import org.springframework.context.annotation.Bean; 12 | 13 | @MapperScan("com.zhushoumao.springboot.mapper") 14 | @SpringBootApplication 15 | @ServletComponentScan 16 | public class Application extends SpringBootServletInitializer { 17 | @Bean 18 | public MultipartConfigElement multipartConfigElement() { 19 | MultipartConfigFactory factory = new MultipartConfigFactory(); 20 | factory.setMaxFileSize("5MB"); 21 | factory.setMaxRequestSize("5MB"); 22 | return factory.createMultipartConfig(); 23 | } 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(Application.class); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/config/DruidDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.config; 2 | 3 | import java.sql.SQLException; 4 | 5 | import javax.sql.DataSource; 6 | 7 | import org.springframework.boot.bind.RelaxedPropertyResolver; 8 | import org.springframework.context.EnvironmentAware; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.core.env.Environment; 12 | import org.springframework.transaction.annotation.EnableTransactionManagement; 13 | 14 | import com.alibaba.druid.pool.DruidDataSource; 15 | @Configuration 16 | @EnableTransactionManagement 17 | /** 18 | * 19 | * Title: Druid的DataResource配置类
20 | * Description: (用一句话描述该文件做什么)
21 | * ClassName: DruidDataSourceConfig
22 | * author: puhaiyang
23 | * date: 2017年1月13日 下午5:38:50
24 | * version: V1.0
25 | * 26 | */ 27 | public class DruidDataSourceConfig implements EnvironmentAware { 28 | 29 | private RelaxedPropertyResolver propertyResolver; 30 | 31 | public void setEnvironment(Environment env) { 32 | this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource."); 33 | } 34 | 35 | @Bean 36 | public DataSource dataSource() { 37 | DruidDataSource datasource = new DruidDataSource(); 38 | datasource.setUrl(propertyResolver.getProperty("url")); 39 | datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name")); 40 | datasource.setUsername(propertyResolver.getProperty("username")); 41 | datasource.setPassword(propertyResolver.getProperty("password")); 42 | datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initial-size"))); 43 | datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("min-idle"))); 44 | datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("max-wait"))); 45 | datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("max-active"))); 46 | datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty("min-evictable-idle-time-millis"))); 47 | try { 48 | datasource.setFilters("stat,wall"); 49 | } catch (SQLException e) { 50 | e.printStackTrace(); 51 | } 52 | return datasource; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/config/DruidStatFilter.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.config; 2 | 3 | import javax.servlet.annotation.WebFilter; 4 | import javax.servlet.annotation.WebInitParam; 5 | 6 | import com.alibaba.druid.support.http.WebStatFilter; 7 | 8 | /** 9 | * 10 | * Title: Druid拦截器,用于查看Druid监控
11 | * Description: (用一句话描述该文件做什么)
12 | * ClassName: DruidStatFilter
13 | * author: puhaiyang
14 | * date: 2017年1月13日 下午5:39:07
15 | * version: V1.0
16 | * 17 | */ 18 | @WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { 19 | @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源 20 | }) 21 | public class DruidStatFilter extends WebStatFilter { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/config/DruidStatViewServlet.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.config; 2 | 3 | import javax.servlet.annotation.WebInitParam; 4 | import javax.servlet.annotation.WebServlet; 5 | 6 | import com.alibaba.druid.support.http.StatViewServlet; 7 | 8 | /** 9 | * Druid的Servlet 10 | * Title:
11 | * Description: (用一句话描述该文件做什么)
12 | * ClassName: DruidStatViewServlet
13 | * author: puhaiyang
14 | * date: 2017年1月13日 下午5:39:26
15 | * version: V1.0
16 | * 17 | */ 18 | @SuppressWarnings("serial") 19 | @WebServlet(urlPatterns = "/druid/*", 20 | initParams={ 21 | @WebInitParam(name="allow",value="127.0.0.1,192.168.0.67"),// IP白名单 (没有配置或者为空,则允许所有访问) 22 | @WebInitParam(name="loginUsername",value="admin"),// 用户名 23 | @WebInitParam(name="loginPassword",value="123456"),// 密码 24 | @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能 25 | }) 26 | public class DruidStatViewServlet extends StatViewServlet { 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/config/MyBatisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.config; 2 | 3 | import java.util.Properties; 4 | 5 | import javax.sql.DataSource; 6 | 7 | import org.apache.ibatis.plugin.Interceptor; 8 | import org.apache.ibatis.session.SqlSessionFactory; 9 | import org.mybatis.spring.SqlSessionFactoryBean; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 | import org.springframework.core.io.support.ResourcePatternResolver; 15 | 16 | import com.github.pagehelper.PageHelper; 17 | import com.google.code.shardbatis.plugin.ShardPlugin; 18 | 19 | @Configuration 20 | public class MyBatisConfig { 21 | @Autowired 22 | DataSource dataSource; 23 | 24 | @Bean(name = "sqlSessionFactory") 25 | public SqlSessionFactory sqlSessionFactoryBean() { 26 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 27 | bean.setDataSource(dataSource); 28 | bean.setTypeAliasesPackage("com.zhushoumao.springboot.pojo"); 29 | 30 | /** 分页插件begin **/ 31 | PageHelper pageHelper = new PageHelper(); 32 | Properties properties = new Properties(); 33 | properties.setProperty("reasonable", "true"); 34 | properties.setProperty("supportMethodsArguments", "true"); 35 | properties.setProperty("returnPageInfo", "check"); 36 | properties.setProperty("params", "count=countSql"); 37 | pageHelper.setProperties(properties); 38 | /** 分页插件end **/ 39 | 40 | /** 分表插件begin **/ 41 | ShardPlugin shardPlugin = new ShardPlugin(); 42 | Properties shardProperties = new Properties(); 43 | shardProperties.setProperty("shardingConfig", "sharding_config.xml"); 44 | shardPlugin.setProperties(shardProperties); 45 | /** 分表插件end **/ 46 | // 添加插件 47 | bean.setPlugins(new Interceptor[] { shardPlugin, pageHelper }); 48 | 49 | // 添加XML目录 50 | ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 51 | try { 52 | bean.setMapperLocations(resolver.getResources("classpath:com/zhushoumao/springboot/mapper/*.xml")); 53 | return bean.getObject(); 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | throw new RuntimeException(e); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.ApiInfo; 9 | import springfox.documentation.spi.DocumentationType; 10 | import springfox.documentation.spring.web.plugins.Docket; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | 13 | /** 14 | * SwaggerConfig 15 | */ 16 | @Configuration 17 | @EnableSwagger2 18 | public class SwaggerConfig { 19 | 20 | /** 21 | * 可以定义多个组,比如本类中定义把test和demo区分开了 22 | * (访问页面就可以看到效果了) 23 | * 24 | */ 25 | @Bean 26 | public Docket testApi() { 27 | return new Docket(DocumentationType.SWAGGER_2) 28 | .apiInfo(apiInfo()) 29 | // .ignoredParameterTypes(User.class) 30 | .select() 31 | .apis(RequestHandlerSelectors.basePackage("com.zhushoumao.springboot")) 32 | .paths(PathSelectors.any()) 33 | .build(); 34 | } 35 | 36 | 37 | 38 | private ApiInfo apiInfo() { 39 | ApiInfo apiInfo = new ApiInfo("SpringBoot学习demo",//大标题 40 | "Spring boot + swagger + mybatis + druid",//小标题 41 | "1.0",//版本 42 | "NO terms of service", 43 | "761396462@qq.com",//作者 44 | "761396462@qq.com",//链接显示文字 45 | "http://blog.csdn.net/puhaiyang"//网站链接 46 | ); 47 | 48 | return apiInfo; 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.zhushoumao.springboot.pojo.City; 12 | import com.zhushoumao.springboot.service.CityService; 13 | 14 | import io.swagger.annotations.Api; 15 | import io.swagger.annotations.ApiImplicitParam; 16 | import io.swagger.annotations.ApiImplicitParams; 17 | import io.swagger.annotations.ApiOperation; 18 | 19 | @Api(value = "TestController") 20 | @RestController 21 | @RequestMapping("test/") 22 | public class TestController { 23 | 24 | @Autowired 25 | private CityService cityService; 26 | 27 | /** 28 | * 29 | * Title: 获取城市测试
30 | * Description: (这里用一句话描述这个方法的作用)
31 | * author: puhaiyang
32 | * date: 2017年1月13日 下午9:15:14
33 | * version: V1.0
34 | * 35 | * @param id 36 | * @return
37 | * City 38 | */ 39 | @ApiOperation(value = "获取城市", notes = "获取城市测试,主要测试分表插件", response = City.class) 40 | @RequestMapping(value = "city", method = RequestMethod.GET) 41 | public City getCity(@RequestParam Integer id) { 42 | City city = cityService.getCityById(id); 43 | return city; 44 | } 45 | 46 | /** 47 | * 48 | * Title:获取城市列表测试
49 | * Description: (这里用一句话描述这个方法的作用)
50 | * author: puhaiyang
51 | * date: 2017年1月13日 下午9:15:36
52 | * version: V1.0
53 | * 54 | * @param page 55 | * @param rows 56 | * @return
57 | * List 58 | */ 59 | 60 | @ApiOperation(value = "获取城市列表测试", notes = "获取城市列表测试,主要测试分页插件") 61 | @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "页号,默认为第1页", required = false, dataType = "Integer"), 62 | @ApiImplicitParam(name = "rows", value = "每页记录书,默认为3条", required = false, dataType = "Integer") }) 63 | @RequestMapping(value = "citys", method = RequestMethod.GET) 64 | public List getCitys(@RequestParam(value = "page", defaultValue = "1") Integer page, 65 | @RequestParam(value = "rows", defaultValue = "3") Integer rows) { 66 | List list = cityService.getCitysByPage(rows, page); 67 | return list; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/mapper/CityMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.mapper; 2 | 3 | import com.zhushoumao.springboot.pojo.City; 4 | import com.zhushoumao.springboot.pojo.CityExample; 5 | import java.util.List; 6 | import org.apache.ibatis.annotations.Param; 7 | 8 | public interface CityMapper { 9 | int countByExample(CityExample example); 10 | 11 | int deleteByExample(CityExample example); 12 | 13 | int deleteByPrimaryKey(Long id); 14 | 15 | int insert(City record); 16 | 17 | int insertSelective(City record); 18 | 19 | List selectByExample(CityExample example); 20 | 21 | City selectByPrimaryKey(Long id); 22 | 23 | int updateByExampleSelective(@Param("record") City record, @Param("example") CityExample example); 24 | 25 | int updateByExample(@Param("record") City record, @Param("example") CityExample example); 26 | 27 | int updateByPrimaryKeySelective(City record); 28 | 29 | int updateByPrimaryKey(City record); 30 | } -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/pojo/City.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.pojo; 2 | 3 | public class City { 4 | private Long id; 5 | 6 | private String name; 7 | 8 | private String state; 9 | 10 | public Long getId() { 11 | return id; 12 | } 13 | 14 | public void setId(Long id) { 15 | this.id = id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name == null ? null : name.trim(); 24 | } 25 | 26 | public String getState() { 27 | return state; 28 | } 29 | 30 | public void setState(String state) { 31 | this.state = state == null ? null : state.trim(); 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/pojo/CityExample.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.pojo; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class CityExample { 7 | protected String orderByClause; 8 | 9 | protected boolean distinct; 10 | 11 | protected List oredCriteria; 12 | 13 | public CityExample() { 14 | oredCriteria = new ArrayList(); 15 | } 16 | 17 | public void setOrderByClause(String orderByClause) { 18 | this.orderByClause = orderByClause; 19 | } 20 | 21 | public String getOrderByClause() { 22 | return orderByClause; 23 | } 24 | 25 | public void setDistinct(boolean distinct) { 26 | this.distinct = distinct; 27 | } 28 | 29 | public boolean isDistinct() { 30 | return distinct; 31 | } 32 | 33 | public List getOredCriteria() { 34 | return oredCriteria; 35 | } 36 | 37 | public void or(Criteria criteria) { 38 | oredCriteria.add(criteria); 39 | } 40 | 41 | public Criteria or() { 42 | Criteria criteria = createCriteriaInternal(); 43 | oredCriteria.add(criteria); 44 | return criteria; 45 | } 46 | 47 | public Criteria createCriteria() { 48 | Criteria criteria = createCriteriaInternal(); 49 | if (oredCriteria.size() == 0) { 50 | oredCriteria.add(criteria); 51 | } 52 | return criteria; 53 | } 54 | 55 | protected Criteria createCriteriaInternal() { 56 | Criteria criteria = new Criteria(); 57 | return criteria; 58 | } 59 | 60 | public void clear() { 61 | oredCriteria.clear(); 62 | orderByClause = null; 63 | distinct = false; 64 | } 65 | 66 | protected abstract static class GeneratedCriteria { 67 | protected List criteria; 68 | 69 | protected GeneratedCriteria() { 70 | super(); 71 | criteria = new ArrayList(); 72 | } 73 | 74 | public boolean isValid() { 75 | return criteria.size() > 0; 76 | } 77 | 78 | public List getAllCriteria() { 79 | return criteria; 80 | } 81 | 82 | public List getCriteria() { 83 | return criteria; 84 | } 85 | 86 | protected void addCriterion(String condition) { 87 | if (condition == null) { 88 | throw new RuntimeException("Value for condition cannot be null"); 89 | } 90 | criteria.add(new Criterion(condition)); 91 | } 92 | 93 | protected void addCriterion(String condition, Object value, String property) { 94 | if (value == null) { 95 | throw new RuntimeException("Value for " + property + " cannot be null"); 96 | } 97 | criteria.add(new Criterion(condition, value)); 98 | } 99 | 100 | protected void addCriterion(String condition, Object value1, Object value2, String property) { 101 | if (value1 == null || value2 == null) { 102 | throw new RuntimeException("Between values for " + property + " cannot be null"); 103 | } 104 | criteria.add(new Criterion(condition, value1, value2)); 105 | } 106 | 107 | public Criteria andIdIsNull() { 108 | addCriterion("id is null"); 109 | return (Criteria) this; 110 | } 111 | 112 | public Criteria andIdIsNotNull() { 113 | addCriterion("id is not null"); 114 | return (Criteria) this; 115 | } 116 | 117 | public Criteria andIdEqualTo(Long value) { 118 | addCriterion("id =", value, "id"); 119 | return (Criteria) this; 120 | } 121 | 122 | public Criteria andIdNotEqualTo(Long value) { 123 | addCriterion("id <>", value, "id"); 124 | return (Criteria) this; 125 | } 126 | 127 | public Criteria andIdGreaterThan(Long value) { 128 | addCriterion("id >", value, "id"); 129 | return (Criteria) this; 130 | } 131 | 132 | public Criteria andIdGreaterThanOrEqualTo(Long value) { 133 | addCriterion("id >=", value, "id"); 134 | return (Criteria) this; 135 | } 136 | 137 | public Criteria andIdLessThan(Long value) { 138 | addCriterion("id <", value, "id"); 139 | return (Criteria) this; 140 | } 141 | 142 | public Criteria andIdLessThanOrEqualTo(Long value) { 143 | addCriterion("id <=", value, "id"); 144 | return (Criteria) this; 145 | } 146 | 147 | public Criteria andIdIn(List values) { 148 | addCriterion("id in", values, "id"); 149 | return (Criteria) this; 150 | } 151 | 152 | public Criteria andIdNotIn(List values) { 153 | addCriterion("id not in", values, "id"); 154 | return (Criteria) this; 155 | } 156 | 157 | public Criteria andIdBetween(Long value1, Long value2) { 158 | addCriterion("id between", value1, value2, "id"); 159 | return (Criteria) this; 160 | } 161 | 162 | public Criteria andIdNotBetween(Long value1, Long value2) { 163 | addCriterion("id not between", value1, value2, "id"); 164 | return (Criteria) this; 165 | } 166 | 167 | public Criteria andNameIsNull() { 168 | addCriterion("name is null"); 169 | return (Criteria) this; 170 | } 171 | 172 | public Criteria andNameIsNotNull() { 173 | addCriterion("name is not null"); 174 | return (Criteria) this; 175 | } 176 | 177 | public Criteria andNameEqualTo(String value) { 178 | addCriterion("name =", value, "name"); 179 | return (Criteria) this; 180 | } 181 | 182 | public Criteria andNameNotEqualTo(String value) { 183 | addCriterion("name <>", value, "name"); 184 | return (Criteria) this; 185 | } 186 | 187 | public Criteria andNameGreaterThan(String value) { 188 | addCriterion("name >", value, "name"); 189 | return (Criteria) this; 190 | } 191 | 192 | public Criteria andNameGreaterThanOrEqualTo(String value) { 193 | addCriterion("name >=", value, "name"); 194 | return (Criteria) this; 195 | } 196 | 197 | public Criteria andNameLessThan(String value) { 198 | addCriterion("name <", value, "name"); 199 | return (Criteria) this; 200 | } 201 | 202 | public Criteria andNameLessThanOrEqualTo(String value) { 203 | addCriterion("name <=", value, "name"); 204 | return (Criteria) this; 205 | } 206 | 207 | public Criteria andNameLike(String value) { 208 | addCriterion("name like", value, "name"); 209 | return (Criteria) this; 210 | } 211 | 212 | public Criteria andNameNotLike(String value) { 213 | addCriterion("name not like", value, "name"); 214 | return (Criteria) this; 215 | } 216 | 217 | public Criteria andNameIn(List values) { 218 | addCriterion("name in", values, "name"); 219 | return (Criteria) this; 220 | } 221 | 222 | public Criteria andNameNotIn(List values) { 223 | addCriterion("name not in", values, "name"); 224 | return (Criteria) this; 225 | } 226 | 227 | public Criteria andNameBetween(String value1, String value2) { 228 | addCriterion("name between", value1, value2, "name"); 229 | return (Criteria) this; 230 | } 231 | 232 | public Criteria andNameNotBetween(String value1, String value2) { 233 | addCriterion("name not between", value1, value2, "name"); 234 | return (Criteria) this; 235 | } 236 | 237 | public Criteria andStateIsNull() { 238 | addCriterion("state is null"); 239 | return (Criteria) this; 240 | } 241 | 242 | public Criteria andStateIsNotNull() { 243 | addCriterion("state is not null"); 244 | return (Criteria) this; 245 | } 246 | 247 | public Criteria andStateEqualTo(String value) { 248 | addCriterion("state =", value, "state"); 249 | return (Criteria) this; 250 | } 251 | 252 | public Criteria andStateNotEqualTo(String value) { 253 | addCriterion("state <>", value, "state"); 254 | return (Criteria) this; 255 | } 256 | 257 | public Criteria andStateGreaterThan(String value) { 258 | addCriterion("state >", value, "state"); 259 | return (Criteria) this; 260 | } 261 | 262 | public Criteria andStateGreaterThanOrEqualTo(String value) { 263 | addCriterion("state >=", value, "state"); 264 | return (Criteria) this; 265 | } 266 | 267 | public Criteria andStateLessThan(String value) { 268 | addCriterion("state <", value, "state"); 269 | return (Criteria) this; 270 | } 271 | 272 | public Criteria andStateLessThanOrEqualTo(String value) { 273 | addCriterion("state <=", value, "state"); 274 | return (Criteria) this; 275 | } 276 | 277 | public Criteria andStateLike(String value) { 278 | addCriterion("state like", value, "state"); 279 | return (Criteria) this; 280 | } 281 | 282 | public Criteria andStateNotLike(String value) { 283 | addCriterion("state not like", value, "state"); 284 | return (Criteria) this; 285 | } 286 | 287 | public Criteria andStateIn(List values) { 288 | addCriterion("state in", values, "state"); 289 | return (Criteria) this; 290 | } 291 | 292 | public Criteria andStateNotIn(List values) { 293 | addCriterion("state not in", values, "state"); 294 | return (Criteria) this; 295 | } 296 | 297 | public Criteria andStateBetween(String value1, String value2) { 298 | addCriterion("state between", value1, value2, "state"); 299 | return (Criteria) this; 300 | } 301 | 302 | public Criteria andStateNotBetween(String value1, String value2) { 303 | addCriterion("state not between", value1, value2, "state"); 304 | return (Criteria) this; 305 | } 306 | } 307 | 308 | public static class Criteria extends GeneratedCriteria { 309 | 310 | protected Criteria() { 311 | super(); 312 | } 313 | } 314 | 315 | public static class Criterion { 316 | private String condition; 317 | 318 | private Object value; 319 | 320 | private Object secondValue; 321 | 322 | private boolean noValue; 323 | 324 | private boolean singleValue; 325 | 326 | private boolean betweenValue; 327 | 328 | private boolean listValue; 329 | 330 | private String typeHandler; 331 | 332 | public String getCondition() { 333 | return condition; 334 | } 335 | 336 | public Object getValue() { 337 | return value; 338 | } 339 | 340 | public Object getSecondValue() { 341 | return secondValue; 342 | } 343 | 344 | public boolean isNoValue() { 345 | return noValue; 346 | } 347 | 348 | public boolean isSingleValue() { 349 | return singleValue; 350 | } 351 | 352 | public boolean isBetweenValue() { 353 | return betweenValue; 354 | } 355 | 356 | public boolean isListValue() { 357 | return listValue; 358 | } 359 | 360 | public String getTypeHandler() { 361 | return typeHandler; 362 | } 363 | 364 | protected Criterion(String condition) { 365 | super(); 366 | this.condition = condition; 367 | this.typeHandler = null; 368 | this.noValue = true; 369 | } 370 | 371 | protected Criterion(String condition, Object value, String typeHandler) { 372 | super(); 373 | this.condition = condition; 374 | this.value = value; 375 | this.typeHandler = typeHandler; 376 | if (value instanceof List) { 377 | this.listValue = true; 378 | } else { 379 | this.singleValue = true; 380 | } 381 | } 382 | 383 | protected Criterion(String condition, Object value) { 384 | this(condition, value, null); 385 | } 386 | 387 | protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { 388 | super(); 389 | this.condition = condition; 390 | this.value = value; 391 | this.secondValue = secondValue; 392 | this.typeHandler = typeHandler; 393 | this.betweenValue = true; 394 | } 395 | 396 | protected Criterion(String condition, Object value, Object secondValue) { 397 | this(condition, value, secondValue, null); 398 | } 399 | } 400 | } -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/service/CityService.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.service; 2 | 3 | import java.util.List; 4 | 5 | import com.zhushoumao.springboot.pojo.City; 6 | 7 | public interface CityService { 8 | 9 | public City getCityById(Integer id); 10 | 11 | public List getCitysByPage(Integer rows,Integer page); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/service/impl/CityServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.service.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.annotation.Resource; 7 | 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.github.pagehelper.PageHelper; 11 | import com.zhushoumao.springboot.mapper.CityMapper; 12 | import com.zhushoumao.springboot.pojo.City; 13 | import com.zhushoumao.springboot.pojo.CityExample; 14 | import com.zhushoumao.springboot.service.CityService; 15 | 16 | @Service 17 | public class CityServiceImpl implements CityService { 18 | @Resource 19 | private CityMapper cityMapper; 20 | 21 | @Override 22 | public City getCityById(Integer id) { 23 | return cityMapper.selectByPrimaryKey(Long.valueOf(id)); 24 | } 25 | 26 | @Override 27 | public List getCitysByPage(Integer rows, Integer page) { 28 | PageHelper.startPage(page, rows); 29 | CityExample example = new CityExample(); 30 | List list = cityMapper.selectByExample(example); 31 | if (list.isEmpty()) { 32 | return new ArrayList(); 33 | } 34 | return list; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/zhushoumao/springboot/utils/CityShardStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhushoumao.springboot.utils; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import com.google.code.shardbatis.strategy.ShardStrategy; 7 | 8 | public class CityShardStrategyImpl implements ShardStrategy { 9 | Logger LOG = LoggerFactory.getLogger(getClass()); 10 | 11 | @Override 12 | public String getTargetTableName(String baseTableName, Object params, String mapperId) { 13 | LOG.debug("baseTableName->" + baseTableName + " \tparams->" + params + "\tmapperId->" + mapperId); 14 | if (params instanceof Long) { 15 | int nowIndex = ((Long) params).intValue(); 16 | if (nowIndex == 3 || nowIndex == 4) { 17 | baseTableName = baseTableName + "_" + 2015; 18 | } else if (nowIndex == 5 || nowIndex == 6) { 19 | baseTableName = baseTableName + "_" + 2016; 20 | } 21 | } 22 | LOG.debug("proced baseTableName->" + baseTableName + " \tparams->" + params + "\tmapperId->" + mapperId); 23 | return baseTableName; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 80 4 | spring: 5 | application: 6 | name: springboot-test 7 | datasource: 8 | url: jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8 9 | username: root 10 | password: root 11 | driver_class_namel: com.mysql.jdbc.Driver 12 | type: com.alibaba.druid.pool.DruidDataSource 13 | 14 | max-active: 20 15 | initial-size: 1 16 | min-idle: 3 17 | max-wait: 60000 18 | time-between-eviction-runs-millis: 60000 19 | min-evictable-idle-time-millis: 300000 20 | test-while-idle: true 21 | test-on-borrow: false 22 | test-on-return: false 23 | poolPreparedStatements: true 24 | 25 | logging: 26 | file: ./springboot.log 27 | level: 28 | org.springframework.web: DEBUG 29 | com.zhushoumao.springboot: DEBUG 30 | 31 | #debug: true 32 | -------------------------------------------------------------------------------- /src/main/resources/com/zhushoumao/springboot/mapper/CityMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | and ${criterion.condition} 18 | 19 | 20 | and ${criterion.condition} #{criterion.value} 21 | 22 | 23 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 24 | 25 | 26 | and ${criterion.condition} 27 | 28 | #{listItem} 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | and ${criterion.condition} 47 | 48 | 49 | and ${criterion.condition} #{criterion.value} 50 | 51 | 52 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 53 | 54 | 55 | and ${criterion.condition} 56 | 57 | #{listItem} 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | id, name, state 69 | 70 | 84 | 90 | 91 | delete from city 92 | where id = #{id,jdbcType=BIGINT} 93 | 94 | 95 | delete from city 96 | 97 | 98 | 99 | 100 | 101 | insert into city (id, name, state 102 | ) 103 | values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{state,jdbcType=VARCHAR} 104 | ) 105 | 106 | 107 | insert into city 108 | 109 | 110 | id, 111 | 112 | 113 | name, 114 | 115 | 116 | state, 117 | 118 | 119 | 120 | 121 | #{id,jdbcType=BIGINT}, 122 | 123 | 124 | #{name,jdbcType=VARCHAR}, 125 | 126 | 127 | #{state,jdbcType=VARCHAR}, 128 | 129 | 130 | 131 | 137 | 138 | update city 139 | 140 | 141 | id = #{record.id,jdbcType=BIGINT}, 142 | 143 | 144 | name = #{record.name,jdbcType=VARCHAR}, 145 | 146 | 147 | state = #{record.state,jdbcType=VARCHAR}, 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | update city 156 | set id = #{record.id,jdbcType=BIGINT}, 157 | name = #{record.name,jdbcType=VARCHAR}, 158 | state = #{record.state,jdbcType=VARCHAR} 159 | 160 | 161 | 162 | 163 | 164 | update city 165 | 166 | 167 | name = #{name,jdbcType=VARCHAR}, 168 | 169 | 170 | state = #{state,jdbcType=VARCHAR}, 171 | 172 | 173 | where id = #{id,jdbcType=BIGINT} 174 | 175 | 176 | update city 177 | set name = #{name,jdbcType=VARCHAR}, 178 | state = #{state,jdbcType=VARCHAR} 179 | where id = #{id,jdbcType=BIGINT} 180 | 181 | -------------------------------------------------------------------------------- /src/main/resources/sharding_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.zhushoumao.springboot.mapper.CityMapper.selectByPrimaryKey 6 | 7 | 8 | 9 | 10 | --------------------------------------------------------------------------------