├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── sql └── myblog.sql └── src ├── main └── java │ └── xyz │ └── coolblog │ ├── chapter1 │ ├── dao │ │ └── ArticleDao.java │ ├── dao2 │ │ ├── ArticleDao.java │ │ └── AuthorDao.java │ ├── model │ │ └── Article.java │ ├── model2 │ │ ├── Article.java │ │ └── Author.java │ └── model3 │ │ └── Article.java │ ├── chapter3 │ └── model │ │ └── Article.java │ ├── chapter4 │ ├── dao │ │ ├── ArticleDao.java │ │ └── AuthorDao.java │ ├── model │ │ ├── Article.java │ │ └── Author.java │ └── model1 │ │ ├── Article.java │ │ └── Author.java │ ├── chapter6 │ ├── dao │ │ └── StudentDao.java │ └── model │ │ └── Student.java │ ├── chapter7 │ ├── dao │ │ └── StudentDao.java │ └── model │ │ └── Student.java │ ├── constant │ ├── ArticleCategoryEnum.java │ ├── ArticleTypeEnum.java │ └── SexEnum.java │ └── mybatis │ ├── ArticleTypeHandler.java │ ├── ExamplePlugin.java │ ├── LoggableObjectFactory.java │ └── MySqlPagingPlugin.java └── test ├── java └── xyz │ └── coolblog │ ├── chapter1 │ ├── HibernateTest.java │ ├── JdbcTest.java │ ├── MyBatisTest.java │ ├── MyBatisTest2.java │ ├── SpringJdbcTest.java │ └── SpringWithMyBatisTest.java │ ├── chapter2 │ └── MetaClassTest.java │ ├── chapter3 │ └── ResultMapTest.java │ ├── chapter4 │ ├── InsertManyTest.java │ ├── OneToOneTest.java │ ├── ParamNameResolverTest.java │ ├── SqlNodeTest.java │ └── SqlSourceBuilderTest.java │ ├── chapter6 │ └── TransactionalCacheTest.java │ └── chapter7 │ └── PluginTest.java └── resources ├── chapter1 ├── application-mybatis.xml ├── application.xml ├── hibernate.cfg.xml ├── mapper │ └── ArticleMapper.xml ├── mapper2 │ ├── ArticleMapper.xml │ └── AuthorMapper.xml ├── mapping │ └── Article.hbm.xml ├── mybatis-config.xml ├── mybatis-config2.xml └── mybatis-config3.xml ├── chapter3 └── mapper │ └── ArticleMapper.xml ├── chapter4 ├── mapper │ ├── ArticleMapper.xml │ └── AuthorMapper.xml ├── mybatis-config.xml └── mybatis-config2.xml ├── chapter6 ├── mapper │ └── StudentMapper.xml └── mybatis-config.xml ├── chapter7 ├── mapper │ └── StudentMapper.xml └── mybatis-config.xml ├── jdbc.properties └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # IDEA 5 | .idea/ 6 | *.iml 7 | 8 | target/ 9 | 10 | # Log file 11 | *.log 12 | 13 | # BlueJ files 14 | *.ctxt 15 | 16 | # Mobile Tools for Java (J2ME) 17 | .mtj.tmp/ 18 | 19 | # Package Files # 20 | *.jar 21 | *.war 22 | *.ear 23 | *.zip 24 | *.tar.gz 25 | *.rar 26 | 27 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 28 | hs_err_pid* 29 | -------------------------------------------------------------------------------- /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 | # mybatis-test 2 | 3 | ## 1.简介 4 | 5 | 《一本小小的MyBatis源码分析书》一书的附属源码,该书以电子书的形式发布,可免费下载。下载途径如下: 6 | 7 | 百度网盘:[点击下载](https://pan.baidu.com/s/1d0JTkab0gHOApXrMUbHGuQ) 8 | 9 | 10 | 11 | 12 | 13 | ## 2. 代码使用说明 14 | 15 | mybatis-test 项目的文件结构大致如下: 16 | 17 | ``` 18 | . 19 | ├── sql 20 | │   └── myblog.sql 21 | ├── src 22 |    ├── main 23 |    │   ├── java 24 |    │   └── resources 25 |    └── test 26 |    ├── java 27 |    └── resources 28 | ``` 29 | 30 | 使用步骤如下: 31 | 32 | 1. 执行 sql/myblog.sql 脚本,导入数据到数据库中 33 | 2. 修改 test/resources/log4j.properties 文件,将数据库配置改为你所使用的数据库 34 | 3. 执行 test/java/ 文件夹下的测试代码进行测试 35 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | xyz.coolblog 6 | mybatis-test 7 | war 8 | 1.0-SNAPSHOT 9 | mybatis-test Maven Webapp 10 | http://maven.apache.org 11 | 12 | 13 | 1.8 14 | 1.8 15 | 4.3.17.RELEASE 16 | 17 | 18 | 19 | 20 | org.mybatis 21 | mybatis 22 | 3.4.6 23 | 24 | 25 | org.mybatis 26 | mybatis-spring 27 | 1.3.2 28 | 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | 6.0.6 34 | 35 | 36 | 37 | org.springframework 38 | spring-core 39 | ${spring.version} 40 | 41 | 42 | org.springframework 43 | spring-beans 44 | ${spring.version} 45 | 46 | 47 | org.springframework 48 | spring-context 49 | ${spring.version} 50 | 51 | 52 | org.springframework 53 | spring-test 54 | ${spring.version} 55 | test 56 | 57 | 58 | 59 | org.springframework 60 | spring-jdbc 61 | ${spring.version} 62 | 63 | 64 | 65 | org.slf4j 66 | slf4j-api 67 | 1.7.25 68 | 69 | 70 | org.slf4j 71 | slf4j-log4j12 72 | 1.7.25 73 | test 74 | 75 | 76 | log4j 77 | log4j 78 | 1.2.17 79 | 80 | 81 | 82 | junit 83 | junit 84 | 4.12 85 | test 86 | 87 | 88 | org.hamcrest 89 | hamcrest-core 90 | 1.3 91 | test 92 | 93 | 94 | commons-lang 95 | commons-lang 96 | 2.6 97 | 98 | 99 | 100 | org.hibernate 101 | hibernate-core 102 | 5.3.2.Final 103 | 104 | 105 | 106 | com.google.code.gson 107 | gson 108 | 2.8.5 109 | 110 | 111 | 112 | 113 | mybatis-test 114 | 115 | 116 | -------------------------------------------------------------------------------- /sql/myblog.sql: -------------------------------------------------------------------------------- 1 | # ************************************************************ 2 | # Sequel Pro SQL dump 3 | # Version 4541 4 | # 5 | # http://www.sequelpro.com/ 6 | # https://github.com/sequelpro/sequelpro 7 | # 8 | # Host: 127.0.0.1 (MySQL 5.7.20) 9 | # Database: myblog 10 | # Generation Time: 2018-09-09 11:05:33 +0000 11 | # ************************************************************ 12 | 13 | 14 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 15 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 16 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 17 | /*!40101 SET NAMES utf8 */; 18 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 19 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 20 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 21 | 22 | 23 | # Dump of table article 24 | # ------------------------------------------------------------ 25 | 26 | DROP TABLE IF EXISTS `article`; 27 | 28 | CREATE TABLE `article` ( 29 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 30 | `author_id` int(11) NOT NULL, 31 | `title` varchar(32) DEFAULT NULL, 32 | `type` tinyint(4) DEFAULT NULL, 33 | `content` text, 34 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 35 | PRIMARY KEY (`id`) 36 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 37 | 38 | LOCK TABLES `article` WRITE; 39 | /*!40000 ALTER TABLE `article` DISABLE KEYS */; 40 | 41 | INSERT INTO `article` (`id`, `author_id`, `title`, `type`, `content`, `create_time`) 42 | VALUES 43 | (1,1,'MyBatis 源码分析系列文章导读',8,'MyBatis 源码分析系列文章导读','2018-07-15 15:30:09'), 44 | (2,2,'HashMap 源码详细分析(JDK1.8)',1,'HashMap 源码详细分析(JDK1.8)','2018-01-18 15:29:13'), 45 | (3,1,'Java CAS 原理分析',1,'Java CAS 原理分析','2018-05-15 15:28:33'), 46 | (4,1,'Spring IOC 容器源码分析 - 获取单例 bean',4,'Spring IOC 容器源码分析 - 获取单例 bean','2018-06-01 00:00:00'), 47 | (5,1,'Spring IOC 容器源码分析 - 循环依赖的解决办法',4,'Spring IOC 容器源码分析 - 循环依赖的解决办法','2018-06-08 00:00:00'), 48 | (6,2,'Spring AOP 源码分析系列文章导读',4,'Spring AOP 源码分析系列文章导读','2018-06-17 00:00:00'), 49 | (7,2,'Spring AOP 源码分析 - 创建代理对象',4,'Spring AOP 源码分析 - 创建代理对象','2018-06-20 00:00:00'), 50 | (8,1,'Spring MVC 原理探秘 - 一个请求的旅行过程',4,'Spring MVC 原理探秘 - 一个请求的旅行过程','2018-06-29 00:00:00'), 51 | (9,2,'Spring MVC 原理探秘 - 容器的创建过程',4,'Spring MVC 原理探秘 - 容器的创建过程','2018-06-30 00:00:00'), 52 | (10,2,'Spring IOC 容器源码分析系列文章导读',4,'Spring IOC 容器源码分析系列文章导读','2018-05-30 00:00:00'); 53 | 54 | /*!40000 ALTER TABLE `article` ENABLE KEYS */; 55 | UNLOCK TABLES; 56 | 57 | 58 | # Dump of table author 59 | # ------------------------------------------------------------ 60 | 61 | DROP TABLE IF EXISTS `author`; 62 | 63 | CREATE TABLE `author` ( 64 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 65 | `name` varchar(32) DEFAULT NULL, 66 | `age` tinyint(4) DEFAULT NULL, 67 | `sex` tinyint(4) DEFAULT NULL, 68 | `email` varchar(64) DEFAULT NULL, 69 | PRIMARY KEY (`id`) 70 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 71 | 72 | LOCK TABLES `author` WRITE; 73 | /*!40000 ALTER TABLE `author` DISABLE KEYS */; 74 | 75 | INSERT INTO `author` (`id`, `name`, `age`, `sex`, `email`) 76 | VALUES 77 | (1,'coolblog.xyz',28,0,'coolblog.xyz@outlook.com'), 78 | (2,'nullllun',29,1,'coolblog.xyz@outlook.com'); 79 | 80 | /*!40000 ALTER TABLE `author` ENABLE KEYS */; 81 | UNLOCK TABLES; 82 | 83 | 84 | # Dump of table student 85 | # ------------------------------------------------------------ 86 | 87 | DROP TABLE IF EXISTS `student`; 88 | 89 | CREATE TABLE `student` ( 90 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 91 | `name` varchar(32) DEFAULT NULL, 92 | `age` tinyint(4) DEFAULT NULL, 93 | PRIMARY KEY (`id`) 94 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 95 | 96 | LOCK TABLES `student` WRITE; 97 | /*!40000 ALTER TABLE `student` DISABLE KEYS */; 98 | 99 | INSERT INTO `student` (`id`, `name`, `age`) 100 | VALUES 101 | (1,'coolblog',20); 102 | 103 | /*!40000 ALTER TABLE `student` ENABLE KEYS */; 104 | UNLOCK TABLES; 105 | 106 | 107 | 108 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 109 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 110 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 111 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 112 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 113 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 114 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/dao/ArticleDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.dao; 2 | 3 | import java.util.List; 4 | import org.apache.ibatis.annotations.Param; 5 | import xyz.coolblog.chapter1.model.Article; 6 | 7 | /** 8 | * ArticleDao 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-07-01 13:20:51 12 | */ 13 | public interface ArticleDao { 14 | List
findByAuthorAndCreateTime(@Param("author") String author, @Param("createTime") String createTime); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/dao2/ArticleDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.dao2; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | import xyz.coolblog.chapter1.model2.Article; 5 | 6 | /** 7 | * ArticleDao 8 | * 9 | * @author Tian ZhongBo 10 | * @date 2018-07-01 13:20:51 11 | */ 12 | public interface ArticleDao { 13 | 14 | Article findOne(@Param("id") int id); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/dao2/AuthorDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.dao2; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | import xyz.coolblog.chapter1.model2.Author; 5 | 6 | /** 7 | * AuthorDao 8 | * 9 | * @author Tian ZhongBo 10 | * @date 2018-07-14 21:53:53 11 | */ 12 | public interface AuthorDao { 13 | 14 | Author findOne(@Param("id") int id); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/model/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | private String content; 22 | 23 | private Date createTime; 24 | 25 | public Article() { 26 | } 27 | 28 | public Article(@Param("title") String title) { 29 | this.title = title; 30 | } 31 | 32 | public Article(@Param("id") Integer id, @Param("title") String title, @Param("content") String content) { 33 | this.id = id; 34 | this.title = title; 35 | this.content = content; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Integer id) { 43 | this.id = id; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public String getAuthor() { 55 | return author; 56 | } 57 | 58 | public void setAuthor(String author) { 59 | this.author = author; 60 | } 61 | 62 | public String getContent() { 63 | return content; 64 | } 65 | 66 | public void setContent(String content) { 67 | this.content = content; 68 | } 69 | 70 | public Date getCreateTime() { 71 | return createTime; 72 | } 73 | 74 | public void setCreateTime(Date createTime) { 75 | this.createTime = createTime; 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "Article{" + 81 | "id=" + id + 82 | ", title='" + title + '\'' + 83 | ", author='" + author + '\'' + 84 | ", content='" + content + '\'' + 85 | ", createTime=" + createTime + 86 | '}'; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/model2/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.model2; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import xyz.coolblog.constant.ArticleTypeEnum; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private Author author; 20 | 21 | private String content; 22 | 23 | private ArticleTypeEnum type; 24 | 25 | private Date createTime; 26 | 27 | public Integer getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public String getTitle() { 36 | return title; 37 | } 38 | 39 | public void setTitle(String title) { 40 | this.title = title; 41 | } 42 | 43 | public Author getAuthor() { 44 | return author; 45 | } 46 | 47 | public void setAuthor(Author author) { 48 | this.author = author; 49 | } 50 | 51 | public String getContent() { 52 | return content; 53 | } 54 | 55 | public void setContent(String content) { 56 | this.content = content; 57 | } 58 | 59 | public ArticleTypeEnum getType() { 60 | return type; 61 | } 62 | 63 | public void setType(ArticleTypeEnum type) { 64 | this.type = type; 65 | } 66 | 67 | public Date getCreateTime() { 68 | return createTime; 69 | } 70 | 71 | public void setCreateTime(Date createTime) { 72 | this.createTime = createTime; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Article{" + 78 | "id=" + id + 79 | ", title='" + title + '\'' + 80 | ", author=" + author + 81 | ", content='" + content + '\'' + 82 | ", type=" + type + 83 | ", createTime=" + createTime + 84 | '}'; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/model2/Author.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.model2; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import xyz.coolblog.constant.SexEnum; 6 | 7 | /** 8 | * Author 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-07-14 21:54:45 12 | */ 13 | public class Author implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String name; 18 | 19 | private Integer age; 20 | 21 | private SexEnum sex; 22 | 23 | private String email; 24 | 25 | private List
articles; 26 | 27 | public Integer getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public Integer getAge() { 44 | return age; 45 | } 46 | 47 | public void setAge(Integer age) { 48 | this.age = age; 49 | } 50 | 51 | public SexEnum getSex() { 52 | return sex; 53 | } 54 | 55 | public void setSex(SexEnum sex) { 56 | this.sex = sex; 57 | } 58 | 59 | public String getEmail() { 60 | return email; 61 | } 62 | 63 | public void setEmail(String email) { 64 | this.email = email; 65 | } 66 | 67 | public List
getArticles() { 68 | return articles; 69 | } 70 | 71 | public void setArticles(List
articles) { 72 | this.articles = articles; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Author{" + 78 | "id=" + id + 79 | ", name='" + name + '\'' + 80 | ", age=" + age + 81 | ", sex=" + sex + 82 | ", email='" + email + '\'' + 83 | ", articles=" + articles + 84 | '}'; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter1/model3/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1.model3; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | private String content; 22 | 23 | private Date createTime; 24 | 25 | public Article() { 26 | } 27 | 28 | public Article(@Param("title") String title) { 29 | this.title = title; 30 | } 31 | 32 | public Article(@Param("id") Integer id, @Param("title") String title, @Param("content") String content) { 33 | this.id = id; 34 | this.title = title; 35 | this.content = content; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Integer id) { 43 | this.id = id; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public String getAuthor() { 55 | return author; 56 | } 57 | 58 | public void setAuthor(String author) { 59 | this.author = author; 60 | } 61 | 62 | public String getContent() { 63 | return content; 64 | } 65 | 66 | public void setContent(String content) { 67 | this.content = content; 68 | } 69 | 70 | public Date getCreateTime() { 71 | return createTime; 72 | } 73 | 74 | public void setCreateTime(Date createTime) { 75 | this.createTime = createTime; 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "Article{" + 81 | "id=" + id + 82 | ", title='" + title + '\'' + 83 | ", author='" + author + '\'' + 84 | ", content='" + content + '\'' + 85 | ", createTime=" + createTime + 86 | '}'; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter3/model/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter3.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | private String content; 22 | 23 | private Date createTime; 24 | 25 | public Article() { 26 | } 27 | 28 | public Article(@Param("title") String title) { 29 | this.title = title; 30 | } 31 | 32 | public Article(@Param("id") Integer id, @Param("title") String title, @Param("content") String content) { 33 | this.id = id; 34 | this.title = title; 35 | this.content = content; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Integer id) { 43 | this.id = id; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public String getAuthor() { 55 | return author; 56 | } 57 | 58 | public void setAuthor(String author) { 59 | this.author = author; 60 | } 61 | 62 | public String getContent() { 63 | return content; 64 | } 65 | 66 | public void setContent(String content) { 67 | this.content = content; 68 | } 69 | 70 | public Date getCreateTime() { 71 | return createTime; 72 | } 73 | 74 | public void setCreateTime(Date createTime) { 75 | this.createTime = createTime; 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "Article{" + 81 | "id=" + id + 82 | ", title='" + title + '\'' + 83 | ", author='" + author + '\'' + 84 | ", content='" + content + '\'' + 85 | ", createTime=" + createTime + 86 | '}'; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/dao/ArticleDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.dao; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | import xyz.coolblog.chapter4.model.Article; 5 | import xyz.coolblog.chapter4.model.Author; 6 | 7 | 8 | /** 9 | * ArticleDao 10 | * 11 | * @author Tian ZhongBo 12 | * @date 2018-07-01 13:20:51 13 | */ 14 | public interface ArticleDao { 15 | 16 | Article findOne(@Param("id") int id); 17 | 18 | Author findAuthor(@Param("article_author_id") int authorId); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/dao/AuthorDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.dao; 2 | 3 | import java.util.List; 4 | import xyz.coolblog.chapter4.model.Author; 5 | 6 | /** 7 | * StudentDao 8 | * 9 | * @author Tian ZhongBo 10 | * @date 2018-07-14 21:53:53 11 | */ 12 | public interface AuthorDao { 13 | 14 | int insertMany(List authors); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/model/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import xyz.coolblog.constant.ArticleTypeEnum; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private ArticleTypeEnum type; 20 | 21 | private Author author; 22 | 23 | private String content; 24 | 25 | private Date createTime; 26 | 27 | public Article() { 28 | } 29 | 30 | 31 | 32 | public Article(Integer id, String title, String content) { 33 | this.id = id; 34 | this.title = title; 35 | this.content = content; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Integer id) { 43 | this.id = id; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public ArticleTypeEnum getType() { 55 | return type; 56 | } 57 | 58 | public void setType(ArticleTypeEnum type) { 59 | this.type = type; 60 | } 61 | 62 | public Author getAuthor() { 63 | return author; 64 | } 65 | 66 | public void setAuthor(Author author) { 67 | this.author = author; 68 | } 69 | 70 | public String getContent() { 71 | return content; 72 | } 73 | 74 | public void setContent(String content) { 75 | this.content = content; 76 | } 77 | 78 | public Date getCreateTime() { 79 | return createTime; 80 | } 81 | 82 | public void setCreateTime(Date createTime) { 83 | this.createTime = createTime; 84 | } 85 | 86 | @Override 87 | public String toString() { 88 | return "Article{" + 89 | "id=" + id + 90 | ", title='" + title + '\'' + 91 | ", type=" + type + 92 | ", author=" + author + 93 | ", content='" + content + '\'' + 94 | ", createTime=" + createTime + 95 | '}'; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/model/Author.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Author 7 | * 8 | * @author Tian ZhongBo 9 | * @date 2018-07-14 21:54:45 10 | */ 11 | public class Author implements Serializable { 12 | 13 | private Integer id; 14 | 15 | private String name; 16 | 17 | private Integer age; 18 | 19 | private Integer sex; 20 | 21 | private String email; 22 | 23 | public Author() { 24 | } 25 | 26 | public Author(String name, Integer age, Integer sex, String email) { 27 | this.name = name; 28 | this.age = age; 29 | this.sex = sex; 30 | this.email = email; 31 | } 32 | 33 | public Integer getId() { 34 | return id; 35 | } 36 | 37 | public void setId(Integer id) { 38 | this.id = id; 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | 45 | public void setName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public Integer getAge() { 50 | return age; 51 | } 52 | 53 | public void setAge(Integer age) { 54 | this.age = age; 55 | } 56 | 57 | public Integer getSex() { 58 | return sex; 59 | } 60 | 61 | public void setSex(Integer sex) { 62 | this.sex = sex; 63 | } 64 | 65 | public String getEmail() { 66 | return email; 67 | } 68 | 69 | public void setEmail(String email) { 70 | this.email = email; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "Author{" + 76 | "id=" + id + 77 | ", name='" + name + '\'' + 78 | ", age=" + age + 79 | ", sex=" + sex + 80 | ", email='" + email + '\'' + 81 | '}'; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/model1/Article.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.model1; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * Article 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-06-25 14:11:14 12 | */ 13 | public class Article implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | private String content; 22 | 23 | private Date createTime; 24 | 25 | public Article() { 26 | } 27 | 28 | public Article(@Param("title") String title) { 29 | this.title = title; 30 | } 31 | 32 | public Article(@Param("id") Integer id, @Param("title") String title, @Param("content") String content) { 33 | this.id = id; 34 | this.title = title; 35 | this.content = content; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Integer id) { 43 | this.id = id; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public String getAuthor() { 55 | return author; 56 | } 57 | 58 | public void setAuthor(String author) { 59 | this.author = author; 60 | } 61 | 62 | public String getContent() { 63 | return content; 64 | } 65 | 66 | public void setContent(String content) { 67 | this.content = content; 68 | } 69 | 70 | public Date getCreateTime() { 71 | return createTime; 72 | } 73 | 74 | public void setCreateTime(Date createTime) { 75 | this.createTime = createTime; 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "Article{" + 81 | "id=" + id + 82 | ", title='" + title + '\'' + 83 | ", author='" + author + '\'' + 84 | ", content='" + content + '\'' + 85 | ", createTime=" + createTime + 86 | '}'; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter4/model1/Author.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4.model1; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import xyz.coolblog.constant.SexEnum; 6 | 7 | /** 8 | * Author 9 | * 10 | * @author Tian ZhongBo 11 | * @date 2018-07-14 21:54:45 12 | */ 13 | public class Author implements Serializable { 14 | 15 | private Integer id; 16 | 17 | private String name; 18 | 19 | private Integer age; 20 | 21 | private SexEnum sex; 22 | 23 | private String email; 24 | 25 | private List
articles; 26 | 27 | public Integer getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public Integer getAge() { 44 | return age; 45 | } 46 | 47 | public void setAge(Integer age) { 48 | this.age = age; 49 | } 50 | 51 | public SexEnum getSex() { 52 | return sex; 53 | } 54 | 55 | public void setSex(SexEnum sex) { 56 | this.sex = sex; 57 | } 58 | 59 | public String getEmail() { 60 | return email; 61 | } 62 | 63 | public void setEmail(String email) { 64 | this.email = email; 65 | } 66 | 67 | public List
getArticles() { 68 | return articles; 69 | } 70 | 71 | public void setArticles(List
articles) { 72 | this.articles = articles; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Author{" + 78 | "id=" + id + 79 | ", name='" + name + '\'' + 80 | ", age=" + age + 81 | ", sex=" + sex + 82 | ", email='" + email + '\'' + 83 | ", articles=" + articles + 84 | '}'; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter6/dao/StudentDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter6.dao; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | import xyz.coolblog.chapter6.model.Student; 5 | 6 | /** 7 | * StudentDao 8 | * 9 | * @author Tian ZhongBo 10 | * @date 2018-07-14 21:53:53 11 | */ 12 | public interface StudentDao { 13 | 14 | Student findOne(@Param("id") Integer id); 15 | 16 | int update(@Param("id") Integer id, @Param("name") String name); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter6/model/Student.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter6.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Student 7 | * 8 | * @author Tian ZhongBo 9 | * @date 2018-08-24 20:24:27 10 | */ 11 | public class Student implements Serializable { 12 | 13 | private Integer id; 14 | 15 | private String name; 16 | 17 | private Integer age; 18 | 19 | public Integer getId() { 20 | return id; 21 | } 22 | 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public Integer getAge() { 36 | return age; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "Student{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", age=" + age + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter7/dao/StudentDao.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter7.dao; 2 | 3 | import java.util.List; 4 | import org.apache.ibatis.annotations.Param; 5 | import org.apache.ibatis.session.RowBounds; 6 | import xyz.coolblog.chapter7.model.Student; 7 | 8 | /** 9 | * StudentDao 10 | * 11 | * @author Tian ZhongBo 12 | * @date 2018-08-25 18:25:33 13 | */ 14 | public interface StudentDao { 15 | 16 | List findByPaging(@Param("id") Integer id, RowBounds rb); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/chapter7/model/Student.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter7.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Student 7 | * 8 | * @author Tian ZhongBo 9 | * @date 2018-08-24 20:24:27 10 | */ 11 | public class Student implements Serializable { 12 | 13 | private Integer id; 14 | 15 | private String name; 16 | 17 | private Integer age; 18 | 19 | public Integer getId() { 20 | return id; 21 | } 22 | 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public Integer getAge() { 36 | return age; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "Student{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", age=" + age + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/constant/ArticleCategoryEnum.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.constant; 2 | 3 | /** 4 | * ArticleCategoryEnum 5 | * 6 | * @author Tian ZhongBo 7 | * @date 2018-07-15 15:21:56 8 | */ 9 | public enum ArticleCategoryEnum { 10 | 11 | JAVA(1), 12 | DUBBO(2), 13 | SPRING(3), 14 | MYBATIS(4); 15 | 16 | private int code; 17 | 18 | ArticleCategoryEnum(int code) { 19 | this.code = code; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/constant/ArticleTypeEnum.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.constant; 2 | 3 | /** 4 | * ArticleTypeEnum 5 | * 6 | * @author Tian ZhongBo 7 | * @date 2018-07-08 10:29:29 8 | */ 9 | public enum ArticleTypeEnum { 10 | 11 | JAVA(1), 12 | DUBBO(2), 13 | SPRING(4), 14 | MYBATIS(8); 15 | 16 | private int code; 17 | 18 | ArticleTypeEnum(int code) { 19 | this.code = code; 20 | } 21 | 22 | public int code() { 23 | return code; 24 | } 25 | 26 | public static ArticleTypeEnum find(int code) { 27 | for (ArticleTypeEnum at : ArticleTypeEnum.values()) { 28 | if (at.code == code) { 29 | return at; 30 | } 31 | } 32 | 33 | return null; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/constant/SexEnum.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.constant; 2 | 3 | /** 4 | * SexEnum 5 | * 6 | * @author Tian ZhongBo 7 | * @date 2018-07-14 21:57:23 8 | */ 9 | public enum SexEnum { 10 | MAN, 11 | FEMALE, 12 | UNKNOWN; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/mybatis/ArticleTypeHandler.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.mybatis; 2 | 3 | import java.sql.CallableStatement; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import org.apache.ibatis.type.BaseTypeHandler; 8 | import org.apache.ibatis.type.JdbcType; 9 | import org.apache.ibatis.type.MappedTypes; 10 | import xyz.coolblog.constant.ArticleTypeEnum; 11 | import xyz.coolblog.model.Article; 12 | 13 | /** 14 | * ArticleTypeHandler 15 | * 16 | * @author Tian ZhongBo 17 | * @date 2018-07-08 10:31:13 18 | */ 19 | @MappedTypes(value = ArticleTypeEnum.class) 20 | public class ArticleTypeHandler extends BaseTypeHandler { 21 | 22 | @Override 23 | public void setNonNullParameter(PreparedStatement ps, int i, ArticleTypeEnum parameter, JdbcType jdbcType) 24 | throws SQLException { 25 | ps.setInt(i, parameter.code()); 26 | } 27 | 28 | @Override 29 | public ArticleTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException { 30 | int code = rs.getInt(columnName); 31 | return ArticleTypeEnum.find(code); 32 | } 33 | 34 | @Override 35 | public ArticleTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 36 | int code = rs.getInt(columnIndex); 37 | return ArticleTypeEnum.find(code); 38 | } 39 | 40 | @Override 41 | public ArticleTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 42 | int code = cs.getInt(columnIndex); 43 | return ArticleTypeEnum.find(code); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/mybatis/ExamplePlugin.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.mybatis; 2 | 3 | import java.lang.reflect.Method; 4 | import java.util.Properties; 5 | import org.apache.ibatis.executor.Executor; 6 | import org.apache.ibatis.mapping.MappedStatement; 7 | import org.apache.ibatis.plugin.Interceptor; 8 | import org.apache.ibatis.plugin.Intercepts; 9 | import org.apache.ibatis.plugin.Invocation; 10 | import org.apache.ibatis.plugin.Plugin; 11 | import org.apache.ibatis.plugin.Signature; 12 | import org.apache.ibatis.session.ResultHandler; 13 | import org.apache.ibatis.session.RowBounds; 14 | 15 | /** 16 | * ExamplePlugin 17 | * 18 | * @author Tian ZhongBo 19 | * @date 2018-07-06 11:31:50 20 | */ 21 | @Intercepts({@Signature( 22 | type= Executor.class, 23 | method = "query", 24 | args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) 25 | public class ExamplePlugin implements Interceptor { 26 | 27 | private Properties properties; 28 | 29 | @Override 30 | public Object intercept(Invocation invocation) throws Throwable { 31 | Method method = invocation.getMethod(); 32 | Object[] args = invocation.getArgs(); 33 | System.out.println(method + " - " + args); 34 | return invocation.proceed(); 35 | } 36 | 37 | @Override 38 | public Object plugin(Object target) { 39 | return Plugin.wrap(target, this); 40 | } 41 | 42 | @Override 43 | public void setProperties(Properties properties) { 44 | this.properties = properties; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/mybatis/LoggableObjectFactory.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.mybatis; 2 | 3 | import java.util.List; 4 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | /** 9 | * LoggableObjectFactory 10 | * 11 | * @author Tian ZhongBo 12 | * @date 2018-07-06 10:22:26 13 | */ 14 | public class LoggableObjectFactory extends DefaultObjectFactory { 15 | 16 | private Logger logger = LoggerFactory.getLogger(LoggableObjectFactory.class); 17 | 18 | @Override 19 | public T create(Class type) { 20 | // System.out.println("using default constructor create " + type); 21 | // logger.info("using default constructor create {}", type); 22 | return super.create(type); 23 | } 24 | 25 | @Override 26 | public T create(Class type, List> constructorArgTypes, List constructorArgs) { 27 | // System.out.println("using constructor create " + type + ", constructorArgs = " + constructorArgs); 28 | // logger.info("using constructor whith args {} create {}", constructorArgs, type); 29 | return super.create(type, constructorArgTypes, constructorArgs); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/xyz/coolblog/mybatis/MySqlPagingPlugin.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.mybatis; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.Properties; 5 | import org.apache.ibatis.builder.StaticSqlSource; 6 | import org.apache.ibatis.cache.CacheKey; 7 | import org.apache.ibatis.executor.Executor; 8 | import org.apache.ibatis.mapping.BoundSql; 9 | import org.apache.ibatis.mapping.MappedStatement; 10 | import org.apache.ibatis.mapping.SqlSource; 11 | import org.apache.ibatis.plugin.Interceptor; 12 | import org.apache.ibatis.plugin.Intercepts; 13 | import org.apache.ibatis.plugin.Invocation; 14 | import org.apache.ibatis.plugin.Plugin; 15 | import org.apache.ibatis.plugin.Signature; 16 | import org.apache.ibatis.session.ResultHandler; 17 | import org.apache.ibatis.session.RowBounds; 18 | 19 | /** 20 | * MySqlPagingPlugin 21 | * 22 | * @author Tian ZhongBo 23 | * @date 2018-08-25 17:37:41 24 | */ 25 | @Intercepts({ 26 | @Signature( 27 | type = Executor.class, 28 | method = "query", 29 | args ={MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} 30 | ) 31 | }) 32 | public class MySqlPagingPlugin implements Interceptor { 33 | 34 | private static final Integer MAPPED_STATEMENT_INDEX = 0; 35 | private static final Integer PARAMETER_INDEX = 1; 36 | private static final Integer ROW_BOUNDS_INDEX = 2; 37 | 38 | @Override 39 | public Object intercept(Invocation invocation) throws Throwable { 40 | Object[] args = invocation.getArgs(); 41 | RowBounds rb = (RowBounds) args[ROW_BOUNDS_INDEX]; 42 | if (rb == RowBounds.DEFAULT) { 43 | return invocation.proceed(); 44 | } 45 | args[ROW_BOUNDS_INDEX] = RowBounds.DEFAULT; 46 | 47 | MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX]; 48 | BoundSql boundSql = ms.getBoundSql(args[PARAMETER_INDEX]); 49 | 50 | System.out.println(); 51 | String sql = boundSql.getSql(); 52 | String limit = String.format("LIMIT %d,%d", rb.getOffset(), rb.getLimit()); 53 | sql = sql + " " + limit; 54 | 55 | SqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, boundSql.getParameterMappings()); 56 | 57 | Field field = MappedStatement.class.getDeclaredField("sqlSource"); 58 | field.setAccessible(true); 59 | field.set(ms, sqlSource); 60 | 61 | return invocation.proceed(); 62 | } 63 | 64 | @Override 65 | public Object plugin(Object target) { 66 | return Plugin.wrap(target, this); 67 | } 68 | 69 | @Override 70 | public void setProperties(Properties properties) { 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/HibernateTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.List; 6 | import java.util.Objects; 7 | import javax.persistence.criteria.CriteriaBuilder; 8 | import javax.persistence.criteria.CriteriaQuery; 9 | import javax.persistence.criteria.Predicate; 10 | import javax.persistence.criteria.Root; 11 | import org.hibernate.Session; 12 | import org.hibernate.SessionFactory; 13 | import org.hibernate.cfg.Configuration; 14 | import org.hibernate.query.Query; 15 | import org.junit.After; 16 | import org.junit.Before; 17 | import org.junit.Test; 18 | import xyz.coolblog.chapter1.model.Article; 19 | 20 | /** 21 | * SpringJdbcTest 22 | * 23 | * @author Tian ZhongBo 24 | * @date 2018-07-12 16:43:04 25 | */ 26 | public class HibernateTest { 27 | 28 | private SessionFactory buildSessionFactory; 29 | 30 | @Before 31 | public void init() { 32 | Configuration configuration = new Configuration(); 33 | configuration.configure("chapter1/hibernate.cfg.xml"); 34 | buildSessionFactory = configuration.buildSessionFactory(); 35 | } 36 | 37 | @After 38 | public void destroy() { 39 | buildSessionFactory.close(); 40 | } 41 | 42 | @Test 43 | public void testORM() { 44 | System.out.println("-----------------------------✨ ORM Query ✨--------------------------"); 45 | 46 | Session session = null; 47 | try { 48 | session = buildSessionFactory.openSession(); 49 | int id = 6; 50 | Article article = session.get(Article.class, id); 51 | System.out.println("ORM Query Result: "); 52 | System.out.println(article); 53 | System.out.println(); 54 | } finally { 55 | if (Objects.nonNull(session)) { 56 | session.close(); 57 | } 58 | } 59 | 60 | } 61 | 62 | @Test 63 | public void testHQL() { 64 | System.out.println("-----------------------------✨ HQL Query ✨+--------------------------"); 65 | Session session = null; 66 | try { 67 | session = buildSessionFactory.openSession(); 68 | String hql = "from Article where author = :author and create_time > :createTime"; 69 | Query query = session.createQuery(hql); 70 | query.setParameter("author", "coolblog.xyz"); 71 | query.setParameter("createTime", "2018.06.10"); 72 | 73 | List
articles = query.list(); 74 | System.out.println("HQL Query Result: "); 75 | articles.forEach(System.out::println); 76 | System.out.println(); 77 | } finally { 78 | if (Objects.nonNull(session)) { 79 | session.close(); 80 | } 81 | } 82 | } 83 | 84 | @Test 85 | public void testJpaCriteria() throws ParseException { 86 | System.out.println("---------------------------✨ JPA Criteria ✨------------------------"); 87 | 88 | Session session = null; 89 | try { 90 | session = buildSessionFactory.openSession(); 91 | CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); 92 | CriteriaQuery
criteriaQuery = criteriaBuilder.createQuery(Article.class); 93 | 94 | // 定义 FROM 子句 95 | Root
article = criteriaQuery.from(Article.class); 96 | 97 | // 构建查询条件 98 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd"); 99 | Predicate greaterThan = criteriaBuilder.greaterThan(article.get("createTime"), sdf.parse("2018.06.10")); 100 | Predicate equal = criteriaBuilder.equal(article.get("author"), "coolblog.xyz"); 101 | 102 | // 通过具有语义化的方法构建 SQL,等价于 SELECT ... FROM article WHERE ... AND ... 103 | criteriaQuery.select(article).where(equal, greaterThan); 104 | 105 | Query
query = session.createQuery(criteriaQuery); 106 | List
articles = query.getResultList(); 107 | 108 | System.out.println("JPA Criteria Query Result: "); 109 | articles.forEach(System.out::println); 110 | } finally { 111 | if (Objects.nonNull(session)) { 112 | session.close(); 113 | } 114 | } 115 | 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/JdbcTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.sql.Statement; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import org.junit.Test; 11 | import xyz.coolblog.chapter1.model.Article; 12 | 13 | /** 14 | * JdbcTest 15 | * 16 | * @author Tian ZhongBo 17 | * @date 2018-07-12 00:09:10 18 | */ 19 | public class JdbcTest { 20 | 21 | @Test 22 | public void testJdbc() { 23 | String url = "jdbc:mysql://localhost:3306/myblog?user=root&password=1234&useUnicode=true&characterEncoding=UTF8&useSSL=false"; 24 | 25 | Connection conn = null; 26 | try { 27 | Class.forName("com.mysql.cj.jdbc.Driver"); 28 | conn = DriverManager.getConnection(url); 29 | 30 | String author = "coolblog.xyz"; 31 | String date = "2018.06.10"; 32 | String sql = "SELECT id, title, author, content, create_time FROM blog_article WHERE author = '" + author + "' AND create_time > '" + date + "'"; 33 | 34 | Statement stmt = conn.createStatement(); 35 | ResultSet rs = stmt.executeQuery(sql); 36 | List
articles = new ArrayList<>(rs.getRow()); 37 | while (rs.next()) { 38 | Article article = new Article(); 39 | article.setId(rs.getInt("id")); 40 | article.setTitle(rs.getString("title")); 41 | article.setAuthor(rs.getString("author")); 42 | article.setContent(rs.getString("content")); 43 | article.setCreateTime(rs.getDate("create_time")); 44 | articles.add(article); 45 | } 46 | System.out.println("Query SQL ==> " + sql); 47 | System.out.println("Query Result: "); 48 | articles.forEach(System.out::println); 49 | } catch (ClassNotFoundException e) { 50 | e.printStackTrace(); 51 | } catch (SQLException e) { 52 | e.printStackTrace(); 53 | } finally { 54 | try { 55 | conn.close(); 56 | } catch (SQLException e) { 57 | e.printStackTrace(); 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/MyBatisTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | import org.apache.ibatis.io.Resources; 7 | import org.apache.ibatis.session.SqlSession; 8 | import org.apache.ibatis.session.SqlSessionFactory; 9 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import xyz.coolblog.chapter1.dao.ArticleDao; 13 | import xyz.coolblog.chapter1.model.Article; 14 | 15 | /** 16 | * MyBatisTest 17 | * 18 | * @author Tian ZhongBo 19 | * @date 2018-07-01 13:24:55 20 | */ 21 | public class MyBatisTest { 22 | 23 | private SqlSessionFactory sqlSessionFactory; 24 | 25 | @Before 26 | public void prepare() throws IOException { 27 | String resource = "chapter1/mybatis-config.xml"; 28 | InputStream inputStream = Resources.getResourceAsStream(resource); 29 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 30 | inputStream.close(); 31 | } 32 | 33 | @Test 34 | public void testMyBatis() throws IOException { 35 | SqlSession session = sqlSessionFactory.openSession(); 36 | try { 37 | ArticleDao articleDao = session.getMapper(ArticleDao.class); 38 | List
articles = articleDao. 39 | findByAuthorAndCreateTime("coolblog.xyz", "2018-06-10"); 40 | 41 | } finally { 42 | session.commit(); 43 | session.close(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/MyBatisTest2.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | import org.apache.ibatis.io.Resources; 8 | import org.apache.ibatis.session.SqlSession; 9 | import org.apache.ibatis.session.SqlSessionFactory; 10 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import xyz.coolblog.chapter1.dao2.AuthorDao; 14 | import xyz.coolblog.chapter1.dao2.ArticleDao; 15 | import xyz.coolblog.chapter1.model2.Author; 16 | import xyz.coolblog.chapter1.model2.Article; 17 | 18 | /** 19 | * MyBatisTest 20 | * 21 | * @author Tian ZhongBo 22 | * @date 2018-07-01 13:24:55 23 | */ 24 | public class MyBatisTest2 { 25 | 26 | private SqlSessionFactory sqlSessionFactory; 27 | 28 | @Before 29 | public void prepare() throws IOException { 30 | String resource = "chapter1/mybatis-config2.xml"; 31 | InputStream inputStream = Resources.getResourceAsStream(resource); 32 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 33 | inputStream.close(); 34 | } 35 | 36 | @Test 37 | public void testOne2One() { 38 | SqlSession session = sqlSessionFactory.openSession(); 39 | try { 40 | ArticleDao articleDao = session.getMapper(ArticleDao.class); 41 | Article article = articleDao.findOne(1); 42 | 43 | Author author = article.getAuthor(); 44 | article.setAuthor(null); 45 | 46 | System.out.println("\nauthor info:"); 47 | System.out.println(author); 48 | System.out.println("\narticles info:"); 49 | System.out.println(article); 50 | } finally { 51 | session.close(); 52 | } 53 | } 54 | 55 | @Test 56 | public void testOne2Many() { 57 | SqlSession session = sqlSessionFactory.openSession(); 58 | try { 59 | AuthorDao authorDao = session.getMapper(AuthorDao.class); 60 | Author author = authorDao.findOne(1); 61 | 62 | List
arts = author.getArticles(); 63 | List
articles = Arrays.asList(arts.toArray(new Article[arts.size()])); 64 | arts.clear(); 65 | 66 | System.out.println("\nauthor info:"); 67 | System.out.println(author); 68 | System.out.println("\narticles info:"); 69 | articles.forEach(System.out::println); 70 | } finally { 71 | session.close(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/SpringJdbcTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.util.List; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.jdbc.core.JdbcTemplate; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | import xyz.coolblog.chapter1.model.Article; 11 | 12 | /** 13 | * SpringJdbcTest 14 | * 15 | * @author Tian ZhongBo 16 | * @date 2018-07-12 16:43:04 17 | */ 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration("classpath:chapter1/application.xml") 20 | public class SpringJdbcTest { 21 | 22 | @Autowired 23 | private JdbcTemplate jdbcTemplate; 24 | 25 | @Test 26 | public void testSpringJdbc() { 27 | String author = "coolblog.xyz"; 28 | String date = "2018.06.10"; 29 | String sql = "SELECT id, title, author, content, create_time" 30 | + " FROM blog_article" 31 | + " WHERE author = '" + author + "' AND create_time > '" + date + "'"; 32 | List
articles = jdbcTemplate.query(sql, (rs, rowNum) -> { 33 | Article article = new Article(); 34 | article.setId(rs.getInt("id")); 35 | article.setTitle(rs.getString("title")); 36 | article.setAuthor(rs.getString("author")); 37 | article.setContent(rs.getString("content")); 38 | article.setCreateTime(rs.getDate("create_time")); 39 | return article; 40 | }); 41 | 42 | System.out.println("Query SQL ==> " + sql); 43 | System.out.println("Spring JDBC Query Result: "); 44 | articles.forEach(System.out::println); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter1/SpringWithMyBatisTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter1; 2 | 3 | import java.util.Arrays; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.BeansException; 8 | import org.springframework.beans.factory.ListableBeanFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.context.ApplicationContext; 11 | import org.springframework.context.ApplicationContextAware; 12 | import org.springframework.test.context.ContextConfiguration; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | import xyz.coolblog.chapter1.dao2.ArticleDao; 15 | import xyz.coolblog.chapter1.dao2.AuthorDao; 16 | import xyz.coolblog.chapter1.model2.Article; 17 | import xyz.coolblog.chapter1.model2.Author; 18 | 19 | /** 20 | * SpringWithMyBatisTest 21 | * 22 | * @author Tian ZhongBo 23 | * @date 2018-07-15 17:09:47 24 | */ 25 | 26 | @RunWith(SpringJUnit4ClassRunner.class) 27 | @ContextConfiguration("classpath:chapter1/application-mybatis.xml") 28 | public class SpringWithMyBatisTest implements ApplicationContextAware { 29 | 30 | private ApplicationContext applicationContext; 31 | 32 | @Autowired 33 | private AuthorDao authorDao; 34 | 35 | @Autowired 36 | private ArticleDao articleDao; 37 | 38 | @Before 39 | public void printBeanInfo() { 40 | ListableBeanFactory lbf = applicationContext; 41 | String[] beanNames = lbf.getBeanDefinitionNames(); 42 | Arrays.sort(beanNames); 43 | 44 | System.out.println(); 45 | System.out.println("----------------☆ bean name ☆---------------"); 46 | Arrays.asList(beanNames).subList(0, 5).forEach(System.out::println); 47 | System.out.println(); 48 | 49 | AuthorDao authorDao = (AuthorDao) applicationContext.getBean("authorDao"); 50 | ArticleDao articleDao = (ArticleDao) applicationContext.getBean("articleDao"); 51 | 52 | System.out.println("-------------☆ bean class info ☆--------------"); 53 | System.out.println("StudentDao Class: " + authorDao.getClass()); 54 | System.out.println("ArticleDao Class: " + articleDao.getClass()); 55 | System.out.println("\n--------xxxx---------xxxx---------xxxx--------\n"); 56 | } 57 | 58 | 59 | @Test 60 | public void testOne2One() { 61 | Article article = articleDao.findOne(1); 62 | 63 | Author author = article.getAuthor(); 64 | article.setAuthor(null); 65 | 66 | System.out.println(); 67 | System.out.println("author info:"); 68 | System.out.println(author); 69 | System.out.println(); 70 | System.out.println("articles info:"); 71 | System.out.println(article); 72 | } 73 | 74 | @Test 75 | public void testOne2Many() { 76 | Author author = authorDao.findOne(1); 77 | 78 | System.out.println(); 79 | System.out.println("author info:"); 80 | System.out.println(author); 81 | System.out.println(); 82 | System.out.println("articles info:"); 83 | author.getArticles().forEach(System.out::println); 84 | } 85 | 86 | @Override 87 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 88 | this.applicationContext = applicationContext; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter2/MetaClassTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter2; 2 | 3 | import java.lang.reflect.Method; 4 | import java.lang.reflect.Type; 5 | import java.util.Arrays; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | import org.apache.ibatis.reflection.DefaultReflectorFactory; 9 | import org.apache.ibatis.reflection.MetaClass; 10 | import org.junit.Test; 11 | import xyz.coolblog.mybatis.ArticleTypeHandler; 12 | 13 | /** 14 | * MetaClassTest 15 | * 16 | * @author Tian ZhongBo 17 | * @date 2018-07-18 14:07:01 18 | */ 19 | public class MetaClassTest { 20 | 21 | @Test 22 | public void testParameterizedType() { 23 | Method[] ms = ArticleTypeHandler.class.getMethods(); 24 | Arrays.asList(ms).forEach(m -> { 25 | System.out.println("-------" + m.getName() + "--------"); 26 | Type[] pts = m.getGenericParameterTypes(); 27 | Arrays.stream(pts).forEach(t -> { 28 | System.out.println(t.getTypeName()); 29 | }); 30 | }); 31 | } 32 | 33 | @Test 34 | public void testHasSetter() { 35 | MetaClass authorMeta = MetaClass.forClass(Author.class, new DefaultReflectorFactory()); 36 | 37 | System.out.println("\n------------☆ Author ☆------------"); 38 | System.out.println("id -> " + authorMeta.hasSetter("id")); 39 | System.out.println("name -> " + authorMeta.hasSetter("name")); 40 | System.out.println("age -> " + authorMeta.hasSetter("age")); 41 | System.out.println("articles -> " + authorMeta.hasSetter("articles")); 42 | System.out.println("articles[] -> " + authorMeta.hasSetter("articles[]")); 43 | System.out.println("title -> " + authorMeta.hasSetter("title")); 44 | 45 | MetaClass articleMeta = MetaClass.forClass(Article.class, new DefaultReflectorFactory()); 46 | System.out.println("\n------------☆ Article ☆------------"); 47 | System.out.println("id -> " + articleMeta.hasSetter("id")); 48 | System.out.println("title -> " + articleMeta.hasSetter("title")); 49 | System.out.println("content -> " + articleMeta.hasSetter("content")); 50 | System.out.println("author.id -> " + articleMeta.hasSetter("author.id")); 51 | System.out.println("author.name -> " + articleMeta.hasSetter("author.name")); 52 | System.out.println(); 53 | } 54 | 55 | private class Author { 56 | private Integer id; 57 | private String name; 58 | private Integer age; 59 | private Article[] articles; 60 | 61 | // 省略 getter/setter 和 toString 62 | 63 | public Integer getId() { 64 | return id; 65 | } 66 | 67 | public void setId(Integer id) { 68 | this.id = id; 69 | } 70 | 71 | public String getName() { 72 | return name; 73 | } 74 | 75 | public void setName(String name) { 76 | this.name = name; 77 | } 78 | 79 | public Integer getAge() { 80 | return age; 81 | } 82 | 83 | public void setAge(Integer age) { 84 | this.age = age; 85 | } 86 | 87 | public Article[] getArticles() { 88 | return articles; 89 | } 90 | 91 | public void setArticles(Article[] articles) { 92 | this.articles = articles; 93 | } 94 | } 95 | 96 | private class Article { 97 | private Integer id; 98 | private String title; 99 | private Author author; 100 | private String content; 101 | 102 | // 省略 getter/setter 和 toString 103 | 104 | public Integer getId() { 105 | return id; 106 | } 107 | 108 | public void setId(Integer id) { 109 | this.id = id; 110 | } 111 | 112 | public String getTitle() { 113 | return title; 114 | } 115 | 116 | public void setTitle(String title) { 117 | this.title = title; 118 | } 119 | 120 | public Author getAuthor() { 121 | return author; 122 | } 123 | 124 | public void setAuthor(Author author) { 125 | this.author = author; 126 | } 127 | 128 | public String getContent() { 129 | return content; 130 | } 131 | 132 | public void setContent(String content) { 133 | this.content = content; 134 | } 135 | } 136 | } 137 | 138 | 139 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter3/ResultMapTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter3; 2 | 3 | import java.io.InputStream; 4 | import org.apache.ibatis.builder.xml.XMLMapperBuilder; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.mapping.ResultMap; 7 | import org.apache.ibatis.mapping.ResultMapping; 8 | import org.apache.ibatis.session.Configuration; 9 | import org.junit.Test; 10 | 11 | /** 12 | * ResultMapTest 13 | * 14 | * @author Tian ZhongBo 15 | * @date 2018-07-26 22:04:47 16 | */ 17 | public class ResultMapTest { 18 | 19 | @Test 20 | public void printResultMapInfo() throws Exception { 21 | Configuration configuration = new Configuration(); 22 | String resource = "chapter3/mapper/ArticleMapper.xml"; 23 | InputStream inputStream = Resources.getResourceAsStream(resource); 24 | XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); 25 | builder.parse(); 26 | 27 | ResultMap resultMap = configuration.getResultMap("articleResult"); 28 | 29 | System.out.println("\n-------------------+✨ mappedColumns ✨+--------------------"); 30 | System.out.println(resultMap.getMappedColumns()); 31 | 32 | System.out.println("\n------------------+✨ mappedProperties ✨+------------------"); 33 | System.out.println(resultMap.getMappedProperties()); 34 | 35 | System.out.println("\n------------------+✨ idResultMappings ✨+------------------"); 36 | resultMap.getIdResultMappings().forEach(rm -> System.out.println(simplify(rm))); 37 | 38 | System.out.println("\n---------------+✨ propertyResultMappings ✨+---------------"); 39 | resultMap.getPropertyResultMappings().forEach(rm -> System.out.println(simplify(rm))); 40 | 41 | System.out.println("\n-------------+✨ constructorResultMappings ✨+--------------"); 42 | resultMap.getConstructorResultMappings().forEach(rm -> System.out.println(simplify(rm))); 43 | 44 | System.out.println("\n------------------+✨ resultMappings ✨+--------------------"); 45 | resultMap.getResultMappings().forEach(rm -> System.out.println(simplify(rm))); 46 | 47 | System.out.println(); 48 | inputStream.close(); 49 | } 50 | 51 | private String simplify(ResultMapping resultMapping) { 52 | return String.format("ResultMapping{column='%s', property='%s', flags=%s, ...}", 53 | resultMapping.getColumn(), resultMapping.getProperty(), resultMapping.getFlags()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter4/InsertManyTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import org.apache.ibatis.io.Resources; 8 | import org.apache.ibatis.session.SqlSession; 9 | import org.apache.ibatis.session.SqlSessionFactory; 10 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import xyz.coolblog.chapter4.dao.AuthorDao; 14 | import xyz.coolblog.chapter4.model.Author; 15 | 16 | 17 | /** 18 | * OneToOneTest 19 | * 20 | * @author Tian ZhongBo 21 | * @date 2018-08-13 10:06:11 22 | */ 23 | public class InsertManyTest { 24 | 25 | private SqlSessionFactory sqlSessionFactory; 26 | 27 | @Before 28 | public void prepare() throws IOException { 29 | String resource = "chapter4/mybatis-config2.xml"; 30 | InputStream inputStream = Resources.getResourceAsStream(resource); 31 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 32 | inputStream.close(); 33 | } 34 | 35 | @Test 36 | public void testInsertMany() { 37 | SqlSession session = sqlSessionFactory.openSession(); 38 | try { 39 | List authors = new ArrayList<>(); 40 | authors.add(new Author("tianxiaobo-1", 20, 0, "coolblog.xyz@outlook.com")); 41 | authors.add(new Author("tianxiaobo-2", 18, 0, "coolblog.xyz@outlook.com")); 42 | 43 | System.out.println("\nBefore Insert: "); 44 | authors.forEach(author -> System.out.println(" " + author)); 45 | System.out.println(); 46 | 47 | AuthorDao authorDao = session.getMapper(AuthorDao.class); 48 | authorDao.insertMany(authors); 49 | session.commit(); 50 | 51 | System.out.println("\nAfter Insert: "); 52 | authors.forEach(author -> System.out.println(" " + author)); 53 | } finally { 54 | session.close(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter4/OneToOneTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import xyz.coolblog.chapter4.dao.ArticleDao; 12 | import xyz.coolblog.chapter4.model.Article; 13 | import xyz.coolblog.chapter4.model.Author; 14 | 15 | /** 16 | * OneToOneTest 17 | * 18 | * @author Tian ZhongBo 19 | * @date 2018-08-13 10:06:11 20 | */ 21 | public class OneToOneTest { 22 | 23 | private SqlSessionFactory sqlSessionFactory; 24 | 25 | @Before 26 | public void prepare() throws IOException { 27 | String resource = "chapter4/mybatis-config.xml"; 28 | InputStream inputStream = Resources.getResourceAsStream(resource); 29 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 30 | inputStream.close(); 31 | } 32 | 33 | @Test 34 | public void testOne2One() { 35 | SqlSession session = sqlSessionFactory.openSession(); 36 | try { 37 | ArticleDao articleDao = session.getMapper(ArticleDao.class); 38 | Article article = articleDao.findOne(1); 39 | Author author = article.getAuthor(); 40 | 41 | System.out.println("\narticles info:"); 42 | System.out.println(article); 43 | 44 | System.out.println("\nauthor info:"); 45 | System.out.println(author); 46 | } finally { 47 | session.close(); 48 | } 49 | } 50 | 51 | @Test 52 | public void testOne2One2() { 53 | SqlSession session = sqlSessionFactory.openSession(); 54 | try { 55 | ArticleDao articleDao = session.getMapper(ArticleDao.class); 56 | Article article = articleDao.findOne(1); 57 | 58 | System.out.println("\narticles info:"); 59 | System.out.println(article); 60 | 61 | System.out.println("\n延迟加载 author 字段:"); 62 | 63 | Author author = article.getAuthor(); 64 | System.out.println("\narticles info:"); 65 | System.out.println(article); 66 | System.out.println("\nauthor info:"); 67 | System.out.println(author); 68 | } finally { 69 | session.close(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter4/ParamNameResolverTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4; 2 | 3 | import java.lang.reflect.Field; 4 | import java.lang.reflect.Method; 5 | import org.apache.ibatis.annotations.Param; 6 | import org.apache.ibatis.reflection.ParamNameResolver; 7 | import org.apache.ibatis.session.Configuration; 8 | import org.apache.ibatis.session.RowBounds; 9 | import org.junit.Test; 10 | import xyz.coolblog.chapter4.model1.Article; 11 | 12 | /** 13 | * ParamNameResolverTest 14 | * 15 | * @author Tian ZhongBo 16 | * @date 2018-08-01 20:39:13 17 | */ 18 | public class ParamNameResolverTest { 19 | 20 | @Test 21 | public void test() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { 22 | Configuration config = new Configuration(); 23 | config.setUseActualParamName(false); 24 | Method method = ArticleMapper.class.getMethod("select", Integer.class, String.class, RowBounds.class, Article.class); 25 | 26 | ParamNameResolver resolver = new ParamNameResolver(config, method); 27 | Field field = resolver.getClass().getDeclaredField("names"); 28 | field.setAccessible(true); 29 | Object names = field.get(resolver); 30 | 31 | System.out.println("names: " + names); 32 | } 33 | 34 | @Test 35 | public void testGetNamedParams() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException { 36 | Configuration config = new Configuration(); 37 | config.setUseActualParamName(false); 38 | Method method = ArticleMapper.class.getMethod("select", Integer.class, String.class, RowBounds.class, Integer.class); 39 | 40 | ParamNameResolver resolver = new ParamNameResolver(config, method); 41 | 42 | System.out.println("names: " + resolver.getNamedParams(new Object[]{1, "coolblog.xyz", new RowBounds(), 20})); 43 | } 44 | 45 | class ArticleMapper { 46 | 47 | public void select(@Param("id") Integer id, @Param("author") String author, RowBounds rb, Article article) {} 48 | 49 | public void select(@Param("id") Integer id, @Param("author") String author, RowBounds rb, Integer age) {} 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter4/SqlNodeTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4; 2 | 3 | import java.io.IOException; 4 | import java.util.Arrays; 5 | import org.apache.ibatis.binding.MapperMethod.ParamMap; 6 | import org.apache.ibatis.scripting.xmltags.DynamicContext; 7 | import org.apache.ibatis.scripting.xmltags.MixedSqlNode; 8 | import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode; 9 | import org.apache.ibatis.scripting.xmltags.TextSqlNode; 10 | import org.apache.ibatis.scripting.xmltags.WhereSqlNode; 11 | import org.apache.ibatis.session.Configuration; 12 | import org.junit.Test; 13 | import xyz.coolblog.chapter4.model1.Article; 14 | 15 | /** 16 | * AutoMappingTest 17 | * 18 | * @author Tian ZhongBo 19 | * @date 2018-07-25 11:46:47 20 | */ 21 | public class SqlNodeTest { 22 | 23 | @Test 24 | public void testTextSqlNode() throws IOException { 25 | TextSqlNode tsn = new TextSqlNode("SELECT * FROM article WHERE author = '${article.author}'"); 26 | Article article = new Article(); 27 | article.setAuthor("tianxiaobo"); 28 | ParamMap pm = new ParamMap<>(); 29 | pm.put("article", article); 30 | DynamicContext dc = new DynamicContext(new Configuration(), pm); 31 | tsn.apply(dc); 32 | System.out.println(dc.getSql()); 33 | 34 | article.setAuthor("tianxiaobo'; DELETE FROM article;#"); 35 | dc = new DynamicContext(new Configuration(), pm); 36 | tsn.apply(dc); 37 | System.out.println(dc.getSql()); 38 | } 39 | 40 | @Test 41 | public void testWhereSqlNode() throws IOException { 42 | String sqlFragment = "AND id = #{id}"; 43 | MixedSqlNode msn = new MixedSqlNode(Arrays.asList(new StaticTextSqlNode(sqlFragment))); 44 | WhereSqlNode wsn = new WhereSqlNode(new Configuration(), msn); 45 | DynamicContext dc = new DynamicContext(new Configuration(), new ParamMap<>()); 46 | wsn.apply(dc); 47 | System.out.println("解析前:" + sqlFragment); 48 | System.out.println("解析后:" + dc.getSql()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter4/SqlSourceBuilderTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter4; 2 | 3 | import java.util.HashMap; 4 | import org.apache.ibatis.builder.SqlSourceBuilder; 5 | import org.apache.ibatis.mapping.BoundSql; 6 | import org.apache.ibatis.mapping.SqlSource; 7 | import org.apache.ibatis.session.Configuration; 8 | import org.junit.Test; 9 | import xyz.coolblog.chapter4.model1.Author; 10 | 11 | /** 12 | * SqlSourceBuilderTest 13 | * 14 | * @author Tian ZhongBo 15 | * @date 2018-08-09 20:40:08 16 | */ 17 | public class SqlSourceBuilderTest { 18 | 19 | @Test 20 | public void test() { 21 | // String sql = "SELECT * FROM Author WHERE name = #{name} AND age = #{age}"; 22 | String sql = "SELECT * FROM Author WHERE age = #{age,javaType=int,jdbcType=NUMERIC}"; 23 | SqlSourceBuilder sqlSourceBuilder = new SqlSourceBuilder(new Configuration()); 24 | SqlSource sqlSource = sqlSourceBuilder.parse(sql, Author.class, new HashMap<>()); 25 | BoundSql boundSql = sqlSource.getBoundSql(new Author()); 26 | 27 | System.out.println(String.format("SQL: %s\n", boundSql.getSql())); 28 | System.out.println(String.format("ParameterMappings: %s", boundSql.getParameterMappings())); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter6/TransactionalCacheTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter6; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.Future; 9 | import java.util.concurrent.TimeUnit; 10 | import org.apache.ibatis.io.Resources; 11 | import org.apache.ibatis.session.SqlSession; 12 | import org.apache.ibatis.session.SqlSessionFactory; 13 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import xyz.coolblog.chapter6.dao.StudentDao; 17 | import xyz.coolblog.chapter6.model.Student; 18 | 19 | /** 20 | * AutoMappingTest 21 | * 22 | * @author Tian ZhongBo 23 | * @date 2018-07-25 11:46:47 24 | */ 25 | public class TransactionalCacheTest { 26 | 27 | private SqlSessionFactory sqlSessionFactory; 28 | 29 | private CountDownLatch countDownLatch = new CountDownLatch(1); 30 | 31 | @Before 32 | public void prepare() throws IOException { 33 | String resource = "chapter6/mybatis-config.xml"; 34 | InputStream inputStream = Resources.getResourceAsStream(resource); 35 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 36 | inputStream.close(); 37 | } 38 | 39 | @Test 40 | public void testTransactional() throws Exception { 41 | ExecutorService es = Executors.newFixedThreadPool(2); 42 | Future fa = es.submit(this::transactionalA); 43 | Future fb = es.submit(this::transactionalB); 44 | 45 | countDownLatch.countDown(); 46 | es.awaitTermination(6, TimeUnit.SECONDS); 47 | 48 | System.out.println(fa.get()); 49 | System.out.println("\n -------- 分割线 ------- \n"); 50 | System.out.println(fb.get()); 51 | 52 | System.out.println(); 53 | System.out.println(); 54 | System.out.println(); 55 | } 56 | 57 | private String transactionalA() throws Exception { 58 | SqlSession sqlSession = sqlSessionFactory.openSession(); 59 | StudentDao studentDao = sqlSession.getMapper(StudentDao.class); 60 | countDownLatch.await(); 61 | 62 | StringBuilder sb = new StringBuilder(); 63 | sb.append("时刻1:开启事务 A\n"); 64 | sb.append("时刻2:查询记录 A\n"); 65 | 66 | Student s1 = studentDao.findOne(1); 67 | sb.append(s1).append("\n"); 68 | 69 | sb.append("时刻3:更新记录 A\n"); 70 | studentDao.update(1, "tianxiaobo"); 71 | sb.append("时刻4:查询记录 A'\n"); 72 | Student s2 = studentDao.findOne(1); 73 | sb.append(s2).append("\n"); 74 | 75 | Thread.sleep(1000); 76 | 77 | sb.append("时刻5:提交事务 A"); 78 | sqlSession.commit(); 79 | 80 | return sb.toString(); 81 | } 82 | 83 | private String transactionalB() throws Exception { 84 | SqlSession sqlSession = sqlSessionFactory.openSession(); 85 | StudentDao studentDao = sqlSession.getMapper(StudentDao.class); 86 | countDownLatch.await(); 87 | 88 | StringBuilder sb = new StringBuilder(); 89 | sb.append("时刻1:开启事务 B\n"); 90 | sb.append("时刻2:查询数据 A\n"); 91 | Student s1 = studentDao.findOne(1); 92 | sb.append(s1).append("\n"); 93 | 94 | sb.append("时刻3:---------\n"); 95 | sb.append("时刻4:查询数据 A\n"); 96 | Student s2 = studentDao.findOne(1); 97 | sb.append(s2).append("\n"); 98 | 99 | Thread.sleep(3000); 100 | 101 | sb.append("时刻5:---------\n"); 102 | sb.append("时刻6:查询数据 A'\n"); 103 | Student s3 = studentDao.findOne(1); 104 | sb.append(s3).append("\n"); 105 | 106 | sb.append("时刻7:提交事务 B"); 107 | sqlSession.commit(); 108 | return sb.toString(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/test/java/xyz/coolblog/chapter7/PluginTest.java: -------------------------------------------------------------------------------- 1 | package xyz.coolblog.chapter7; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.session.RowBounds; 7 | import org.apache.ibatis.session.SqlSession; 8 | import org.apache.ibatis.session.SqlSessionFactory; 9 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import xyz.coolblog.chapter7.dao.StudentDao; 13 | 14 | /** 15 | * MyBatisTest 16 | * 17 | * @author Tian ZhongBo 18 | * @date 2018-07-01 13:24:55 19 | */ 20 | public class PluginTest { 21 | 22 | private SqlSessionFactory sqlSessionFactory; 23 | 24 | @Before 25 | public void prepare() throws IOException { 26 | String resource = "chapter7/mybatis-config.xml"; 27 | InputStream inputStream = Resources.getResourceAsStream(resource); 28 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 29 | inputStream.close(); 30 | } 31 | 32 | @Test 33 | public void testPlugin() { 34 | SqlSession session = sqlSessionFactory.openSession(); 35 | try { 36 | StudentDao studentDao = session.getMapper(StudentDao.class); 37 | studentDao.findByPaging(1, new RowBounds(20, 10)); 38 | } finally { 39 | session.close(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/application-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/application.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.mysql.cj.jdbc.Driver 8 | jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false 9 | root 10 | 1234 11 | org.hibernate.dialect.MySQL5Dialect 12 | true 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mapper/ArticleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 15 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mapper2/ArticleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 34 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mapper2/AuthorMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 38 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mapping/Article.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mybatis-config.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/test/resources/chapter1/mybatis-config2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/test/resources/chapter1/mybatis-config3.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | -------------------------------------------------------------------------------- /src/test/resources/chapter3/mapper/ArticleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/test/resources/chapter4/mapper/ArticleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 23 | 24 | 32 | -------------------------------------------------------------------------------- /src/test/resources/chapter4/mapper/AuthorMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | INSERT INTO 9 | author (`name`, `age`, `sex`, `email`) 10 | VALUES 11 | 12 | (#{author.name}, #{author.age}, #{author.sex}, #{author.email}) 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/test/resources/chapter4/mybatis-config.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 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/test/resources/chapter4/mybatis-config2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/resources/chapter6/mapper/StudentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 19 | 20 | 21 | UPDATE 22 | student 23 | SET 24 | `name` = #{name} 25 | WHERE 26 | id = #{id} 27 | 28 | -------------------------------------------------------------------------------- /src/test/resources/chapter6/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/resources/chapter7/mapper/StudentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 15 | -------------------------------------------------------------------------------- /src/test/resources/chapter7/mybatis-config.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/test/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driver=com.mysql.cj.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false 3 | jdbc.username=root 4 | jdbc.password=1234 -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=ERROR, stdout 2 | 3 | log4j.logger.xyz.coolblog.chapter1.dao=TRACE 4 | log4j.logger.xyz.coolblog.chapter1.dao2=TRACE 5 | log4j.logger.xyz.coolblog.chapter4.dao=TRACE 6 | log4j.logger.xyz.coolblog.chapter7.dao=TRACE 7 | 8 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 9 | log4j.appender.stdout.Target = System.out 10 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n --------------------------------------------------------------------------------