فهرست منبع

myabtis缓存和分页+自定义类型转换器

guyanqing 1 سال پیش
والد
کامیت
e31b8cb230
30فایلهای تغییر یافته به همراه619 افزوده شده و 38 حذف شده
  1. 38 35
      04maven/mybatis/.idea/workspace.xml
  2. 24 0
      04maven/mybatis/pom.xml
  3. 38 0
      04maven/mybatis/src/main/java/com/sf/dto/JsonArrayTypeHandler.java
  4. 38 0
      04maven/mybatis/src/main/java/com/sf/dto/jsonObjectTypeHandler.java
  5. 2 1
      04maven/mybatis/src/main/java/com/sf/entity/Department.java
  6. 59 0
      04maven/mybatis/src/main/java/com/sf/entity/TbOrder.java
  7. 15 0
      04maven/mybatis/src/main/java/com/sf/mapper/TbOderMapper.java
  8. 7 0
      04maven/mybatis/src/main/java/com/sf/mapper/UserMapper.java
  9. 21 0
      04maven/mybatis/src/main/resources/DepartmentMapper.xml
  10. 17 0
      04maven/mybatis/src/main/resources/TbOderMapper.xml
  11. 11 0
      04maven/mybatis/src/main/resources/UserMapper.xml
  12. 94 0
      04maven/mybatis/src/main/resources/ehcache.xml
  13. 8 0
      04maven/mybatis/src/main/resources/mybatis-config.xml
  14. 11 2
      04maven/mybatis/src/test/java/com/sf/mapper/DepartmentMapperTest.java
  15. 31 0
      04maven/mybatis/src/test/java/com/sf/mapper/TbOderMapperTest.java
  16. 54 0
      04maven/mybatis/src/test/java/com/sf/mapper/UserMapper2Test.java
  17. 21 0
      04maven/mybatis/target/classes/DepartmentMapper.xml
  18. 17 0
      04maven/mybatis/target/classes/TbOderMapper.xml
  19. 11 0
      04maven/mybatis/target/classes/UserMapper.xml
  20. BIN
      04maven/mybatis/target/classes/com/sf/dto/JsonArrayTypeHandler.class
  21. BIN
      04maven/mybatis/target/classes/com/sf/dto/jsonObjectTypeHandler.class
  22. BIN
      04maven/mybatis/target/classes/com/sf/entity/Department.class
  23. BIN
      04maven/mybatis/target/classes/com/sf/entity/TbOrder.class
  24. BIN
      04maven/mybatis/target/classes/com/sf/mapper/TbOderMapper.class
  25. BIN
      04maven/mybatis/target/classes/com/sf/mapper/UserMapper.class
  26. 94 0
      04maven/mybatis/target/classes/ehcache.xml
  27. 8 0
      04maven/mybatis/target/classes/mybatis-config.xml
  28. BIN
      04maven/mybatis/target/test-classes/com/sf/mapper/DepartmentMapperTest.class
  29. BIN
      04maven/mybatis/target/test-classes/com/sf/mapper/TbOderMapperTest.class
  30. BIN
      04maven/mybatis/target/test-classes/com/sf/mapper/UserMapper2Test.class

+ 38 - 35
04maven/mybatis/.idea/workspace.xml

@@ -5,29 +5,28 @@
   </component>
   <component name="ChangeListManager">
     <list default="true" id="ce9a7213-3ec1-4f41-8ade-c4d87d022933" name="Changes" comment="">
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/Department.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/Employee.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/TbKey.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/TbLock.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/mapper/DepartmentMapper.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/mapper/EmployeeMapper.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/mapper/TbKeyMapper.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/DepartmentMapper.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/EmployeeMapper.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/TbKeyMapper.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/DepartmentMapperTest.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/EmployeeMapperTest.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/TbKeyMapperTest.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/dto/JsonArrayTypeHandler.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/dto/jsonObjectTypeHandler.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/TbOrder.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/mapper/TbOderMapper.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/TbOderMapper.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/ehcache.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/TbOderMapperTest.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/entity/Department.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/Department.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/mapper/UserMapper.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/mapper/UserMapper.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/resources/DepartmentMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/DepartmentMapper.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/resources/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/UserMapper.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/resources/db.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/db.properties" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/resources/mybatis-config.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/mybatis-config.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/test/java/com/sf/mapper/DepartmentMapperTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/DepartmentMapperTest.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/test/java/com/sf/mapper/UserMapper2Test.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/com/sf/mapper/UserMapper2Test.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/target/classes/DepartmentMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/DepartmentMapper.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/UserMapper.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/target/classes/com/sf/entity/Department.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/sf/entity/Department.class" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/com/sf/mapper/UserMapper.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/sf/mapper/UserMapper.class" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/target/classes/db.properties" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/db.properties" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/mybatis-config.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/mybatis-config.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/target/test-classes/com/sf/mapper/DepartmentMapperTest.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/com/sf/mapper/DepartmentMapperTest.class" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/test-classes/com/sf/mapper/UserMapper2Test.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/test-classes/com/sf/mapper/UserMapper2Test.class" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
@@ -38,8 +37,8 @@
   <component name="FileTemplateManagerImpl">
     <option name="RECENT_TEMPLATES">
       <list>
-        <option value="Class" />
         <option value="Interface" />
+        <option value="Class" />
         <option value="JUnit4 Test Class" />
       </list>
     </option>
@@ -65,6 +64,7 @@
     "WebServerToolWindowFactoryState": "false",
     "com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrary": "JUnit4",
     "com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrarySuperClass.JUnit4": "",
+    "last_opened_file_path": "D:/Program Files/IntelliJ IDEA 2019.3.3/VIP23/04maven/mybatis/src/main/resources",
     "node.js.detected.package.eslint": "true",
     "node.js.detected.package.tslint": "true",
     "node.js.selected.package.eslint": "(autodetect)",
@@ -77,6 +77,9 @@
   }
 }]]></component>
   <component name="RecentsManager">
+    <key name="CopyFile.RECENT_KEYS">
+      <recent name="D:\Program Files\IntelliJ IDEA 2019.3.3\VIP23\04maven\mybatis\src\main\resources" />
+    </key>
     <key name="CreateTestDialog.Recents.Supers">
       <recent name="" />
     </key>
@@ -84,8 +87,8 @@
       <recent name="com.sf.mapper" />
     </key>
   </component>
-  <component name="RunManager" selected="JUnit.DepartmentMapperTest.getDepartmentInfoWithEmployee2">
-    <configuration name="DepartmentMapperTest.getDepartmentInfoWithEmployee" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+  <component name="RunManager" selected="JUnit.TbOderMapperTest.getTbOrderInfo">
+    <configuration name="DepartmentMapperTest.getDepartmentInfoWithEmployee2" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="mybatis" />
       <extension name="coverage">
         <pattern>
@@ -95,13 +98,13 @@
       </extension>
       <option name="PACKAGE_NAME" value="com.sf.mapper" />
       <option name="MAIN_CLASS_NAME" value="com.sf.mapper.DepartmentMapperTest" />
-      <option name="METHOD_NAME" value="getDepartmentInfoWithEmployee" />
+      <option name="METHOD_NAME" value="getDepartmentInfoWithEmployee2" />
       <option name="TEST_OBJECT" value="method" />
       <method v="2">
         <option name="Make" enabled="true" />
       </method>
     </configuration>
-    <configuration name="DepartmentMapperTest.getDepartmentInfoWithEmployee2" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+    <configuration name="TbKeyMapperTest.getKeyWithLock" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="mybatis" />
       <extension name="coverage">
         <pattern>
@@ -110,14 +113,14 @@
         </pattern>
       </extension>
       <option name="PACKAGE_NAME" value="com.sf.mapper" />
-      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.DepartmentMapperTest" />
-      <option name="METHOD_NAME" value="getDepartmentInfoWithEmployee2" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.TbKeyMapperTest" />
+      <option name="METHOD_NAME" value="getKeyWithLock" />
       <option name="TEST_OBJECT" value="method" />
       <method v="2">
         <option name="Make" enabled="true" />
       </method>
     </configuration>
-    <configuration name="EmployeeMapperTest.getEmployeeinfo" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+    <configuration name="TbOderMapperTest.getTbOrderInfo" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="mybatis" />
       <extension name="coverage">
         <pattern>
@@ -126,14 +129,14 @@
         </pattern>
       </extension>
       <option name="PACKAGE_NAME" value="com.sf.mapper" />
-      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.EmployeeMapperTest" />
-      <option name="METHOD_NAME" value="getEmployeeinfo" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.TbOderMapperTest" />
+      <option name="METHOD_NAME" value="getTbOrderInfo" />
       <option name="TEST_OBJECT" value="method" />
       <method v="2">
         <option name="Make" enabled="true" />
       </method>
     </configuration>
-    <configuration name="EmployeeMapperTest.getEmployeeinfoWithDepartment" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+    <configuration name="UserMapper2Test.PagehelperTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="mybatis" />
       <extension name="coverage">
         <pattern>
@@ -142,14 +145,14 @@
         </pattern>
       </extension>
       <option name="PACKAGE_NAME" value="com.sf.mapper" />
-      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.EmployeeMapperTest" />
-      <option name="METHOD_NAME" value="getEmployeeinfoWithDepartment" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.UserMapper2Test" />
+      <option name="METHOD_NAME" value="PagehelperTest" />
       <option name="TEST_OBJECT" value="method" />
       <method v="2">
         <option name="Make" enabled="true" />
       </method>
     </configuration>
-    <configuration name="TbKeyMapperTest.getKeyWithLock" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+    <configuration name="UserMapper2Test.getUserListWithPageTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
       <module name="mybatis" />
       <extension name="coverage">
         <pattern>
@@ -158,8 +161,8 @@
         </pattern>
       </extension>
       <option name="PACKAGE_NAME" value="com.sf.mapper" />
-      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.TbKeyMapperTest" />
-      <option name="METHOD_NAME" value="getKeyWithLock" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.mapper.UserMapper2Test" />
+      <option name="METHOD_NAME" value="getUserListWithPageTest" />
       <option name="TEST_OBJECT" value="method" />
       <method v="2">
         <option name="Make" enabled="true" />
@@ -167,11 +170,11 @@
     </configuration>
     <recent_temporary>
       <list>
+        <item itemvalue="JUnit.TbOderMapperTest.getTbOrderInfo" />
+        <item itemvalue="JUnit.UserMapper2Test.PagehelperTest" />
+        <item itemvalue="JUnit.UserMapper2Test.getUserListWithPageTest" />
         <item itemvalue="JUnit.DepartmentMapperTest.getDepartmentInfoWithEmployee2" />
         <item itemvalue="JUnit.TbKeyMapperTest.getKeyWithLock" />
-        <item itemvalue="JUnit.EmployeeMapperTest.getEmployeeinfoWithDepartment" />
-        <item itemvalue="JUnit.EmployeeMapperTest.getEmployeeinfo" />
-        <item itemvalue="JUnit.DepartmentMapperTest.getDepartmentInfoWithEmployee" />
       </list>
     </recent_temporary>
   </component>
@@ -184,7 +187,7 @@
       <option name="presentableId" value="Default" />
       <updated>1689489421847</updated>
       <workItem from="1689489423797" duration="8067000" />
-      <workItem from="1689584772400" duration="38786000" />
+      <workItem from="1689584772400" duration="56590000" />
     </task>
     <servers />
   </component>

+ 24 - 0
04maven/mybatis/pom.xml

@@ -48,6 +48,30 @@
       <version>1.7.21</version>
     </dependency>
 
+
+<!--    mybatis - 第三方缓存依赖-->
+
+    <dependency>
+      <groupId>org.mybatis.caches</groupId>
+      <artifactId>mybatis-ehcache</artifactId>
+      <version>1.2.3</version>
+    </dependency>
+
+    <!--pagehelper插件 -->
+    <dependency>
+      <groupId>com.github.pagehelper</groupId>
+      <artifactId>pagehelper</artifactId>
+      <version>5.1.4</version>
+    </dependency>
+
+    <!--  fastjson2  -->
+    <dependency>
+      <groupId>com.alibaba.fastjson2</groupId>
+      <artifactId>fastjson2</artifactId>
+      <version>2.0.19</version>
+    </dependency>
+
+
   </dependencies>
 
   <build>

+ 38 - 0
04maven/mybatis/src/main/java/com/sf/dto/JsonArrayTypeHandler.java

@@ -0,0 +1,38 @@
+package com.sf.dto;
+
+import com.alibaba.fastjson2.JSON;
+import com.sf.entity.User;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+public class JsonArrayTypeHandler extends BaseTypeHandler<List<User>> {
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, List<User> parameter, JdbcType jdbcType) throws SQLException {
+    ps.setString(i,JSON.toJSONString(parameter));
+    }
+
+    @Override
+    public List<User> getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        // [{"id":1000,"userName":"tom","age":12},{"id":1000,"userName":"tom","age":12},{"id":1000,"userName":"tom","age":12}]
+        String string = rs.getString(columnName);
+        return JSON.parseObject(string,List.class);
+    }
+
+    @Override
+    public List<User> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        String string = rs.getString(columnIndex);
+        return JSON.parseObject(string,List.class);
+    }
+
+    @Override
+    public List<User> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        String string = cs.getString(columnIndex);
+        return JSON.parseObject(string,List.class);
+    }
+}

+ 38 - 0
04maven/mybatis/src/main/java/com/sf/dto/jsonObjectTypeHandler.java

@@ -0,0 +1,38 @@
+package com.sf.dto;
+
+import com.alibaba.fastjson2.JSON;
+import com.sf.entity.User;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class jsonObjectTypeHandler extends BaseTypeHandler<User> {
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, User parameter, JdbcType jdbcType) throws SQLException {
+        ps.setString(i, JSON.toJSONString(parameter));
+    }
+
+    @Override
+    public User getNullableResult(ResultSet rs, String columnName) throws SQLException {
+//        {"id":1000,"userName":"tom","age":12}
+        String string = rs.getString(columnName);
+        return JSON.parseObject(string,User.class);
+    }
+
+    @Override
+    public User getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+//         {"id":1000,"userName":"tom","age":12}
+        String string = rs.getString(columnIndex);
+        return JSON.parseObject(string,User.class);
+    }
+
+    @Override
+    public User getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        String string = cs.getString(columnIndex);
+        return JSON.parseObject(string,User.class);
+    }
+}

+ 2 - 1
04maven/mybatis/src/main/java/com/sf/entity/Department.java

@@ -1,12 +1,13 @@
 package com.sf.entity;
 
 
+import java.io.Serializable;
 import java.util.List;
 
 /**
  * 映射部门表的实体类
  */
-public class Department {
+public class Department implements Serializable {
     /**
      * 部门id
      */

+ 59 - 0
04maven/mybatis/src/main/java/com/sf/entity/TbOrder.java

@@ -0,0 +1,59 @@
+package com.sf.entity;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * 映射数据库的实体类
+ *
+ */
+public class TbOrder implements Serializable {
+
+    private Integer orderId;
+
+    private List<User> users;
+
+    private User user;
+
+    public TbOrder() {
+    }
+
+    public TbOrder(Integer orderId, List<User> users, User user) {
+        this.orderId = orderId;
+        this.users = users;
+        this.user = user;
+    }
+
+    public Integer getOrderId() {
+        return orderId;
+    }
+
+    public void setOrderId(Integer orderId) {
+        this.orderId = orderId;
+    }
+
+    public List<User> getUsers() {
+        return users;
+    }
+
+    public void setUsers(List<User> users) {
+        this.users = users;
+    }
+
+    public User getUser() {
+        return user;
+    }
+
+    public void setUser(User user) {
+        this.user = user;
+    }
+
+    @Override
+    public String toString() {
+        return "TbOrder{" +
+                "orderId=" + orderId +
+                ", users=" + users +
+                ", user=" + user +
+                '}';
+    }
+}

+ 15 - 0
04maven/mybatis/src/main/java/com/sf/mapper/TbOderMapper.java

@@ -0,0 +1,15 @@
+package com.sf.mapper;
+
+import com.sf.entity.TbOrder;
+
+/**
+ * 查询TbOder的接口
+ */
+public interface TbOderMapper {
+
+    /**
+     * 查询信息
+     * @return
+     */
+    TbOrder getTbOrderInfo();
+}

+ 7 - 0
04maven/mybatis/src/main/java/com/sf/mapper/UserMapper.java

@@ -32,6 +32,8 @@ public interface UserMapper {
 
     List<User>  getUserList();
 
+    int  getUserListCount();
+
     /**
      * 根据用户的id查询的详情
      */
@@ -62,4 +64,9 @@ public interface UserMapper {
      */
 
     void batchUpdateUsers(@Param("users") List<User> users);
+
+    /**
+     * user查询列表的分页
+     */
+    List<User> getUserListWithPage(@Param("startRow") Integer startRow , @Param("pageSize") Integer pageSize);
 }

+ 21 - 0
04maven/mybatis/src/main/resources/DepartmentMapper.xml

@@ -3,6 +3,27 @@
         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.sf.mapper.DepartmentMapper">
+<!--    二级缓存-->
+<!--    <cache-->
+<!--            eviction="FIFO"-->
+<!--           flushInterval="60000"-->
+<!--           size="512"-->
+<!--           readOnly="true"/>-->
+
+
+<!--    mybatis - 集成的第三方缓存ehcache-->
+    <cache type="org.mybatis.caches.ehcache.EhcacheCache">
+        <!-- 缓存创建以后,最后一次访问缓存,间隔多久缓存失效(单位s) -->
+        <property name="timeToIdleSeconds" value="3600"/>
+        <!-- 缓存创建日期起到失效时的时间间隔(单位s) -->
+        <property name="timeToLiveSeconds" value="3600"/>
+        <!-- 堆内存中缓存对象数(0:没有限制) -->
+        <property name="maxEntriesLocalHeap" value="1000"/>
+        <!-- 磁盘中的对象数,默认为0不限制 -->
+        <property name="maxEntriesLocalDisk" value="1000000"/>
+        <!-- 内存存储与释放策略(FIFO:先进先出,LFU:一直以来最少被使用的,LRU:最近最少使用的) -->
+        <property name="memoryStoreEvictionPolicy" value="LRU"/>
+    </cache>
 
     <select id="getDepartmentInfo" parameterType="integer" resultType="department">
         select * from department where dept_id = #{deptId}

+ 17 - 0
04maven/mybatis/src/main/resources/TbOderMapper.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.mapper.TbOderMapper">
+
+
+    <resultMap id="tbOrderMap" type="com.sf.entity.TbOrder">
+        <id column="order_id" property="orderId"></id>
+        <result column="users" property="users" typeHandler="com.sf.dto.JsonArrayTypeHandler"></result>
+        <result column="user" property="user" typeHandler="com.sf.dto.jsonObjectTypeHandler"></result>
+    </resultMap>
+
+    <select id="getTbOrderInfo" resultMap="tbOrderMap">
+        select * from tb_order
+    </select>
+</mapper>

+ 11 - 0
04maven/mybatis/src/main/resources/UserMapper.xml

@@ -39,6 +39,10 @@ select user_id , user_name ,create_time from user
         select user_id  , user_name ,create_time from user
     </select>
 
+    <select id="getUserListCount" resultType="int">
+        select count(*) from user
+    </select>
+
 <!--    根据用户的id查询详情-->
     <select id="getUserInfo" resultType="user" parameterType="integer">
         select user_id  , user_name ,create_time from user where user_id = #{userId}
@@ -95,4 +99,11 @@ select user_id , user_name ,create_time from user
         </foreach>
     </update>
 
+
+
+<!--    查询user列表带有分页-->
+    <select id="getUserListWithPage" resultType="user">
+        select * from  user  limit #{startRow},#{pageSize}
+    </select>
+
 </mapper>

+ 94 - 0
04maven/mybatis/src/main/resources/ehcache.xml

@@ -0,0 +1,94 @@
+<!--
+  ~ Hibernate, Relational Persistence for Idiomatic Java
+  ~
+  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
+  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
+  -->
+<ehcache>
+
+    <!-- Sets the path to the directory where cache .data files are created.
+
+         If the path is a Java System Property it is replaced by
+         its value in the running VM.
+
+         The following properties are translated:
+         user.home - User's home directory
+         user.dir - User's current working directory
+         java.io.tmpdir - Default temp file path -->
+    <diskStore path="java.io.tmpdir"/>
+
+
+    <!--Default Cache configuration. These will applied to caches programmatically created through
+        the CacheManager.
+
+        The following attributes are required for defaultCache:
+
+        maxInMemory       - Sets the maximum number of objects that will be created in memory
+        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
+                            is never expired.
+        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
+                            if the element is not eternal. Idle time is now - last accessed time
+        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
+                            if the element is not eternal. TTL is now - creation time
+        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
+                            has reached the maxInMemory limit.
+
+        -->
+    <defaultCache
+        maxElementsInMemory="10000"
+        eternal="false"
+        timeToIdleSeconds="120"
+        timeToLiveSeconds="120"
+        overflowToDisk="true"
+        />
+
+    <!--Predefined caches.  Add your cache configuration settings here.
+        If you do not have a configuration for your cache a WARNING will be issued when the
+        CacheManager starts
+
+        The following attributes are required for defaultCache:
+
+        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
+        maxInMemory       - Sets the maximum number of objects that will be created in memory
+        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
+                            is never expired.
+        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
+                            if the element is not eternal. Idle time is now - last accessed time
+        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
+                            if the element is not eternal. TTL is now - creation time
+        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
+                            has reached the maxInMemory limit.
+
+        -->
+
+    <!-- Sample cache named sampleCache1
+        This cache contains a maximum in memory of 10000 elements, and will expire
+        an element if it is idle for more than 5 minutes and lives for more than
+        10 minutes.
+
+        If there are more than 10000 elements it will overflow to the
+        disk cache, which in this configuration will go to wherever java.io.tmp is
+        defined on your system. On a standard Linux system this will be /tmp"
+        -->
+    <cache name="sampleCache1"
+        maxElementsInMemory="10000"
+        eternal="false"
+        timeToIdleSeconds="300"
+        timeToLiveSeconds="600"
+        overflowToDisk="true"
+        />
+
+    <!-- Sample cache named sampleCache2
+        This cache contains 1000 elements. Elements will always be held in memory.
+        They are not expired. -->
+    <cache name="sampleCache2"
+        maxElementsInMemory="1000"
+        eternal="true"
+        timeToIdleSeconds="0"
+        timeToLiveSeconds="0"
+        overflowToDisk="false"
+        /> -->
+
+    <!-- Place configuration for your caches following -->
+
+</ehcache>

+ 8 - 0
04maven/mybatis/src/main/resources/mybatis-config.xml

@@ -17,13 +17,20 @@ ${xxxx}:去获取外部资源文件中的属性值
         <!--        开启懒加载-->
         <setting name="lazyLoadingEnabled" value="true"/>
 
+<!--        开启二级缓存-->
+        <setting name="cacheEnabled" value="true"/>
 
     </settings>
 
+
     <typeAliases>
        <package name="com.sf.entity"/>
     </typeAliases>
 
+    <!--    配置分页插件-->
+    <plugins>
+        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
+    </plugins>
 
     <environments default="development">
         <environment id="development">
@@ -47,5 +54,6 @@ ${xxxx}:去获取外部资源文件中的属性值
         <mapper resource="DepartmentMapper.xml"></mapper>
         <mapper resource="EmployeeMapper.xml"></mapper>
         <mapper resource="TbKeyMapper.xml"></mapper>
+        <mapper resource="TbOderMapper.xml"></mapper>
     </mappers>
 </configuration>

+ 11 - 2
04maven/mybatis/src/test/java/com/sf/mapper/DepartmentMapperTest.java

@@ -53,9 +53,18 @@ public class DepartmentMapperTest {
         SqlSession sqlSession = sqlSessionFactory.openSession();
         DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
         Department departmentInfo = departmentMapper.getDepartmentInfoWithEmployee2(1000);
-        System.out.println(departmentInfo.getDeptId());
-        sqlSession.commit();
+        System.out.println("部门名称====>"+departmentInfo.getDeptName());
+        System.out.println("=====>"+"sqlSession已关闭");
+//        关闭sqlSession
         sqlSession.close();
+        SqlSession sqlSession1 = sqlSessionFactory.openSession();
+        DepartmentMapper departmentMapper1 = sqlSession1.getMapper(DepartmentMapper.class);
+        Department departmentInfo2 = departmentMapper1.getDepartmentInfoWithEmployee2(1000);
+        Department departmentInfo3 = departmentMapper1.getDepartmentInfoWithEmployee2(1000);
+        System.out.println("部门名称====>"+departmentInfo2.getDeptName());
+        System.out.println("部门名称====>"+departmentInfo3.getDeptName());
+        sqlSession1.commit();
+        sqlSession1.close();
         inputStream.close();
     }
 }

+ 31 - 0
04maven/mybatis/src/test/java/com/sf/mapper/TbOderMapperTest.java

@@ -0,0 +1,31 @@
+package com.sf.mapper;
+
+import com.sf.entity.TbOrder;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.*;
+
+public class TbOderMapperTest {
+
+    @Test
+    public void getTbOrderInfo() throws IOException {
+        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
+        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
+        SqlSession sqlSession = sqlSessionFactory.openSession();
+
+        TbOderMapper tbOderMapper = sqlSession.getMapper(TbOderMapper.class);
+
+        TbOrder tbOrderInfo = tbOderMapper.getTbOrderInfo();
+        System.out.println(tbOrderInfo);
+        sqlSession.commit();
+        sqlSession.close();
+        inputStream.close();
+    }
+}

+ 54 - 0
04maven/mybatis/src/test/java/com/sf/mapper/UserMapper2Test.java

@@ -1,5 +1,7 @@
 package com.sf.mapper;
 
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
 import com.sf.entity.User;
 import org.apache.ibatis.io.Resources;
 import org.apache.ibatis.session.SqlSession;
@@ -123,4 +125,56 @@ public class UserMapper2Test {
         sqlSession.close();
         inputStream.close();
     }
+
+
+    /**
+     * 测试查询user列表带分页
+     */
+    @Test
+    public void getUserListWithPageTest() throws IOException {
+        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
+        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
+        SqlSession sqlSession = sqlSessionFactory.openSession();
+        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
+//        调用查询分页列表的方法
+//        第一页
+        Integer pageNo = 2;
+//        页大小  每页显示2条数据
+        Integer pageSize = 3;
+//        开始行
+        Integer startRow =(pageNo-1) * pageSize;
+        List<User> userListWithPage = userMapper.getUserListWithPage(startRow, pageSize);
+        System.out.println("分页查询"+userListWithPage);
+        sqlSession.commit();
+        sqlSession.close();
+        inputStream.close();
+    }
+
+    /**
+     * 测试查询user列表带分页---使用分页插件
+     */
+    @Test
+    public void PagehelperTest() throws IOException {
+        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
+        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
+        SqlSession sqlSession = sqlSessionFactory.openSession();
+        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
+//        分页
+//        注意:分页插件这行代码和查询全部这行代码必须挨着   中间不能添加任何逻辑代码
+        PageHelper.startPage(1,1);
+        List<User> userList = userMapper.getUserList();
+        PageInfo<User> userPageInfo = new PageInfo<>(userList);
+        System.out.println("当前页"+userPageInfo.getPageNum());
+        System.out.println("页大小"+userPageInfo.getPageSize());
+        System.out.println(userPageInfo.getList());
+        System.out.println("总页数"+userPageInfo.getPages());
+        System.out.println("userList=====>"+userList);
+        for (User user : userList) {
+            System.out.println(user);
+        }
+
+        sqlSession.commit();
+        sqlSession.close();
+        inputStream.close();
+    }
 }

+ 21 - 0
04maven/mybatis/target/classes/DepartmentMapper.xml

@@ -3,6 +3,27 @@
         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.sf.mapper.DepartmentMapper">
+<!--    二级缓存-->
+<!--    <cache-->
+<!--            eviction="FIFO"-->
+<!--           flushInterval="60000"-->
+<!--           size="512"-->
+<!--           readOnly="true"/>-->
+
+
+<!--    mybatis - 集成的第三方缓存ehcache-->
+    <cache type="org.mybatis.caches.ehcache.EhcacheCache">
+        <!-- 缓存创建以后,最后一次访问缓存,间隔多久缓存失效(单位s) -->
+        <property name="timeToIdleSeconds" value="3600"/>
+        <!-- 缓存创建日期起到失效时的时间间隔(单位s) -->
+        <property name="timeToLiveSeconds" value="3600"/>
+        <!-- 堆内存中缓存对象数(0:没有限制) -->
+        <property name="maxEntriesLocalHeap" value="1000"/>
+        <!-- 磁盘中的对象数,默认为0不限制 -->
+        <property name="maxEntriesLocalDisk" value="1000000"/>
+        <!-- 内存存储与释放策略(FIFO:先进先出,LFU:一直以来最少被使用的,LRU:最近最少使用的) -->
+        <property name="memoryStoreEvictionPolicy" value="LRU"/>
+    </cache>
 
     <select id="getDepartmentInfo" parameterType="integer" resultType="department">
         select * from department where dept_id = #{deptId}

+ 17 - 0
04maven/mybatis/target/classes/TbOderMapper.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.mapper.TbOderMapper">
+
+
+    <resultMap id="tbOrderMap" type="com.sf.entity.TbOrder">
+        <id column="order_id" property="orderId"></id>
+        <result column="users" property="users" typeHandler="com.sf.dto.JsonArrayTypeHandler"></result>
+        <result column="user" property="user" typeHandler="com.sf.dto.jsonObjectTypeHandler"></result>
+    </resultMap>
+
+    <select id="getTbOrderInfo" resultMap="tbOrderMap">
+        select * from tb_order
+    </select>
+</mapper>

+ 11 - 0
04maven/mybatis/target/classes/UserMapper.xml

@@ -39,6 +39,10 @@ select user_id , user_name ,create_time from user
         select user_id  , user_name ,create_time from user
     </select>
 
+    <select id="getUserListCount" resultType="int">
+        select count(*) from user
+    </select>
+
 <!--    根据用户的id查询详情-->
     <select id="getUserInfo" resultType="user" parameterType="integer">
         select user_id  , user_name ,create_time from user where user_id = #{userId}
@@ -95,4 +99,11 @@ select user_id , user_name ,create_time from user
         </foreach>
     </update>
 
+
+
+<!--    查询user列表带有分页-->
+    <select id="getUserListWithPage" resultType="user">
+        select * from  user  limit #{startRow},#{pageSize}
+    </select>
+
 </mapper>

BIN
04maven/mybatis/target/classes/com/sf/dto/JsonArrayTypeHandler.class


BIN
04maven/mybatis/target/classes/com/sf/dto/jsonObjectTypeHandler.class


BIN
04maven/mybatis/target/classes/com/sf/entity/Department.class


BIN
04maven/mybatis/target/classes/com/sf/entity/TbOrder.class


BIN
04maven/mybatis/target/classes/com/sf/mapper/TbOderMapper.class


BIN
04maven/mybatis/target/classes/com/sf/mapper/UserMapper.class


+ 94 - 0
04maven/mybatis/target/classes/ehcache.xml

@@ -0,0 +1,94 @@
+<!--
+  ~ Hibernate, Relational Persistence for Idiomatic Java
+  ~
+  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
+  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
+  -->
+<ehcache>
+
+    <!-- Sets the path to the directory where cache .data files are created.
+
+         If the path is a Java System Property it is replaced by
+         its value in the running VM.
+
+         The following properties are translated:
+         user.home - User's home directory
+         user.dir - User's current working directory
+         java.io.tmpdir - Default temp file path -->
+    <diskStore path="java.io.tmpdir"/>
+
+
+    <!--Default Cache configuration. These will applied to caches programmatically created through
+        the CacheManager.
+
+        The following attributes are required for defaultCache:
+
+        maxInMemory       - Sets the maximum number of objects that will be created in memory
+        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
+                            is never expired.
+        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
+                            if the element is not eternal. Idle time is now - last accessed time
+        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
+                            if the element is not eternal. TTL is now - creation time
+        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
+                            has reached the maxInMemory limit.
+
+        -->
+    <defaultCache
+        maxElementsInMemory="10000"
+        eternal="false"
+        timeToIdleSeconds="120"
+        timeToLiveSeconds="120"
+        overflowToDisk="true"
+        />
+
+    <!--Predefined caches.  Add your cache configuration settings here.
+        If you do not have a configuration for your cache a WARNING will be issued when the
+        CacheManager starts
+
+        The following attributes are required for defaultCache:
+
+        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
+        maxInMemory       - Sets the maximum number of objects that will be created in memory
+        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
+                            is never expired.
+        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
+                            if the element is not eternal. Idle time is now - last accessed time
+        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
+                            if the element is not eternal. TTL is now - creation time
+        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
+                            has reached the maxInMemory limit.
+
+        -->
+
+    <!-- Sample cache named sampleCache1
+        This cache contains a maximum in memory of 10000 elements, and will expire
+        an element if it is idle for more than 5 minutes and lives for more than
+        10 minutes.
+
+        If there are more than 10000 elements it will overflow to the
+        disk cache, which in this configuration will go to wherever java.io.tmp is
+        defined on your system. On a standard Linux system this will be /tmp"
+        -->
+    <cache name="sampleCache1"
+        maxElementsInMemory="10000"
+        eternal="false"
+        timeToIdleSeconds="300"
+        timeToLiveSeconds="600"
+        overflowToDisk="true"
+        />
+
+    <!-- Sample cache named sampleCache2
+        This cache contains 1000 elements. Elements will always be held in memory.
+        They are not expired. -->
+    <cache name="sampleCache2"
+        maxElementsInMemory="1000"
+        eternal="true"
+        timeToIdleSeconds="0"
+        timeToLiveSeconds="0"
+        overflowToDisk="false"
+        /> -->
+
+    <!-- Place configuration for your caches following -->
+
+</ehcache>

+ 8 - 0
04maven/mybatis/target/classes/mybatis-config.xml

@@ -17,13 +17,20 @@ ${xxxx}:去获取外部资源文件中的属性值
         <!--        开启懒加载-->
         <setting name="lazyLoadingEnabled" value="true"/>
 
+<!--        开启二级缓存-->
+        <setting name="cacheEnabled" value="true"/>
 
     </settings>
 
+
     <typeAliases>
        <package name="com.sf.entity"/>
     </typeAliases>
 
+    <!--    配置分页插件-->
+    <plugins>
+        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
+    </plugins>
 
     <environments default="development">
         <environment id="development">
@@ -47,5 +54,6 @@ ${xxxx}:去获取外部资源文件中的属性值
         <mapper resource="DepartmentMapper.xml"></mapper>
         <mapper resource="EmployeeMapper.xml"></mapper>
         <mapper resource="TbKeyMapper.xml"></mapper>
+        <mapper resource="TbOderMapper.xml"></mapper>
     </mappers>
 </configuration>

BIN
04maven/mybatis/target/test-classes/com/sf/mapper/DepartmentMapperTest.class


BIN
04maven/mybatis/target/test-classes/com/sf/mapper/TbOderMapperTest.class


BIN
04maven/mybatis/target/test-classes/com/sf/mapper/UserMapper2Test.class