├── README.md ├── target └── classes │ ├── mybatis.out.xml │ ├── com │ └── mycat │ │ ├── pojo │ │ └── Item.class │ │ ├── Application.class │ │ ├── mapper │ │ ├── ItemMapper.class │ │ └── ItemMapper.xml │ │ ├── controller │ │ └── TestController.class │ │ ├── interceptor │ │ └── MyInterceptor.class │ │ └── com │ │ └── mycat │ │ └── utils │ │ └── SessionUtil.class │ ├── META-INF │ ├── maven │ │ └── com.mycat │ │ │ └── mycatTest │ │ │ ├── pom.properties │ │ │ └── pom.xml │ └── MANIFEST.MF │ ├── application.properties │ └── mybatis.xml ├── src └── main │ ├── resources │ ├── mybatis.out.xml │ ├── application.properties │ ├── mybatis.xml │ └── com │ │ └── mycat │ │ └── mapper │ │ └── ItemMapper.xml │ └── java │ └── com │ └── mycat │ ├── mapper │ └── ItemMapper.java │ ├── Application.java │ ├── pojo │ └── Item.java │ ├── com │ └── mycat │ │ └── utils │ │ └── SessionUtil.java │ ├── controller │ └── TestController.java │ └── interceptor │ └── MyInterceptor.java ├── mycatTest.iml ├── .settings ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── .idea ├── encodings.xml ├── misc.xml ├── compiler.xml └── workspace.xml ├── .project ├── .classpath └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # multi-tenant 2 | -------------------------------------------------------------------------------- /target/classes/mybatis.out.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/resources/mybatis.out.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mycatTest.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /target/classes/com/mycat/pojo/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/pojo/Item.class -------------------------------------------------------------------------------- /target/classes/com/mycat/Application.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/Application.class -------------------------------------------------------------------------------- /target/classes/com/mycat/mapper/ItemMapper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/mapper/ItemMapper.class -------------------------------------------------------------------------------- /target/classes/com/mycat/controller/TestController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/controller/TestController.class -------------------------------------------------------------------------------- /target/classes/com/mycat/interceptor/MyInterceptor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/interceptor/MyInterceptor.class -------------------------------------------------------------------------------- /target/classes/com/mycat/com/mycat/utils/SessionUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangpangdaa/multi-tenant/HEAD/target/classes/com/mycat/com/mycat/utils/SessionUtil.class -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 3 | org.eclipse.jdt.core.compiler.compliance=1.7 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.7 6 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/com.mycat/mycatTest/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Wed May 16 14:41:04 CST 2018 3 | version=0.0.1-SNAPSHOT 4 | groupId=com.mycat 5 | m2e.projectName=mycatTest 6 | m2e.projectLocation=C\:\\Users\\huang\\mycatTest 7 | artifactId=mycatTest 8 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.thymeleaf.mode=LEGACYHTML5 2 | spring.datasource.url=jdbc:mysql://localhost:8066/TESTDB?serverTimezone=GMT 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 | mybatis.config-location=classpath:mybatis.xml -------------------------------------------------------------------------------- /target/classes/application.properties: -------------------------------------------------------------------------------- 1 | spring.thymeleaf.mode=LEGACYHTML5 2 | spring.datasource.url=jdbc:mysql://localhost:8066/TESTDB?serverTimezone=GMT 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 | mybatis.config-location=classpath:mybatis.xml -------------------------------------------------------------------------------- /src/main/java/com/mycat/mapper/ItemMapper.java: -------------------------------------------------------------------------------- 1 | package com.mycat.mapper; 2 | 3 | import com.mycat.pojo.Item; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | @Mapper 8 | public interface ItemMapper { 9 | public Item getOne(@Param("id") int id); 10 | public Integer count(); 11 | public void insert(Item item); 12 | } 13 | -------------------------------------------------------------------------------- /target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Implementation-Title: NISHINO 3 | Implementation-Version: 0.0.1-SNAPSHOT 4 | Built-By: huang 5 | Implementation-Vendor-Id: com.mycat 6 | Build-Jdk: 1.8.0_171 7 | Implementation-URL: http://projects.spring.io/spring-boot/mycatTest/ 8 | Created-By: Maven Integration for Eclipse 9 | Implementation-Vendor: Pivotal Software, Inc. 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/com/mycat/Application.java: -------------------------------------------------------------------------------- 1 | package com.mycat; 2 | 3 | 4 | import org.apache.ibatis.mapping.MappedStatement; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import java.sql.*; 9 | 10 | @SpringBootApplication 11 | public class Application { 12 | public static void main(String[] args) throws SQLException, ClassNotFoundException { 13 | SpringApplication.run(Application.class); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | mycatTest 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/com/mycat/pojo/Item.java: -------------------------------------------------------------------------------- 1 | package com.mycat.pojo; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | 8 | @Data 9 | public class Item implements Serializable { 10 | int id; 11 | int value; 12 | public int getId() { 13 | return id; 14 | } 15 | public void setId(int id) { 16 | this.id = id; 17 | } 18 | public int getValue() { 19 | return value; 20 | } 21 | public void setValue(int value) { 22 | this.value = value; 23 | } 24 | public Date getIndate() { 25 | return indate; 26 | } 27 | public void setIndate(Date indate) { 28 | this.indate = indate; 29 | } 30 | Date indate; 31 | } 32 | -------------------------------------------------------------------------------- /target/classes/mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/resources/mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /target/classes/com/mycat/mapper/ItemMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | insert into item (id,value) 20 | values (#{id},#{value}); 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/resources/com/mycat/mapper/ItemMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | insert into item (id,value) 20 | values (#{id},#{value}); 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/com/mycat/com/mycat/utils/SessionUtil.java: -------------------------------------------------------------------------------- 1 | package com.mycat.com.mycat.utils; 2 | 3 | import org.springframework.web.context.request.RequestContextHolder; 4 | import org.springframework.web.context.request.ServletRequestAttributes; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpSession; 8 | 9 | public class SessionUtil { 10 | /** 11 | * SpringMvc下获取request 12 | * 13 | * @return 14 | */ 15 | public static HttpServletRequest getRequest() { 16 | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 17 | return request; 18 | 19 | } 20 | 21 | public static HttpSession getSession(){ 22 | HttpServletRequest request=getRequest(); 23 | HttpSession session=request.getSession(); 24 | return session; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/com/mycat/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.mycat.controller; 2 | 3 | import com.mycat.com.mycat.utils.SessionUtil; 4 | import com.mycat.mapper.ItemMapper; 5 | import com.mycat.pojo.Item; 6 | 7 | import org.apache.ibatis.plugin.Plugin; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.ResponseBody; 14 | 15 | import javax.servlet.http.HttpSession; 16 | 17 | @Controller 18 | public class TestController { 19 | 20 | @Autowired 21 | ItemMapper itemMapper; 22 | 23 | @GetMapping("/{id}") 24 | @ResponseBody 25 | public Object test(@PathVariable("id") int id){ 26 | System.out.println(id); 27 | return itemMapper.getOne(id); 28 | } 29 | 30 | //简化,直接通过这里设置session 31 | @GetMapping("/set/{sess}") 32 | @ResponseBody 33 | public Object setSession(@PathVariable("sess") String sess){ 34 | HttpSession httpSession=SessionUtil.getSession(); 35 | httpSession.setAttribute("corp",sess); 36 | return "ok"; 37 | } 38 | 39 | 40 | @GetMapping("/count") 41 | @ResponseBody 42 | public Object getCount(){ 43 | //要测试的方法 44 | 45 | return itemMapper.count(); 46 | } 47 | 48 | @PostMapping("/add") 49 | @ResponseBody 50 | public Object add(Item item){ 51 | itemMapper.insert(item); 52 | return 1; 53 | } 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/mycat/interceptor/MyInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.mycat.interceptor; 2 | 3 | import com.mycat.com.mycat.utils.SessionUtil; 4 | import com.mycat.controller.TestController; 5 | import org.apache.ibatis.cache.CacheKey; 6 | import org.apache.ibatis.executor.Executor; 7 | import org.apache.ibatis.executor.statement.StatementHandler; 8 | import org.apache.ibatis.mapping.BoundSql; 9 | import org.apache.ibatis.mapping.MappedStatement; 10 | import org.apache.ibatis.plugin.*; 11 | import org.apache.ibatis.reflection.MetaObject; 12 | import org.apache.ibatis.reflection.SystemMetaObject; 13 | import org.apache.ibatis.session.ResultHandler; 14 | import org.apache.ibatis.session.RowBounds; 15 | 16 | import javax.websocket.Session; 17 | import java.sql.Connection; 18 | import java.util.Properties; 19 | 20 | @Intercepts(value = { 21 | @Signature(type = StatementHandler.class, 22 | method = "prepare", 23 | args = {Connection.class,Integer.class})}) 24 | public class MyInterceptor implements Interceptor { 25 | private static final String preState="/*!mycat:datanode="; 26 | private static final String afterState="*/"; 27 | 28 | public Object intercept(Invocation invocation) throws Throwable { 29 | StatementHandler statementHandler=(StatementHandler)invocation.getTarget(); 30 | MetaObject metaStatementHandler=SystemMetaObject.forObject(statementHandler); 31 | Object object=null; 32 | //分离代理对象链 33 | while(metaStatementHandler.hasGetter("h")){ 34 | object=metaStatementHandler.getValue("h"); 35 | metaStatementHandler=SystemMetaObject.forObject(object); 36 | } 37 | statementHandler=(StatementHandler)object; 38 | String sql=(String)metaStatementHandler.getValue("delegate.boundSql.sql"); 39 | /* 40 | String node=(String)TestController.threadLocal.get(); 41 | */ 42 | String node=(String)SessionUtil.getSession().getAttribute("corp"); 43 | if(node!=null) { 44 | sql = preState + node + afterState + sql; 45 | } 46 | 47 | System.out.println("sql is "+sql); 48 | metaStatementHandler.setValue("delegate.boundSql.sql",sql); 49 | Object result = invocation.proceed(); 50 | System.out.println("Invocation.proceed()"); 51 | return result; 52 | } 53 | 54 | public Object plugin(Object target) { 55 | 56 | return Plugin.wrap(target, this); 57 | } 58 | 59 | public void setProperties(Properties properties) { 60 | String prop1 = properties.getProperty("prop1"); 61 | String prop2 = properties.getProperty("prop2"); 62 | System.out.println(prop1 + "------" + prop2); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.mycat 7 | mycatTest 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | NISHINO 12 | BOOK RECOMMEND WITH SPARK 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 1.5.9.RELEASE 19 | 20 | 21 | 22 | 23 | 24 | UTF-8 25 | 1.8 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-devtools 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | 39 | spring-boot-starter-data-redis 40 | 41 | 42 | 43 | 44 | org.projectlombok 45 | lombok 46 | ${lombok.version} 47 | provided 48 | 49 | 50 | 51 | 52 | com.github.pagehelper 53 | pagehelper 54 | 4.1.6 55 | 56 | 57 | 58 | org.mybatis.spring.boot 59 | mybatis-spring-boot-starter 60 | 1.3.1 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-jdbc 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-starter-thymeleaf 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-web 74 | 75 | 76 | net.sourceforge.nekohtml 77 | nekohtml 78 | 79 | 80 | 81 | 82 | mysql 83 | mysql-connector-java 84 | 5.1.38 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | aliyun-nexus 94 | Aliyun Maven Repository 95 | http://maven.aliyun.com/nexus/content/groups/public/ 96 | 97 | true 98 | 99 | 100 | true 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/com.mycat/mycatTest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.mycat 7 | mycatTest 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | NISHINO 12 | BOOK RECOMMEND WITH SPARK 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 1.5.9.RELEASE 19 | 20 | 21 | 22 | 23 | 24 | UTF-8 25 | 1.8 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-devtools 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | 39 | spring-boot-starter-data-redis 40 | 41 | 42 | 43 | 44 | org.projectlombok 45 | lombok 46 | ${lombok.version} 47 | provided 48 | 49 | 50 | 51 | 52 | com.github.pagehelper 53 | pagehelper 54 | 4.1.6 55 | 56 | 57 | 58 | org.mybatis.spring.boot 59 | mybatis-spring-boot-starter 60 | 1.3.1 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-jdbc 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-starter-thymeleaf 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-web 74 | 75 | 76 | net.sourceforge.nekohtml 77 | nekohtml 78 | 79 | 80 | 81 | 82 | mysql 83 | mysql-connector-java 84 | 5.1.38 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | aliyun-nexus 94 | Aliyun Maven Repository 95 | http://maven.aliyun.com/nexus/content/groups/public/ 96 | 97 | true 98 | 99 | 100 | true 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 123 | 124 | 125 | 126 | boundsql getbou 127 | 128 | 129 | 130 | 144 | 145 | 146 | 151 | 152 | 153 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 |