├── .gitignore ├── LICENSE ├── README.md ├── mybatis-action.iml ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── zwwhnly │ │ ├── generator │ │ └── Generator.java │ │ └── mybatisaction │ │ ├── mapper │ │ ├── CountryMapper.java │ │ ├── SysPrivilegeMapper.java │ │ ├── SysPrivilegeProvider.java │ │ ├── SysRoleMapper.java │ │ ├── SysRolePrivilegeMapper.java │ │ ├── SysUserMapper.java │ │ └── SysUserRoleMapper.java │ │ ├── model │ │ ├── Country.java │ │ ├── CreateInfo.java │ │ ├── SysPrivilege.java │ │ ├── SysRole.java │ │ ├── SysRoleExtend.java │ │ ├── SysRolePrivilege.java │ │ ├── SysUser.java │ │ ├── SysUserExtend.java │ │ └── SysUserRole.java │ │ └── type │ │ ├── Enabled.java │ │ └── EnabledTypeHandler.java └── resources │ ├── com │ └── zwwhnly │ │ └── mybatisaction │ │ └── mapper │ │ ├── CountryMapper.xml │ │ ├── SysPrivilegeMapper.xml │ │ ├── SysRoleMapper.xml │ │ ├── SysRolePrivilegeMapper.xml │ │ ├── SysUserMapper.xml │ │ └── SysUserRoleMapper.xml │ ├── ehcache.xml │ ├── generator │ └── generatorConfig.xml │ ├── log4j.properties │ └── mybatis-config.xml └── test └── java └── com └── zwwhnly └── mybatisaction └── mapper ├── BaseMapperTest.java ├── CacheTest.java ├── CountryMapperTest.java ├── SysPrivilegeMapperTest.java ├── SysRoleMapperTest.java └── SysUserMapperTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | .idea/ -------------------------------------------------------------------------------- /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实战 2 | 3 | 该开源项目包含大量MyBatis实战的具体示例代码,旨在帮助大家一步一步的学习MyBatis这门技术。 4 | 5 | 目前的示例代码主要来自于刘增辉老师所著的《MyBatis从入门到精通》一书,也就是下面这本。 6 | ![](https://images.zwwhnly.com/picture/424537-20190711100832062-1032140749.jpg) 7 | 8 | ### 学习过程 9 | 10 | - 2019-06-13 购买 11 | - 2019-07-01 开始阅读 12 | - 2019-07-10 阅读完毕 13 | 14 | ### 输出 15 | 16 | 以下为自己学习过程中总结的博客,博客中的源码都能在该项目中找到,建议按顺序阅读。 17 | 18 | 1. [MyBatis系列(一):MyBatis入门](https://juejin.im/post/5d15e464518825268b110fd8) 19 | 2. [MyBatis系列(二):MyBatis XML方式的基本用法之Select](https://juejin.im/post/5d1ab668f265da1b9253f3b0) 20 | 3. [MyBatis系列(三):MyBatis XML方式的基本用法之多表查询](https://juejin.im/post/5d1c09dd6fb9a07ee9588a9b) 21 | 4. [MyBatis系列(四):MyBatis XML方式的基本用法之增删改](https://juejin.im/post/5d1d5ebef265da1b971a9169) 22 | 5. [MyBatis系列(五):MyBatis 注解方式的基本用法](https://juejin.im/post/5d1eaaa5f265da1bb7768849) 23 | 6. [MyBatis系列(六):MyBatis动态Sql之if标签的用法](https://juejin.im/post/5d22bd68f265da1b971a9765) 24 | 7. [MyBatis系列(七):MyBatis动态Sql之choose,where,set标签的用法](https://juejin.im/post/5d2430baf265da1bbc6ffb8f) 25 | 8. [MyBatis系列(八):MyBatis动态Sql之foreach标签的用法](https://juejin.im/post/5d25834d5188251b1b1fe7e6) 26 | 9. [MyBatis系列(九):MyBatis高级结果映射之一对一映射](https://juejin.im/post/5d26d7e9518825451f65eccb) 27 | 10. [MyBatis系列(十):使用association标签实现嵌套查询](https://juejin.im/post/5d2816e6f265da1bb67a4539) 28 | 11. [MyBatis系列(十一):MyBatis高级结果映射之一对多映射](https://juejin.im/post/5d2d4b11f265da1b725c3559) 29 | 12. [MyBatis系列(十二):使用collection标签实现嵌套查询](https://juejin.im/post/5d2eca636fb9a07ea33c4acc) 30 | 13. [MyBatis系列(十三):使用discriminator鉴别器映射](https://juejin.im/post/5d313df6f265da1b827ad507) 31 | 14. [MyBatis系列(十四):在MyBatis中使用类型处理器](https://juejin.im/post/5d380469e51d45598611ba32) 32 | -------------------------------------------------------------------------------- /mybatis-action.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.zwwhnly 8 | mybatis-action 9 | 1.0-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 14 | 15 | 16 | 17 | org.mybatis 18 | mybatis 19 | 3.3.1 20 | 21 | 22 | mysql 23 | mysql-connector-java 24 | 5.1.38 25 | 26 | 27 | junit 28 | junit 29 | 4.12 30 | 31 | 32 | org.slf4j 33 | slf4j-api 34 | 1.7.12 35 | 36 | 37 | org.slf4j 38 | slf4j-log4j12 39 | 1.7.12 40 | 41 | 42 | log4j 43 | log4j 44 | 1.2.17 45 | 46 | 47 | org.mybatis.generator 48 | mybatis-generator-core 49 | 1.3.3 50 | 51 | 52 | org.mybatis.caches 53 | mybatis-ehcache 54 | 1.0.3 55 | 56 | 57 | org.mybatis.caches 58 | mybatis-redis 59 | 1.0.0-beta2 60 | 61 | 62 | 63 | 64 | 65 | 66 | maven-compiler-plugin 67 | 68 | 1.6 69 | 1.6 70 | 71 | 72 | 73 | org.mybatis.generator 74 | mybatis-generator-maven-plugin 75 | 1.3.5 76 | 77 | 78 | ${basedir}/src/main/resources/generator/generatorConfig.xml 79 | 80 | true 81 | true 82 | 83 | 84 | 85 | mysql 86 | mysql-connector-java 87 | 5.1.38 88 | 89 | 90 | com.zwwhnly 91 | mybatis-action 92 | 1.0-SNAPSHOT 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/generator/Generator.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.generator; 2 | 3 | import org.mybatis.generator.api.MyBatisGenerator; 4 | import org.mybatis.generator.config.Configuration; 5 | import org.mybatis.generator.config.xml.ConfigurationParser; 6 | import org.mybatis.generator.exception.InvalidConfigurationException; 7 | import org.mybatis.generator.exception.XMLParserException; 8 | import org.mybatis.generator.internal.DefaultShellCallback; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.sql.SQLException; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * 读取MBG配置生成代码 18 | */ 19 | public class Generator { 20 | public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { 21 | List warnings = new ArrayList(); 22 | boolean overwrite = true; 23 | InputStream is = Generator.class.getResourceAsStream("/generator/generatorConfig.xml"); 24 | ConfigurationParser cp = new ConfigurationParser(warnings); 25 | Configuration configuration = cp.parseConfiguration(is); 26 | is.close(); 27 | 28 | DefaultShellCallback callback = new DefaultShellCallback(overwrite); 29 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings); 30 | myBatisGenerator.generate(null); 31 | 32 | for (String warning : warnings) { 33 | System.out.println(warning); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/CountryMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.Country; 4 | 5 | import java.util.List; 6 | 7 | public interface CountryMapper { 8 | /** 9 | * 查询全部用户 10 | * 11 | * @return 12 | */ 13 | List selectAll(); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysPrivilegeMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysPrivilege; 4 | import org.apache.ibatis.annotations.SelectProvider; 5 | 6 | import java.util.List; 7 | 8 | public interface SysPrivilegeMapper { 9 | @SelectProvider(type = SysPrivilegeProvider.class, method = "selectById") 10 | SysPrivilege selectById(Long id); 11 | 12 | /** 13 | * 获取角色包含的所有权限信息 14 | * 15 | * @param roleId 16 | * @return 17 | */ 18 | List selectPrivilegeByRoleId(Long roleId); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysPrivilegeProvider.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import org.apache.ibatis.jdbc.SQL; 4 | 5 | public class SysPrivilegeProvider { 6 | public String selectById(final Long id) { 7 | return new SQL() { 8 | { 9 | SELECT("id,privilege_name,privilege_url"); 10 | FROM("sys_privilege"); 11 | WHERE("id = #{id}"); 12 | } 13 | }.toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysRoleMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysRole; 4 | import com.zwwhnly.mybatisaction.model.SysRoleExtend; 5 | import org.apache.ibatis.annotations.*; 6 | 7 | import java.util.List; 8 | 9 | public interface SysRoleMapper { 10 | @Select("SELECT id,role_name,enabled,create_by,create_time FROM sys_role WHERE id = #{id}") 11 | SysRole selectById(Long id); 12 | 13 | @Results(id = "roleResultMap", value = { 14 | @Result(property = "id", column = "id", id = true), 15 | @Result(property = "roleName", column = "role_name"), 16 | @Result(property = "enabled", column = "enabled"), 17 | @Result(property = "createBy", column = "create_by"), 18 | @Result(property = "createTime", column = "create_time") 19 | }) 20 | @Select("SELECT id,role_name,enabled,create_by,create_time FROM sys_role WHERE id = #{id}") 21 | SysRole selectByIdUseResults(Long id); 22 | 23 | @ResultMap("roleResultMap") 24 | @Select("SELECT * FROM sys_role") 25 | List selectAll(); 26 | 27 | @Insert({"INSERT INTO sys_role(id, role_name, enabled, create_by, create_time) ", 28 | "VALUES (#{id},#{roleName},#{enabled},#{createBy},#{createTime,jdbcType=TIMESTAMP})"}) 29 | int insert(SysRole sysRole); 30 | 31 | @Insert({"INSERT INTO sys_role(role_name, enabled, create_by, create_time) ", 32 | "VALUES (#{roleName},#{enabled},#{createBy},#{createTime,jdbcType=TIMESTAMP})"}) 33 | @Options(useGeneratedKeys = true, keyProperty = "id") 34 | int insertUseGeneratedKeys(SysRole sysRole); 35 | 36 | @Insert({"INSERT INTO sys_role(role_name, enabled, create_by, create_time) ", 37 | "VALUES (#{roleName},#{enabled},#{createBy},#{createTime,jdbcType=TIMESTAMP})"}) 38 | @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyColumn = "id", keyProperty = "id", resultType = Long.class, before = false) 39 | int insertUseSelectKey(SysRole sysRole); 40 | 41 | @Update({"UPDATE sys_role ", "SET role_name = #{roleName},enabled = #{enabled},create_by=#{createBy}, ", 42 | "create_time=#{createTime,jdbcType=TIMESTAMP} ", " WHERE id=#{id}"}) 43 | int updateById(SysRole sysRole); 44 | 45 | @Delete("DELETE FROM sys_role WHERE id = #{id}") 46 | int deleteById(Long id); 47 | 48 | List selectAllRoleAndPrivileges(); 49 | 50 | List selectRoleByUserId(Long userId); 51 | 52 | /** 53 | * 根据用户id获取用户的角色信息 54 | * 55 | * @param userId 56 | * @return 57 | */ 58 | List selectRoleByUserIdChoose(Long userId); 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysRolePrivilegeMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | public interface SysRolePrivilegeMapper { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysUserMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysRole; 4 | import com.zwwhnly.mybatisaction.model.SysUser; 5 | import com.zwwhnly.mybatisaction.model.SysUserExtend; 6 | import org.apache.ibatis.annotations.Param; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | public interface SysUserMapper { 12 | /** 13 | * 通过id查询用户 14 | * 15 | * @param id 16 | * @return 17 | */ 18 | SysUser selectById(Long id); 19 | 20 | /** 21 | * 查询全部用户 22 | * 23 | * @return 24 | */ 25 | List selectAll(); 26 | 27 | /** 28 | * 根据用户id获取角色信息 29 | * 30 | * @param userId 31 | * @return 32 | */ 33 | List selectRolesByUserId(Long userId); 34 | 35 | /** 36 | * 新增用户 37 | * 38 | * @param sysUser 39 | * @return 40 | */ 41 | int insert(SysUser sysUser); 42 | 43 | /** 44 | * 新增用户-使用useGeneratedKeys方式 45 | * 46 | * @param sysUser 47 | * @return 48 | */ 49 | int insertUseGeneratedKeys(SysUser sysUser); 50 | 51 | /** 52 | * 新增用户-使用selectKey方式 53 | * 54 | * @param sysUser 55 | * @return 56 | */ 57 | int insertUseSelectKey(SysUser sysUser); 58 | 59 | /** 60 | * 根据主键更新 61 | * 62 | * @param sysUser 63 | * @return 64 | */ 65 | int updateById(SysUser sysUser); 66 | 67 | /** 68 | * 根据主键删除 69 | * 70 | * @param id 71 | * @return 72 | */ 73 | int deleteById(Long id); 74 | 75 | /** 76 | * 根据对象的主键删除 77 | * 78 | * @param sysUser 79 | * @return 80 | */ 81 | int deleteBySysUser(SysUser sysUser); 82 | 83 | /** 84 | * 根据用户id和角色的enabled状态获取用户的角色 85 | * 86 | * @param userId 87 | * @param enabled 88 | * @return 89 | */ 90 | List selectRolesByUserIdAndRoleEnabled(@Param("userId") Long userId, @Param("enabled") Integer enabled); 91 | 92 | /** 93 | * 根据用户id和角色的enabled状态获取用户的角色 94 | * 95 | * @param user 96 | * @param role 97 | * @return 98 | */ 99 | List selectRolesByUserAndRole(@Param("user") SysUser user, @Param("role") SysRole role); 100 | 101 | /** 102 | * 根据动态条件查询用户信息 103 | * 104 | * @param sysUser 105 | * @return 106 | */ 107 | List selectByUser(SysUser sysUser); 108 | 109 | /** 110 | * 根据动态条件查询用户信息(使用Where标签) 111 | * 112 | * @param sysUser 113 | * @return 114 | */ 115 | List selectByUserWhere(SysUser sysUser); 116 | 117 | /** 118 | * 根据主键选择性更新用户信息 119 | * 120 | * @param sysUser 121 | * @return 122 | */ 123 | int updateByIdSelective(SysUser sysUser); 124 | 125 | /** 126 | * 根据主键选择性更新用户信息(使用Set标签) 127 | * 128 | * @param sysUser 129 | * @return 130 | */ 131 | int updateByIdSelectiveSet(SysUser sysUser); 132 | 133 | /** 134 | * 根据传入的参数值动态插入列 135 | * 136 | * @param sysUser 137 | * @return 138 | */ 139 | int insertSelective(SysUser sysUser); 140 | 141 | /** 142 | * 根据用户id或用户名查询 143 | * 144 | * @param sysUser 145 | * @return 146 | */ 147 | SysUser selectByIdOrUserName(SysUser sysUser); 148 | 149 | /** 150 | * 根据用户id集合查询用户 151 | * 152 | * @param idList 153 | * @return 154 | */ 155 | List selectByIdList(@Param("idList") List idList); 156 | 157 | /** 158 | * 根据用户id数组查询用户 159 | * 160 | * @param idArray 161 | * @return 162 | */ 163 | List selectByIdArray(@Param("idArray") Long[] idArray); 164 | 165 | /** 166 | * 批量插入用户信息 167 | * 168 | * @param userList 169 | * @return 170 | */ 171 | int insertList(List userList); 172 | 173 | /** 174 | * 通过Map更新列 175 | * 176 | * @param map 177 | * @return 178 | */ 179 | int updateByMap(@Param("userMap") Map map); 180 | 181 | /** 182 | * 根据用户id获取用户信息和用户的角色信息 183 | * 184 | * @param id 185 | * @return 186 | */ 187 | SysUserExtend selectUserAndRoleById(Long id); 188 | 189 | /** 190 | * 根据用户id获取用户信息和用户的角色信息 191 | * 192 | * @param id 193 | * @return 194 | */ 195 | SysUserExtend selectUserAndRoleByIdResultMap(Long id); 196 | 197 | /** 198 | * 根据用户id获取用户信息和用户的角色信息,嵌套查询方式 199 | * 200 | * @param id 201 | * @return 202 | */ 203 | SysUserExtend selectUserAndRoleByIdSelect(Long id); 204 | 205 | /** 206 | * 获取所有的用户以及对应的所有角色 207 | * 208 | * @return 209 | */ 210 | List selectAllUserAndRoles(); 211 | 212 | /** 213 | * 通过嵌套查询获取指定用户的信息以及用户的角色和权限信息 214 | * 215 | * @param id 216 | * @return 217 | */ 218 | SysUserExtend selectAllUserAndRolesSelect(Long id); 219 | 220 | /** 221 | * 使用存储过程查询用户信息 222 | * 223 | * @param sysUser 224 | */ 225 | void selectUserById(SysUser sysUser); 226 | 227 | /** 228 | * 使用存储过程分页查询 229 | * 230 | * @param params 231 | * @return 232 | */ 233 | List selectUserPage(Map params); 234 | 235 | /** 236 | * 保存用户信息和角色关联信息 237 | * 238 | * @param sysUser 239 | * @param roleIds 240 | * @return 241 | */ 242 | int insertUserAndRoles(@Param("sysUser") SysUser sysUser, @Param("roleIds") String roleIds); 243 | 244 | /** 245 | * 根据用户id删除用户和用户的角色信息 246 | * 247 | * @param id 248 | * @return 249 | */ 250 | int deleteUserById(Long id); 251 | } 252 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/mapper/SysUserRoleMapper.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | public interface SysUserRoleMapper { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/Country.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | public class Country { 4 | private Integer id; 5 | 6 | private String countryname; 7 | 8 | private String countrycode; 9 | 10 | public Integer getId() { 11 | return id; 12 | } 13 | 14 | public void setId(Integer id) { 15 | this.id = id; 16 | } 17 | 18 | public String getCountryname() { 19 | return countryname; 20 | } 21 | 22 | public void setCountryname(String countryname) { 23 | this.countryname = countryname; 24 | } 25 | 26 | public String getCountrycode() { 27 | return countrycode; 28 | } 29 | 30 | public void setCountrycode(String countrycode) { 31 | this.countrycode = countrycode; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/CreateInfo.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 创建信息 7 | */ 8 | public class CreateInfo { 9 | /** 10 | * 创建人 11 | */ 12 | private String createBy; 13 | 14 | /** 15 | * 创建时间 16 | */ 17 | private Date createTime; 18 | 19 | public String getCreateBy() { 20 | return createBy; 21 | } 22 | 23 | public void setCreateBy(String createBy) { 24 | this.createBy = createBy; 25 | } 26 | 27 | public Date getCreateTime() { 28 | return createTime; 29 | } 30 | 31 | public void setCreateTime(Date createTime) { 32 | this.createTime = createTime; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysPrivilege.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | /** 4 | * 权限表 5 | */ 6 | public class SysPrivilege { 7 | /** 8 | * 权限ID 9 | */ 10 | private Long id; 11 | 12 | /** 13 | * 权限名称 14 | */ 15 | private String privilegeName; 16 | 17 | /** 18 | * 权限URL 19 | */ 20 | private String privilegeUrl; 21 | 22 | public Long getId() { 23 | return id; 24 | } 25 | 26 | public void setId(Long id) { 27 | this.id = id; 28 | } 29 | 30 | public String getPrivilegeName() { 31 | return privilegeName; 32 | } 33 | 34 | public void setPrivilegeName(String privilegeName) { 35 | this.privilegeName = privilegeName; 36 | } 37 | 38 | public String getPrivilegeUrl() { 39 | return privilegeUrl; 40 | } 41 | 42 | public void setPrivilegeUrl(String privilegeUrl) { 43 | this.privilegeUrl = privilegeUrl; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysRole.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | import com.zwwhnly.mybatisaction.type.Enabled; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | /** 10 | * 角色表 11 | */ 12 | public class SysRole implements Serializable { 13 | private static final long serialVersionUID = 6320941908222932112L; 14 | 15 | /** 16 | * 角色ID 17 | */ 18 | private Long id; 19 | 20 | /** 21 | * 角色名 22 | */ 23 | private String roleName; 24 | 25 | /** 26 | * 有效标志 27 | */ 28 | //private Integer enabled; 29 | 30 | /** 31 | * 有效标志 32 | */ 33 | private Enabled enabled; 34 | 35 | /** 36 | * 创建人 37 | */ 38 | private Long createBy; 39 | 40 | /** 41 | * 创建时间 42 | */ 43 | private Date createTime; 44 | 45 | /** 46 | * 创建信息 47 | */ 48 | private CreateInfo createInfo; 49 | 50 | public Long getId() { 51 | return id; 52 | } 53 | 54 | public void setId(Long id) { 55 | this.id = id; 56 | } 57 | 58 | public String getRoleName() { 59 | return roleName; 60 | } 61 | 62 | public void setRoleName(String roleName) { 63 | this.roleName = roleName; 64 | } 65 | 66 | /*public Integer getEnabled() { 67 | return enabled; 68 | } 69 | 70 | public void setEnabled(Integer enabled) { 71 | this.enabled = enabled; 72 | }*/ 73 | 74 | public Enabled getEnabled() { 75 | return enabled; 76 | } 77 | 78 | public void setEnabled(Enabled enabled) { 79 | this.enabled = enabled; 80 | } 81 | 82 | public Long getCreateBy() { 83 | return createBy; 84 | } 85 | 86 | public void setCreateBy(Long createBy) { 87 | this.createBy = createBy; 88 | } 89 | 90 | public Date getCreateTime() { 91 | return createTime; 92 | } 93 | 94 | public void setCreateTime(Date createTime) { 95 | this.createTime = createTime; 96 | } 97 | 98 | public CreateInfo getCreateInfo() { 99 | return createInfo; 100 | } 101 | 102 | public void setCreateInfo(CreateInfo createInfo) { 103 | this.createInfo = createInfo; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysRoleExtend.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | import java.util.List; 4 | 5 | public class SysRoleExtend extends SysRole { 6 | private SysUser sysUser; 7 | /** 8 | * 角色包含的权限列表 9 | */ 10 | private List sysPrivilegeList; 11 | 12 | public SysUser getSysUser() { 13 | return sysUser; 14 | } 15 | 16 | public void setSysUser(SysUser sysUser) { 17 | this.sysUser = sysUser; 18 | } 19 | 20 | public List getSysPrivilegeList() { 21 | return sysPrivilegeList; 22 | } 23 | 24 | public void setSysPrivilegeList(List sysPrivilegeList) { 25 | this.sysPrivilegeList = sysPrivilegeList; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysRolePrivilege.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | /** 4 | * 角色权限关联表 5 | */ 6 | public class SysRolePrivilege { 7 | /** 8 | * 角色ID 9 | */ 10 | private Long roleId; 11 | 12 | /** 13 | * 权限ID 14 | */ 15 | private Long privilegeId; 16 | 17 | public Long getRoleId() { 18 | return roleId; 19 | } 20 | 21 | public void setRoleId(Long roleId) { 22 | this.roleId = roleId; 23 | } 24 | 25 | public Long getPrivilegeId() { 26 | return privilegeId; 27 | } 28 | 29 | public void setPrivilegeId(Long privilegeId) { 30 | this.privilegeId = privilegeId; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysUser.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 用户表 7 | */ 8 | public class SysUser { 9 | /** 10 | * 用户ID 11 | */ 12 | private Long id; 13 | 14 | /** 15 | * 用户名 16 | */ 17 | private String userName; 18 | 19 | /** 20 | * 密码 21 | */ 22 | private String userPassword; 23 | 24 | /** 25 | * 邮箱 26 | */ 27 | private String userEmail; 28 | 29 | /** 30 | * 简介 31 | */ 32 | private String userInfo; 33 | 34 | /** 35 | * 头像 36 | */ 37 | private byte[] headImg; 38 | 39 | /** 40 | * 创建时间 41 | */ 42 | private Date createTime; 43 | 44 | public Long getId() { 45 | return id; 46 | } 47 | 48 | public void setId(Long id) { 49 | this.id = id; 50 | } 51 | 52 | public String getUserName() { 53 | return userName; 54 | } 55 | 56 | public void setUserName(String userName) { 57 | this.userName = userName; 58 | } 59 | 60 | public String getUserPassword() { 61 | return userPassword; 62 | } 63 | 64 | public void setUserPassword(String userPassword) { 65 | this.userPassword = userPassword; 66 | } 67 | 68 | public String getUserEmail() { 69 | return userEmail; 70 | } 71 | 72 | public void setUserEmail(String userEmail) { 73 | this.userEmail = userEmail; 74 | } 75 | 76 | public String getUserInfo() { 77 | return userInfo; 78 | } 79 | 80 | public void setUserInfo(String userInfo) { 81 | this.userInfo = userInfo; 82 | } 83 | 84 | public byte[] getHeadImg() { 85 | return headImg; 86 | } 87 | 88 | public void setHeadImg(byte[] headImg) { 89 | this.headImg = headImg; 90 | } 91 | 92 | public Date getCreateTime() { 93 | return createTime; 94 | } 95 | 96 | public void setCreateTime(Date createTime) { 97 | this.createTime = createTime; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysUserExtend.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | import java.util.List; 4 | 5 | public class SysUserExtend extends SysUser { 6 | /** 7 | * 用户角色 8 | */ 9 | private SysRole sysRole; 10 | 11 | /** 12 | * 用户的角色集合 13 | */ 14 | private List sysRoleList; 15 | 16 | public SysRole getSysRole() { 17 | return sysRole; 18 | } 19 | 20 | public void setSysRole(SysRole sysRole) { 21 | this.sysRole = sysRole; 22 | } 23 | 24 | public List getSysRoleList() { 25 | return sysRoleList; 26 | } 27 | 28 | public void setSysRoleList(List sysRoleList) { 29 | this.sysRoleList = sysRoleList; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/model/SysUserRole.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.model; 2 | 3 | /** 4 | * 用户角色关联表 5 | */ 6 | public class SysUserRole { 7 | 8 | /** 9 | * 用户ID 10 | */ 11 | private Long userId; 12 | 13 | /** 14 | * 角色ID 15 | */ 16 | private Long roleId; 17 | 18 | public Long getUserId() { 19 | return userId; 20 | } 21 | 22 | public void setUserId(Long userId) { 23 | this.userId = userId; 24 | } 25 | 26 | public Long getRoleId() { 27 | return roleId; 28 | } 29 | 30 | public void setRoleId(Long roleId) { 31 | this.roleId = roleId; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/type/Enabled.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.type; 2 | 3 | public enum Enabled { 4 | 5 | /** 6 | * 启用 7 | */ 8 | enabled(1), 9 | 10 | /** 11 | * 禁用 12 | */ 13 | disabled(0); 14 | 15 | private final int value; 16 | 17 | private Enabled(int value) { 18 | this.value = value; 19 | } 20 | 21 | public int getValue() { 22 | return value; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/zwwhnly/mybatisaction/type/EnabledTypeHandler.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.type; 2 | 3 | import org.apache.ibatis.type.JdbcType; 4 | import org.apache.ibatis.type.TypeHandler; 5 | 6 | import java.sql.CallableStatement; 7 | import java.sql.PreparedStatement; 8 | import java.sql.ResultSet; 9 | import java.sql.SQLException; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * Enabled类型处理器 15 | */ 16 | public class EnabledTypeHandler implements TypeHandler { 17 | private final Map enabledMap = new HashMap(); 18 | 19 | public EnabledTypeHandler() { 20 | for (Enabled enabled : Enabled.values()) { 21 | enabledMap.put(enabled.getValue(), enabled); 22 | } 23 | } 24 | 25 | @Override 26 | public void setParameter(PreparedStatement preparedStatement, int i, Enabled enabled, JdbcType jdbcType) throws SQLException { 27 | preparedStatement.setInt(i, enabled.getValue()); 28 | } 29 | 30 | @Override 31 | public Enabled getResult(ResultSet resultSet, String s) throws SQLException { 32 | Integer value = resultSet.getInt(s); 33 | return enabledMap.get(value); 34 | } 35 | 36 | @Override 37 | public Enabled getResult(ResultSet resultSet, int i) throws SQLException { 38 | Integer value = resultSet.getInt(i); 39 | return enabledMap.get(value); 40 | } 41 | 42 | @Override 43 | public Enabled getResult(CallableStatement callableStatement, int i) throws SQLException { 44 | Integer value = callableStatement.getInt(i); 45 | return enabledMap.get(value); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/CountryMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/SysPrivilegeMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 15 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/SysRoleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 30 | 32 | 33 | 35 | 38 | 39 | 41 | 42 | 43 | 44 | 45 | 46 | 49 | 63 | 74 | 85 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/SysRolePrivilegeMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/SysUserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 22 | 27 | 29 | 36 | 37 | 38 | 42 | 43 | 44 | 47 | 49 | 50 | 51 | 53 | 56 | 57 | 58 | 61 | 71 | 84 | 85 | INSERT INTO sys_user(id, user_name, user_password, user_email, user_info, head_img, create_time) 86 | VALUES (#{id},#{userName},#{userPassword},#{userEmail},#{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP}) 87 | 88 | 89 | INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time) 90 | VALUES (#{userName},#{userPassword},#{userEmail},#{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP}) 91 | 92 | 93 | INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time) 94 | VALUES 95 | (#{userName},#{userPassword},#{userEmail},#{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP}) 96 | 97 | SELECT LAST_INSERT_ID() 98 | 99 | 100 | 101 | UPDATE sys_user 102 | SET user_name = #{userName}, 103 | user_password = #{userPassword}, 104 | user_email = #{userEmail}, 105 | user_info = #{userInfo}, 106 | head_img = #{headImg,jdbcType=BLOB}, 107 | create_time = #{createTime,jdbcType=TIMESTAMP} 108 | WHERE id = #{id} 109 | 110 | 111 | DELETE FROM sys_user WHERE id = #{id} 112 | 113 | 114 | DELETE FROM sys_user WHERE id = #{id} 115 | 116 | 127 | 138 | 153 | 169 | 170 | UPDATE sys_user 171 | SET 172 | 173 | user_name = #{userName}, 174 | 175 | 176 | user_password = #{userPassword}, 177 | 178 | 179 | user_email = #{userEmail}, 180 | 181 | 182 | user_info = #{userInfo}, 183 | 184 | 185 | head_img = #{headImg,jdbcType=BLOB}, 186 | 187 | 188 | create_time = #{createTime,jdbcType=TIMESTAMP}, 189 | 190 | id = #{id} 191 | WHERE id = #{id} 192 | 193 | 194 | UPDATE sys_user 195 | 196 | 197 | user_name = #{userName}, 198 | 199 | 200 | user_password = #{userPassword}, 201 | 202 | 203 | user_email = #{userEmail}, 204 | 205 | 206 | user_info = #{userInfo}, 207 | 208 | 209 | head_img = #{headImg,jdbcType=BLOB}, 210 | 211 | 212 | create_time = #{createTime,jdbcType=TIMESTAMP}, 213 | 214 | id = #{id}, 215 | 216 | WHERE id = #{id} 217 | 218 | 219 | INSERT INTO sys_user(user_name, user_password, 220 | 221 | user_email, 222 | 223 | user_info, head_img, create_time) 224 | VALUES (#{userName},#{userPassword}, 225 | 226 | #{userEmail}, 227 | 228 | #{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP}) 229 | 230 | 250 | 263 | 276 | 277 | INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time) 278 | VALUES 279 | 280 | (#{user.userName},#{user.userPassword},#{user.userEmail},#{user.userInfo},#{user.headImg,jdbcType=BLOB},#{user.createTime,jdbcType=TIMESTAMP}) 281 | 282 | 283 | 284 | UPDATE sys_user 285 | SET 286 | 287 | ${key} = #{val} 288 | 289 | WHERE id = #{userMap.id} 290 | 291 | 307 | 323 | 334 | 350 | 370 | 380 | 391 | 400 | 401 | {CALL insert_user_and_roles( 402 | #{sysUser.id,mode=OUT,jdbcType=BIGINT}, 403 | #{sysUser.userName,mode=IN}, 404 | #{sysUser.userPassword,mode=IN}, 405 | #{sysUser.userEmail,mode=IN}, 406 | #{sysUser.userInfo,mode=IN}, 407 | #{sysUser.headImg,mode=IN,jdbcType=BLOB}, 408 | #{sysUser.createTime,mode=OUT,jdbcType=TIMESTAMP}, 409 | #{roleIds,mode=IN} 410 | )} 411 | 412 | 413 | {CALL delete_user_by_id(#{id,mode=IN})} 414 | 415 | -------------------------------------------------------------------------------- /src/main/resources/com/zwwhnly/mybatisaction/mapper/SysUserRoleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/main/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 15 | 16 | 26 | -------------------------------------------------------------------------------- /src/main/resources/generator/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
33 |
34 |
-------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=ERROR, stdout 2 | log4j.logger.com.zwwhnly.mybatisaction.mapper=TRACE 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n -------------------------------------------------------------------------------- /src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/BaseMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import org.apache.ibatis.io.Resources; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 | import org.junit.BeforeClass; 8 | 9 | import java.io.IOException; 10 | import java.io.Reader; 11 | 12 | /** 13 | * 基础测试类 14 | */ 15 | public class BaseMapperTest { 16 | private static SqlSessionFactory sqlSessionFactory; 17 | 18 | @BeforeClass 19 | public static void init() { 20 | try { 21 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); 22 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 23 | reader.close(); 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | 29 | public SqlSession getSqlSession() { 30 | return sqlSessionFactory.openSession(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/CacheTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysRole; 4 | import com.zwwhnly.mybatisaction.model.SysUser; 5 | import org.apache.ibatis.jdbc.SQL; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | public class CacheTest extends BaseMapperTest { 11 | @Test 12 | public void testL1Cache() { 13 | SqlSession sqlSession = getSqlSession(); 14 | SysUser sysUser1 = null; 15 | 16 | try { 17 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 18 | sysUser1 = sysUserMapper.selectById(1L); 19 | sysUser1.setUserName("New Name"); 20 | SysUser sysUser2 = sysUserMapper.selectById(1L); 21 | 22 | Assert.assertEquals("New Name", sysUser2.getUserName()); 23 | Assert.assertEquals(sysUser1, sysUser2); 24 | } finally { 25 | sqlSession.close(); 26 | } 27 | 28 | System.out.println("开启新的sqlSession"); 29 | sqlSession = getSqlSession(); 30 | 31 | try { 32 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 33 | SysUser sysUser2 = sysUserMapper.selectById(1L); 34 | 35 | Assert.assertNotEquals("New Name", sysUser2.getUserName()); 36 | Assert.assertNotEquals(sysUser1, sysUser2); 37 | 38 | sysUserMapper.deleteById(2L); 39 | SysUser sysUser3 = sysUserMapper.selectById(1L); 40 | Assert.assertNotEquals(sysUser2, sysUser3); 41 | } finally { 42 | sqlSession.close(); 43 | } 44 | } 45 | 46 | @Test 47 | public void testL2Cache() { 48 | SqlSession sqlSession = getSqlSession(); 49 | SysRole sysRole1 = null; 50 | 51 | try { 52 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 53 | 54 | sysRole1 = sysRoleMapper.selectById(1L); 55 | sysRole1.setRoleName("New Name"); 56 | SysRole sysRole2 = sysRoleMapper.selectById(1L); 57 | 58 | Assert.assertEquals("New Name", sysRole2.getRoleName()); 59 | Assert.assertEquals(sysRole1, sysRole2); 60 | } finally { 61 | sqlSession.close(); 62 | } 63 | 64 | System.out.println("开启新的sqlSession"); 65 | sqlSession = getSqlSession(); 66 | try { 67 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 68 | 69 | SysRole sysRole2 = sysRoleMapper.selectById(1L); 70 | 71 | Assert.assertEquals("New Name", sysRole2.getRoleName()); 72 | Assert.assertNotEquals(sysRole1, sysRole2); 73 | 74 | SysRole sysRole3 = sysRoleMapper.selectById(1L); 75 | Assert.assertNotEquals(sysRole2, sysRole3); 76 | } finally { 77 | sqlSession.close(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/CountryMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.Country; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.junit.Test; 6 | 7 | import java.util.List; 8 | 9 | public class CountryMapperTest extends BaseMapperTest { 10 | 11 | @Test 12 | public void testSelectAll() { 13 | SqlSession sqlSession = getSqlSession(); 14 | 15 | try { 16 | List countryList = sqlSession.selectList("com.zwwhnly.mybatisaction.mapper.CountryMapper.selectAll"); 17 | printCountryList(countryList); 18 | } finally { 19 | sqlSession.close(); 20 | } 21 | } 22 | 23 | private void printCountryList(List countryList) { 24 | for (Country country : countryList) { 25 | System.out.printf("%-4d%4s%4s\n", country.getId(), country.getCountryname(), country.getCountrycode()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/SysPrivilegeMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysPrivilege; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.List; 9 | 10 | public class SysPrivilegeMapperTest extends BaseMapperTest { 11 | @Test 12 | public void testSelectById() { 13 | SqlSession sqlSession = getSqlSession(); 14 | 15 | try { 16 | SysPrivilegeMapper sysPrivilegeMapper = sqlSession.getMapper(SysPrivilegeMapper.class); 17 | SysPrivilege sysPrivilege = sysPrivilegeMapper.selectById(1L); 18 | 19 | Assert.assertNotNull(sysPrivilege); 20 | Assert.assertEquals("用户管理", sysPrivilege.getPrivilegeName()); 21 | } finally { 22 | sqlSession.close(); 23 | } 24 | } 25 | 26 | @Test 27 | public void testSelectPrivilegeByRoleId() { 28 | SqlSession sqlSession = getSqlSession(); 29 | 30 | try { 31 | SysPrivilegeMapper sysPrivilegeMapper = sqlSession.getMapper(SysPrivilegeMapper.class); 32 | List sysPrivilegeList = sysPrivilegeMapper.selectPrivilegeByRoleId(1L); 33 | 34 | Assert.assertTrue(sysPrivilegeList.size() > 0); 35 | } finally { 36 | sqlSession.close(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/SysRoleMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.SysPrivilege; 4 | import com.zwwhnly.mybatisaction.model.SysRole; 5 | import com.zwwhnly.mybatisaction.model.SysRoleExtend; 6 | import com.zwwhnly.mybatisaction.type.Enabled; 7 | import org.apache.ibatis.session.SqlSession; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | import java.util.List; 12 | 13 | public class SysRoleMapperTest extends BaseMapperTest { 14 | @Test 15 | public void testSelectById() { 16 | SqlSession sqlSession = getSqlSession(); 17 | 18 | try { 19 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 20 | 21 | SysRole sysRole = sysRoleMapper.selectById(1L); 22 | 23 | Assert.assertNotNull(sysRole); 24 | Assert.assertEquals("管理员", sysRole.getRoleName()); 25 | } finally { 26 | sqlSession.close(); 27 | } 28 | } 29 | 30 | @Test 31 | public void testSelectByIdUseResults() { 32 | SqlSession sqlSession = getSqlSession(); 33 | 34 | try { 35 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 36 | 37 | SysRole sysRole = sysRoleMapper.selectByIdUseResults(1L); 38 | 39 | Assert.assertNotNull(sysRole); 40 | Assert.assertEquals("管理员", sysRole.getRoleName()); 41 | } finally { 42 | sqlSession.close(); 43 | } 44 | } 45 | 46 | @Test 47 | public void testSelectAll() { 48 | SqlSession sqlSession = getSqlSession(); 49 | 50 | try { 51 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 52 | 53 | List sysRoleList = sysRoleMapper.selectAll(); 54 | 55 | Assert.assertNotNull(sysRoleList); 56 | Assert.assertTrue(sysRoleList.size() > 0); 57 | Assert.assertNotNull(sysRoleList.get(0).getRoleName()); 58 | } finally { 59 | sqlSession.close(); 60 | } 61 | } 62 | 63 | @Test 64 | public void testSelectAllRoleAndPrivileges() { 65 | SqlSession sqlSession = getSqlSession(); 66 | 67 | try { 68 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 69 | 70 | List sysRoleExtendList = sysRoleMapper.selectAllRoleAndPrivileges(); 71 | 72 | Assert.assertNotNull(sysRoleExtendList); 73 | Assert.assertTrue(sysRoleExtendList.size() > 0); 74 | Assert.assertNotNull(sysRoleExtendList.get(0).getRoleName()); 75 | } finally { 76 | sqlSession.close(); 77 | } 78 | } 79 | 80 | @Test 81 | public void testSelectRoleByUserId() { 82 | SqlSession sqlSession = getSqlSession(); 83 | 84 | try { 85 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 86 | 87 | List sysRoleList = sysRoleMapper.selectRoleByUserId(1L); 88 | 89 | Assert.assertNotNull(sysRoleList); 90 | Assert.assertTrue(sysRoleList.size() > 0); 91 | Assert.assertNotNull(sysRoleList.get(0).getRoleName()); 92 | } finally { 93 | sqlSession.close(); 94 | } 95 | } 96 | 97 | @Test 98 | public void testSelectRoleByUserIdChoose() { 99 | SqlSession sqlSession = getSqlSession(); 100 | 101 | try { 102 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 103 | 104 | // 将id=2的角色的enabled赋值为0 105 | SysRole sysRole = sysRoleMapper.selectById(2L); 106 | sysRole.setEnabled(Enabled.disabled); 107 | sysRoleMapper.updateById(sysRole); 108 | 109 | // 获取用户id为1的角色 110 | List sysRoleExtendList = sysRoleMapper.selectRoleByUserIdChoose(1L); 111 | for (SysRoleExtend item : sysRoleExtendList) { 112 | System.out.println("角色名:" + item.getRoleName()); 113 | if (item.getId().equals(1L)) { 114 | // 第一个角色是启用的,所以存在权限信息 115 | Assert.assertNotNull(item.getSysPrivilegeList()); 116 | } else if (item.getId().equals(2L)) { 117 | // 第二个角色是禁用的,所以权限为null 118 | Assert.assertNull(item.getSysPrivilegeList()); 119 | continue; 120 | } 121 | for (SysPrivilege sysPrivilege : item.getSysPrivilegeList()) { 122 | System.out.println("权限名:" + sysPrivilege.getPrivilegeName()); 123 | } 124 | } 125 | } finally { 126 | sqlSession.close(); 127 | } 128 | } 129 | 130 | @Test 131 | public void testUpdateById() { 132 | SqlSession sqlSession = getSqlSession(); 133 | 134 | try { 135 | SysRoleMapper sysRoleMapper = sqlSession.getMapper(SysRoleMapper.class); 136 | 137 | // 先查询出id=2的角色,然后修改角色的enabled值为disabled 138 | SysRole sysRole = sysRoleMapper.selectById(2L); 139 | Assert.assertEquals(Enabled.enabled, sysRole.getEnabled()); 140 | 141 | // 修改角色的enabled为disabled 142 | sysRole.setEnabled(Enabled.disabled); 143 | 144 | if (sysRole.getEnabled() == Enabled.disabled || sysRole.getEnabled() == Enabled.enabled) { 145 | sysRoleMapper.updateById(sysRole); 146 | } else { 147 | throw new Exception("无效的enabled值"); 148 | } 149 | } catch (Exception e) { 150 | e.printStackTrace(); 151 | } finally { 152 | sqlSession.close(); 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/test/java/com/zwwhnly/mybatisaction/mapper/SysUserMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.zwwhnly.mybatisaction.mapper; 2 | 3 | import com.zwwhnly.mybatisaction.model.*; 4 | import com.zwwhnly.mybatisaction.type.Enabled; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; 9 | 10 | import java.util.*; 11 | 12 | public class SysUserMapperTest extends BaseMapperTest { 13 | @Test 14 | public void testSelectById() { 15 | SqlSession sqlSession = getSqlSession(); 16 | 17 | try { 18 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 19 | 20 | SysUser sysUser = sysUserMapper.selectById(1L); 21 | Assert.assertNotNull(sysUser); 22 | 23 | Assert.assertEquals("admin", sysUser.getUserName()); 24 | } finally { 25 | sqlSession.close(); 26 | } 27 | } 28 | 29 | @Test 30 | public void testSelectAll() { 31 | SqlSession sqlSession = getSqlSession(); 32 | 33 | try { 34 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 35 | 36 | List sysUserList = sysUserMapper.selectAll(); 37 | 38 | Assert.assertNotNull(sysUserList); 39 | Assert.assertTrue(sysUserList.size() > 0); 40 | } finally { 41 | sqlSession.close(); 42 | } 43 | } 44 | 45 | @Test 46 | public void testSelectRolesByUserId() { 47 | SqlSession sqlSession = getSqlSession(); 48 | 49 | try { 50 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 51 | 52 | List sysRoleList = sysUserMapper.selectRolesByUserId(1L); 53 | 54 | Assert.assertNotNull(sysRoleList); 55 | Assert.assertTrue(sysRoleList.size() > 0); 56 | } finally { 57 | sqlSession.close(); 58 | } 59 | } 60 | 61 | @Test 62 | public void testInsert() { 63 | SqlSession sqlSession = getSqlSession(); 64 | 65 | try { 66 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 67 | 68 | SysUser sysUser = new SysUser(); 69 | sysUser.setUserName("test1"); 70 | sysUser.setUserPassword("123456"); 71 | sysUser.setUserEmail("test@mybatis.tk"); 72 | sysUser.setUserInfo("test info"); 73 | // 正常情况下应该读入一张图片保存到byte数组中 74 | sysUser.setHeadImg(new byte[]{1, 2, 3}); 75 | sysUser.setCreateTime(new Date()); 76 | 77 | // 这里的返回值result是执行的SQL影响的行数 78 | int result = sysUserMapper.insert(sysUser); 79 | // 只插入1条数据 80 | Assert.assertEquals(1, result); 81 | // id为null,没有给id赋值,并且没有配置回写id的值 82 | Assert.assertNull(sysUser.getId()); 83 | } finally { 84 | // 为了不影响其他测试,这里选择回滚 85 | // 默认的sqlSessionFactory.openSession()是不自动提交的 86 | // 因此不手动执行commit也不会提交到数据库 87 | sqlSession.rollback(); 88 | sqlSession.close(); 89 | } 90 | } 91 | 92 | @Test 93 | public void testInsertUseGeneratedKeys() { 94 | SqlSession sqlSession = getSqlSession(); 95 | 96 | try { 97 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 98 | 99 | SysUser sysUser = new SysUser(); 100 | sysUser.setUserName("test1"); 101 | sysUser.setUserPassword("123456"); 102 | sysUser.setUserEmail("test@mybatis.tk"); 103 | sysUser.setUserInfo("test info"); 104 | // 正常情况下应该读入一张图片保存到byte数组中 105 | sysUser.setHeadImg(new byte[]{1, 2, 3}); 106 | sysUser.setCreateTime(new Date()); 107 | 108 | // 这里的返回值result是执行的SQL影响的行数 109 | int result = sysUserMapper.insertUseGeneratedKeys(sysUser); 110 | // 只插入1条数据 111 | Assert.assertEquals(1, result); 112 | // 因为id回写,所以id不为null 113 | Assert.assertNotNull(sysUser.getId()); 114 | } finally { 115 | sqlSession.rollback(); 116 | sqlSession.close(); 117 | } 118 | } 119 | 120 | @Test 121 | public void testInsertUseSelectKey() { 122 | SqlSession sqlSession = getSqlSession(); 123 | 124 | try { 125 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 126 | 127 | SysUser sysUser = new SysUser(); 128 | sysUser.setUserName("test1"); 129 | sysUser.setUserPassword("123456"); 130 | sysUser.setUserEmail("test@mybatis.tk"); 131 | sysUser.setUserInfo("test info"); 132 | // 正常情况下应该读入一张图片保存到byte数组中 133 | sysUser.setHeadImg(new byte[]{1, 2, 3}); 134 | sysUser.setCreateTime(new Date()); 135 | 136 | // 这里的返回值result是执行的SQL影响的行数 137 | int result = sysUserMapper.insertUseSelectKey(sysUser); 138 | // 只插入1条数据 139 | Assert.assertEquals(1, result); 140 | // 因为id回写,所以id不为null 141 | Assert.assertNotNull(sysUser.getId()); 142 | } finally { 143 | sqlSession.rollback(); 144 | sqlSession.close(); 145 | } 146 | } 147 | 148 | @Test 149 | public void testUpdateById() { 150 | SqlSession sqlSession = getSqlSession(); 151 | 152 | try { 153 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 154 | SysUser sysUser = sysUserMapper.selectById(1L); 155 | 156 | Assert.assertEquals("admin", sysUser.getUserName()); 157 | 158 | sysUser.setUserName("admin_test"); 159 | sysUser.setUserEmail("admin_test@mybatis.tk"); 160 | sysUser.setUserInfo("test info"); 161 | // 正常情况下应该读入一张图片保存到byte数组中 162 | sysUser.setHeadImg(new byte[]{1, 2, 3}); 163 | sysUser.setCreateTime(new Date()); 164 | 165 | // 这里的返回值result是执行的SQL影响的行数 166 | int result = sysUserMapper.updateById(sysUser); 167 | // 只更新1条数据 168 | Assert.assertEquals(1, result); 169 | 170 | sysUser = sysUserMapper.selectById(1L); 171 | Assert.assertEquals("admin_test", sysUser.getUserName()); 172 | Assert.assertEquals("admin_test@mybatis.tk", sysUser.getUserEmail()); 173 | } finally { 174 | sqlSession.rollback(); 175 | sqlSession.close(); 176 | } 177 | } 178 | 179 | @Test 180 | public void testDeleteById() { 181 | SqlSession sqlSession = getSqlSession(); 182 | 183 | try { 184 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 185 | SysUser sysUser = sysUserMapper.selectById(1L); 186 | Assert.assertNotNull(sysUser); 187 | 188 | // 这里的返回值result是执行的SQL影响的行数 189 | int result = sysUserMapper.deleteById(1L); 190 | // 只删除1条数据 191 | Assert.assertEquals(1, result); 192 | 193 | Assert.assertNull(sysUserMapper.selectById(1L)); 194 | 195 | SysUser sysUser2 = sysUserMapper.selectById(1001L); 196 | Assert.assertNotNull(sysUser2); 197 | 198 | // 只删除1条数据 199 | Assert.assertEquals(1, sysUserMapper.deleteBySysUser(sysUser2)); 200 | 201 | Assert.assertNull(sysUserMapper.selectById(1001L)); 202 | } finally { 203 | sqlSession.rollback(); 204 | sqlSession.close(); 205 | } 206 | } 207 | 208 | @Test 209 | public void testSelectRolesByUserIdAndRoleEnabled() { 210 | SqlSession sqlSession = getSqlSession(); 211 | 212 | try { 213 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 214 | List sysRoleList = sysUserMapper.selectRolesByUserIdAndRoleEnabled(1L, 1); 215 | 216 | Assert.assertNotNull(sysRoleList); 217 | Assert.assertTrue(sysRoleList.size() > 0); 218 | } finally { 219 | sqlSession.rollback(); 220 | sqlSession.close(); 221 | } 222 | } 223 | 224 | @Test 225 | public void testSelectRolesByUserAndRole() { 226 | SqlSession sqlSession = getSqlSession(); 227 | 228 | try { 229 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 230 | 231 | SysUser sysUser = new SysUser(); 232 | sysUser.setId(1L); 233 | SysRole sysRole = new SysRole(); 234 | sysRole.setEnabled(Enabled.enabled); 235 | 236 | List sysRoleList = sysUserMapper.selectRolesByUserAndRole(sysUser, sysRole); 237 | 238 | Assert.assertNotNull(sysRoleList); 239 | Assert.assertTrue(sysRoleList.size() > 0); 240 | } finally { 241 | sqlSession.rollback(); 242 | sqlSession.close(); 243 | } 244 | } 245 | 246 | @Test 247 | public void testSelectByUser() { 248 | SqlSession sqlSession = getSqlSession(); 249 | 250 | try { 251 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 252 | 253 | // 只按用户名查询 254 | SysUser query = new SysUser(); 255 | query.setUserName("ad"); 256 | List sysUserList = sysUserMapper.selectByUser(query); 257 | Assert.assertTrue(sysUserList.size() > 0); 258 | 259 | // 只按邮箱查询 260 | query = new SysUser(); 261 | query.setUserEmail("test@mybatis.tk"); 262 | sysUserList = sysUserMapper.selectByUser(query); 263 | Assert.assertTrue(sysUserList.size() > 0); 264 | 265 | // 同时按用户民和邮箱查询 266 | query = new SysUser(); 267 | query.setUserName("ad"); 268 | query.setUserEmail("test@mybatis.tk"); 269 | sysUserList = sysUserMapper.selectByUser(query); 270 | // 由于没有同时符合这两个条件的用户,因此查询结果数为0 271 | Assert.assertTrue(sysUserList.size() == 0); 272 | } finally { 273 | sqlSession.close(); 274 | } 275 | } 276 | 277 | @Test 278 | public void testSelectByUserWhere() { 279 | SqlSession sqlSession = getSqlSession(); 280 | 281 | try { 282 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 283 | 284 | // 只按用户名查询 285 | SysUser query = new SysUser(); 286 | query.setUserName("ad"); 287 | List sysUserList = sysUserMapper.selectByUserWhere(query); 288 | Assert.assertTrue(sysUserList.size() > 0); 289 | 290 | // 只按邮箱查询 291 | query = new SysUser(); 292 | query.setUserEmail("test@mybatis.tk"); 293 | sysUserList = sysUserMapper.selectByUserWhere(query); 294 | Assert.assertTrue(sysUserList.size() > 0); 295 | 296 | // 同时按用户民和邮箱查询 297 | query = new SysUser(); 298 | query.setUserName("ad"); 299 | query.setUserEmail("test@mybatis.tk"); 300 | sysUserList = sysUserMapper.selectByUserWhere(query); 301 | // 由于没有同时符合这两个条件的用户,因此查询结果数为0 302 | Assert.assertTrue(sysUserList.size() == 0); 303 | } finally { 304 | sqlSession.close(); 305 | } 306 | } 307 | 308 | @Test 309 | public void testUpdateByIdSelective() { 310 | SqlSession sqlSession = getSqlSession(); 311 | 312 | try { 313 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 314 | 315 | SysUser sysUser = new SysUser(); 316 | // 更新id=1的用户 317 | sysUser.setId(1L); 318 | // 修改邮箱 319 | sysUser.setUserEmail("test@mybatis.tk"); 320 | 321 | int result = sysUserMapper.updateByIdSelective(sysUser); 322 | Assert.assertEquals(1, result); 323 | 324 | // 查询id=1的用户 325 | sysUser = sysUserMapper.selectById(1L); 326 | // 修改后的名字保持不变,但是邮箱变成了新的 327 | Assert.assertEquals("admin", sysUser.getUserName()); 328 | Assert.assertEquals("test@mybatis.tk", sysUser.getUserEmail()); 329 | } finally { 330 | sqlSession.close(); 331 | } 332 | } 333 | 334 | @Test 335 | public void testUpdateByIdSelectiveSet() { 336 | SqlSession sqlSession = getSqlSession(); 337 | 338 | try { 339 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 340 | 341 | SysUser sysUser = new SysUser(); 342 | // 更新id=1的用户 343 | sysUser.setId(1L); 344 | // 修改邮箱 345 | sysUser.setUserEmail("test@mybatis.tk"); 346 | 347 | int result = sysUserMapper.updateByIdSelectiveSet(sysUser); 348 | Assert.assertEquals(1, result); 349 | 350 | // 查询id=1的用户 351 | sysUser = sysUserMapper.selectById(1L); 352 | // 修改后的名字保持不变,但是邮箱变成了新的 353 | Assert.assertEquals("admin", sysUser.getUserName()); 354 | Assert.assertEquals("test@mybatis.tk", sysUser.getUserEmail()); 355 | } finally { 356 | sqlSession.close(); 357 | } 358 | } 359 | 360 | @Test 361 | public void testInsertSelective() { 362 | SqlSession sqlSession = getSqlSession(); 363 | 364 | try { 365 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 366 | 367 | SysUser sysUser = new SysUser(); 368 | sysUser.setUserName("test-selective"); 369 | sysUser.setUserPassword("123456"); 370 | sysUser.setUserInfo("test info"); 371 | sysUser.setCreateTime(new Date()); 372 | 373 | sysUserMapper.insertSelective(sysUser); 374 | 375 | // 获取刚刚插入的数据 376 | sysUser = sysUserMapper.selectById(sysUser.getId()); 377 | // 因为没有指定userEmail,所以用的是数据库的默认值 378 | Assert.assertEquals("test@mybatis.tk", sysUser.getUserEmail()); 379 | } finally { 380 | sqlSession.close(); 381 | } 382 | } 383 | 384 | @Test 385 | public void testSelectByIdOrUserName() { 386 | SqlSession sqlSession = getSqlSession(); 387 | 388 | try { 389 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 390 | 391 | // 按id查询 392 | SysUser query = new SysUser(); 393 | query.setId(1L); 394 | query.setUserName("admin"); 395 | SysUser sysUser = sysUserMapper.selectByIdOrUserName(query); 396 | Assert.assertNotNull(sysUser); 397 | 398 | // 只按userName查询 399 | query.setId(null); 400 | sysUser = sysUserMapper.selectByIdOrUserName(query); 401 | Assert.assertNotNull(sysUser); 402 | 403 | // id 和 userName 都为空 404 | query.setUserName(null); 405 | sysUser = sysUserMapper.selectByIdOrUserName(query); 406 | Assert.assertNull(sysUser); 407 | } finally { 408 | sqlSession.close(); 409 | } 410 | } 411 | 412 | @Test 413 | public void testSelectByIdList() { 414 | SqlSession sqlSession = getSqlSession(); 415 | 416 | try { 417 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 418 | 419 | List idList = new ArrayList(); 420 | idList.add(1L); 421 | idList.add(1001L); 422 | 423 | List userList = sysUserMapper.selectByIdList(idList); 424 | Assert.assertEquals(2, userList.size()); 425 | } finally { 426 | sqlSession.close(); 427 | } 428 | } 429 | 430 | @Test 431 | public void testSelectByIdArray() { 432 | SqlSession sqlSession = getSqlSession(); 433 | 434 | try { 435 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 436 | 437 | Long[] longArray = new Long[2]; 438 | longArray[0] = 1L; 439 | longArray[1] = 1001L; 440 | 441 | List userList = sysUserMapper.selectByIdArray(longArray); 442 | Assert.assertEquals(2, userList.size()); 443 | } finally { 444 | sqlSession.close(); 445 | } 446 | } 447 | 448 | @Test 449 | public void testInsertList() { 450 | SqlSession sqlSession = getSqlSession(); 451 | 452 | try { 453 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 454 | 455 | List sysUserList = new ArrayList(); 456 | for (int i = 0; i < 2; i++) { 457 | SysUser sysUser = new SysUser(); 458 | sysUser.setUserName("test" + i); 459 | sysUser.setUserPassword("123456"); 460 | sysUser.setUserEmail("test@mybatis.tk"); 461 | 462 | sysUserList.add(sysUser); 463 | } 464 | 465 | int result = sysUserMapper.insertList(sysUserList); 466 | 467 | for (SysUser sysUser : sysUserList) { 468 | System.out.println(sysUser.getId()); 469 | } 470 | 471 | Assert.assertEquals(2, result); 472 | } finally { 473 | sqlSession.close(); 474 | } 475 | } 476 | 477 | @Test 478 | public void testUpdateByMap() { 479 | SqlSession sqlSession = getSqlSession(); 480 | 481 | try { 482 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 483 | 484 | Map map = new HashMap(); 485 | map.put("id", 1L); 486 | map.put("user_email", "test@mybatis.tk"); 487 | map.put("user_password", "12345678"); 488 | 489 | Assert.assertEquals(1, sysUserMapper.updateByMap(map)); 490 | 491 | SysUser sysUser = sysUserMapper.selectById(1L); 492 | Assert.assertEquals("test@mybatis.tk", sysUser.getUserEmail()); 493 | Assert.assertEquals("12345678", sysUser.getUserPassword()); 494 | } finally { 495 | sqlSession.close(); 496 | } 497 | } 498 | 499 | @Test 500 | public void testSelectUserAndRoleById() { 501 | SqlSession sqlSession = getSqlSession(); 502 | 503 | try { 504 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 505 | 506 | // 注意这里使用1001这个用户,因为这个用户只有1个角色 507 | SysUserExtend sysUserExtend = sysUserMapper.selectUserAndRoleById(1001L); 508 | 509 | Assert.assertNotNull(sysUserExtend); 510 | Assert.assertNotNull(sysUserExtend.getSysRole()); 511 | } finally { 512 | sqlSession.close(); 513 | } 514 | } 515 | 516 | @Test 517 | public void testSelectUserAndRoleByIdResultMap() { 518 | SqlSession sqlSession = getSqlSession(); 519 | 520 | try { 521 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 522 | 523 | SysUserExtend sysUserExtend = sysUserMapper.selectUserAndRoleByIdResultMap(1001L); 524 | Assert.assertNotNull(sysUserExtend); 525 | Assert.assertNotNull(sysUserExtend.getSysRole()); 526 | } finally { 527 | sqlSession.close(); 528 | } 529 | } 530 | 531 | @Test 532 | public void testSelectUserAndRoleByIdSelect() { 533 | SqlSession sqlSession = getSqlSession(); 534 | 535 | try { 536 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 537 | 538 | SysUserExtend sysUserExtend = sysUserMapper.selectUserAndRoleByIdSelect(1001L); 539 | Assert.assertNotNull(sysUserExtend); 540 | 541 | System.out.println("调用sysUserExtend.equals(null)"); 542 | sysUserExtend.equals(null); 543 | 544 | System.out.println("调用sysUserExtend.getSysRole()"); 545 | Assert.assertNotNull(sysUserExtend.getSysRole()); 546 | } finally { 547 | sqlSession.close(); 548 | } 549 | } 550 | 551 | @Test 552 | public void testSelectAllUserAndRoles() { 553 | SqlSession sqlSession = getSqlSession(); 554 | 555 | try { 556 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 557 | 558 | List sysUserList = sysUserMapper.selectAllUserAndRoles(); 559 | System.out.println("用户数:" + sysUserList.size()); 560 | for (SysUserExtend sysUser : sysUserList) { 561 | System.out.println("用户名:" + sysUser.getUserName()); 562 | for (SysRoleExtend sysRoleExtend : sysUser.getSysRoleList()) { 563 | System.out.println("角色名:" + sysRoleExtend.getRoleName()); 564 | for (SysPrivilege sysPrivilege : sysRoleExtend.getSysPrivilegeList()) { 565 | System.out.println("权限名:" + sysPrivilege.getPrivilegeName()); 566 | } 567 | } 568 | } 569 | } finally { 570 | sqlSession.close(); 571 | } 572 | } 573 | 574 | @Test 575 | public void testSelectAllUserAndRolesSelect() { 576 | SqlSession sqlSession = getSqlSession(); 577 | 578 | try { 579 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 580 | 581 | SysUserExtend sysUserExtend = sysUserMapper.selectAllUserAndRolesSelect(1L); 582 | System.out.println("用户名:" + sysUserExtend.getUserName()); 583 | for (SysRoleExtend sysRoleExtend : sysUserExtend.getSysRoleList()) { 584 | System.out.println("角色名:" + sysRoleExtend.getRoleName()); 585 | for (SysPrivilege sysPrivilege : sysRoleExtend.getSysPrivilegeList()) { 586 | System.out.println("权限名:" + sysPrivilege.getPrivilegeName()); 587 | } 588 | } 589 | } finally { 590 | sqlSession.close(); 591 | } 592 | } 593 | 594 | @Test 595 | public void testSelectUserById() { 596 | SqlSession sqlSession = getSqlSession(); 597 | 598 | try { 599 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 600 | 601 | SysUser sysUser = new SysUser(); 602 | sysUser.setId(1L); 603 | sysUserMapper.selectUserById(sysUser); 604 | 605 | Assert.assertNotNull(sysUser.getUserName()); 606 | System.out.println("用户名:" + sysUser.getUserName()); 607 | } finally { 608 | sqlSession.close(); 609 | } 610 | } 611 | 612 | @Test 613 | public void testSelectUserPage() { 614 | SqlSession sqlSession = getSqlSession(); 615 | 616 | try { 617 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 618 | 619 | Map params = new HashMap(); 620 | params.put("userName", "ad"); 621 | params.put("offset", 0); 622 | params.put("limit", 10); 623 | 624 | List sysUserList = sysUserMapper.selectUserPage(params); 625 | Long total = (Long) params.get("total"); 626 | System.out.println("总数:" + total); 627 | for (SysUser sysUser : sysUserList) { 628 | System.out.println("用户名:" + sysUser.getUserName()); 629 | } 630 | } finally { 631 | sqlSession.close(); 632 | } 633 | } 634 | 635 | @Test 636 | public void testInsertAndDelete() { 637 | SqlSession sqlSession = getSqlSession(); 638 | 639 | try { 640 | SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); 641 | 642 | SysUser sysUser = new SysUser(); 643 | sysUser.setUserName("test1"); 644 | sysUser.setUserPassword("123456"); 645 | sysUser.setUserEmail("test@mybatis.tk"); 646 | sysUser.setUserInfo("test info"); 647 | sysUser.setHeadImg(new byte[]{1, 2, 3}); 648 | sysUserMapper.insertUserAndRoles(sysUser, "1,2"); 649 | 650 | Assert.assertNotNull(sysUser.getId()); 651 | Assert.assertNotNull(sysUser.getCreateTime()); 652 | 653 | // 删除刚刚新增的数据 654 | sysUserMapper.deleteUserById(sysUser.getId()); 655 | } finally { 656 | sqlSession.close(); 657 | } 658 | } 659 | } 660 | --------------------------------------------------------------------------------