1.resultMap
SQL mapped XML files are where all SQL statements are placed. You need to define a workspace, which is generally defined as the path of the corresponding interface class. After writing the SQL statement mapping file, you need to reference it in the MyBAtis configuration file mappers tag, for example:
<mapper resource="com/liming/manager/data/mappers/UserMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/StudentMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/ClassMapper.xml" /> <mapper resource="com/liming/manager/data/mappers/TeacherMapper.xml" /> </mappers>
When the Java interface is in a relative path to the XML file, it can not be declared in mappers of the myBatis configuration file.
SQL maps some elementary elements of XML files:
(1). cache configures cache for a given pattern (2). cache-ref refers to a cache from another pattern (3). resultMap This is the most complex but powerful element, which describes how to load objects from the result set (4). sql A SQL block that can be multiplexed by other statements (5). insert map INSERT statement (6). update map UPDATE statement (7). delete map DELEETE statement (8). select - map SELECT statement
1.1 resultMap
resultMap is the most important and powerful element in MyBatis. You can save 90% of your code than calling result sets with JDBC, or you can do a lot of things that JDBC does not support. In fact, it may take thousands of lines of code to write a complex statement equivalent to a mapping similar to an interaction. The purpose of ResultMaps is such a simple statement without unnecessary result mapping. More complex statements do not require any other than just some absolutely necessary statements to describe the relationship.
resultMap attribute: type is the java entity class; id is the identifier of this resultMap.
The mapping that resultMap can be set:
(1). constructor is used to reflect the result to an instantiated class constructor
a) idArg ID parameter; mark the result set as ID to facilitate global call
b) Average reflection to the constructor
(2). id ID result, mark the result set as ID to facilitate global call
(3). result reflects normal results to JavaBean properties
(4). association The combination of complex types; types of multiple results synthesis
a) nested result mappings Several results maps nested associations themselves, and can also be referenced to one other
(5). collection a collection of complex types
(6). The nested result mappings collection can also be referenced to another
(7). discriminator uses a result value to decide which resultMap to use
a) case case case case of the result map of some basic values
i. nested result mappings A case scenario is itself a result map, so it can also include some of the same elements, or reference an external resultMap.
1.1.1 id, result
id and result are the simplest mappings, id is the primary key mapping; result maps from other basic database table fields to entity class attributes.
The simplest example:
<resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap"> <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/> <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/> <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/> <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/> <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" /> </resultMap>
Details of the configuration of id and result statement properties:
property | describe | |
property | The attribute name that needs to be mapped to the JavaBean. | |
column | The column name or label alias of the data table. | |
javaType | A complete class name, or a type alias. If you match a JavaBean, MyBatis will usually detect it by itself. Then, if you are mapping to a HashMap, you need to specify the purpose to be achieved by javaType. | |
jdbcType | A list of types supported by the data table. This property is only useful for columns that allow empty when insert, update or delete. JDBC requires this, but MyBatis does not. If you are encoding directly for JDBC and have columns that allow empty, you want to specify this item. | |
typeHandler | Use this property to override the type processor. This value can be a complete class name or a type alias. |
<resultMap type="StudentEntity" id="studentResultMap" > <constructor> <idArg javaType="String" column="STUDENT_ID"/> <arg javaType="String" column="STUDENT_NAME"/> <arg javaType="String" column="STUDENT_SEX"/> <arg javaType="Date" column="STUDENT_BIRTHDAY"/> </constructor> </resultMap>
Of course, we need to define the constructor method of the StudentEntity entity class:
public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){ this.studentID = studentID; this.studentName = studentName; this.studentSex = studentSex; this.studentBirthday = studentBirthday; } 1.1.3 association
The union element is used to deal with a "one-to-one" relationship. You need to specify the properties of the mapped Java entity class and the javaType of the attribute (usually MyBatis will recognize it yourself). The column name of the corresponding database table. If you want to override, return the result value, you need to specify the typeHandler.
Different situations need to tell MyBatis how to load a union. MyBatis can be loaded in two ways:
(1). select: Execute an other mapping SQL statement to return a Java entity type. More flexible;
(2). resultsMap: Use a nested result map to process query result sets through join and map them to Java entity types.
For example, a class corresponds to a class teacher.
First, define the attributes of the class teacher in the class:
private TeacherEntity teacherEntity;
1.1.3.1 Use select to implement union
Example: There is the attribute of a class teacher in the class entity category. When a class entity is obtained through joint efforts, the class teacher entity is mapped at the same time.
This can directly reuse the select statements defined in the TeacherMapper.xml file for querying teacher according to its ID. And you don’t need to modify the written SQL statements, you just need to modify the resultMap directly.
Part of the contents of ClassMapper.xml file:
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/> </resultMap> <select id="getClassByID" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT WHERE CT.CLASS_ID = #{classID}; </select>Part of TeacherMapper.xml file:
<resultMap type="TeacherEntity" id="teacherResultMap"> <id property="teacherID" column="TEACHER_ID" /> <result property="teacherName" column="TEACHER_NAME" /> <result property="teacherSex" column="TEACHER_SEX" /> <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/> <result property="workDate" column="WORK_DATE"/> <result property="workDate" column="WORK_DATE"/> <result property="professional" column="PROFESSIONAL"/> </resultMap> <select id="getTeacher" parameterType="String" resultMap="teacherResultMap"> SELECT * FROM TEACHER_TBL TT WHERE TT.TEACHER_ID = #{teacherID} </select> 1.1.3.2 Use resultMap to achieve union
The same function as above is to query the class and also query the class teacher. You need to add resultMap (defined in the teacher's xml file) in the association, write new SQL (query the class table left join teacher table), and do not need teacher's select.
Modify the contents of the ClassMapper.xml file:
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/> </resultMap> <select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID WHERE CT.CLASS_ID = #{classID}; </select>For the teacherResultMap, please see the above content in the TeacherMapper.xml file.
1.1.4 Collection
Aggregation elements are used to deal with "one-to-many" relationships. You need to specify the attributes of the mapping Java entity class, the javaType of the attribute (usually ArrayList); the type of the object in the list of the object (Java entity class); the column name of the corresponding database table;
Different situations need to tell MyBatis how to load an aggregation. MyBatis can be loaded in two ways:
(1). select: Execute an other mapping SQL statement to return a Java entity type. More flexible;
(2). resultsMap: Use a nested result map to process query result sets through join and map them to Java entity types.
For example, a class has multiple students.
First define the student list attributes in the class:
private List<StudentEntity> studentList;
1.1.4.1 Use select to implement aggregation
The usage is very similar to union, the difference is that this is one-to-many, so generally the mapped lists are listed. So here you need to define javaType as ArrayList, you also need to define the type of the object in the list, and the statement name of the select that must be set (it should be noted that the select statement condition for the query student here must be the foreign key classID).
Part of the contents of ClassMapper.xml file:
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/> </resultMap> <select id="getClassByID" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT WHERE CT.CLASS_ID = #{classID}; </select>Part of the StudentMapper.xml file:
<!-- java properties, mapping definition between database table fields--> <resultMap type="StudentEntity" id="studentResultMap"> <id property="studentID" column="STUDENT_ID" /> <result property="studentName" column="STUDENT_NAME" /> <result property="studentSex" column="STUDENT_SEX" /> <result property="studentBirthday" column="STUDENT_BIRTHDAY" /> </resultMap> <!-- Query the student list, based on class id --> <select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> <include refid="selectStudentAll" /> WHERE ST.CLASS_ID = #{classID} </select> 1.1.4.2 Use resultMap to achieve aggregation
Using resultMap, you need to rewrite a SQL, left join student table.
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/> </resultMap> <select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT LEFT JOIN STUDENT_TBL ST ON CT.CLASS_ID = ST.CLASS_ID LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID WHERE CT.CLASS_ID = #{classID}; </select>
For the teacherResultMap, please see the above content in the TeacherMapper.xml file. Please see the studentResultMap in the above section of the StudentMapper.xml file.
1.1.5discriminator discriminator <br /> Sometimes a separate database query may return a result set of many different (but hopefully some correlation) data types. The discriminator element is designed to handle this situation, including the inheritance hierarchy of classes. The discriminator is very easy to understand because it behaves much like switch statements in Java.
The definition discriminator specifies column and javaType properties. Columns are where MyBatis looks for comparison values. JavaType is the right type to be used to ensure equivalent tests (although strings are useful in many cases).
The following example is that when classId is 20000001, the classId attribute is mapped.
<resultMap type="liming.student.manager.data.model.StudentEntity" id="resultMap_studentEntity_discriminator"> <id property="studentId" column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/> <result property="studentName" column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/> <result property="studentSex" column="STUDENT_SEX" javaType="int" jdbcType="INTEGER"/> <result property="studentBirthday" column="STUDENT_BIRTHDAY" javaType="Date" jdbcType="DATE"/> <result property="studentPhoto" column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" /> <result property="placeId" column="PLACE_ID" javaType="String" jdbcType="VARCHAR"/> <discriminator column="CLASS_ID" javaType="String" jdbcType="VARCHAR"> <case value="20000001" resultType="liming.student.manager.data.model.StudentEntity" > <result property="classId" column="CLASS_ID" javaType="String" jdbcType="VARCHAR"/> </case> </discriminator> </resultMap>
2. Add, delete, modify and check, parameters, and cache
2.1 select
A select element is very simple. For example:
<!-- Query the student, according to id --> <select id="getStudent" parameterType="String" resultMap="studentResultMap"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.CLASS_ID FROM STUDENT_TBL ST WHERE ST.STUDENT_ID = #{studentID} </select> This statement is called 'getStudent, has a String parameter, and returns an object of StudentEntity type.
Note that the parameter identification is: #{studentID}.
Select statement attribute configuration details:
| property | describe | Get the value | default |
| id | In this mode, a unique identifier can be referenced by other statements | ||
| parameterType | The full class name or alias for the parameter passed to this statement | ||
| resultType | The statement returns the entire class name or alias of the value type. Note that if it is a collection, then the entire class name or alias of the item in the collection is filled in here, rather than the class name of the collection itself. (resultType and resultMap cannot be used together) | ||
| resultMap | The referenced external resultMap name. Result Set Mapping is the most powerful feature in MyBatis. Many complex mappings can be easily solved. (resultType and resultMap cannot be used together) | ||
| flushCache | If set to true, the cache will be cleared every time the statement is called. The select statement is set to false by default | true|false | false |
| useCache | If set to true, the result set of the statement will be cached. The select statement is set to false by default true|false false timeout Sets the maximum time the drive waits for response before throwing an exception. The default is to no value, and the drive decides itself. | true|false | false |
| timeout | Set the maximum time the drive waits for a response before throwing an exception. The default is to be set without a value, and the drive itself decides | Positive integer | Not set |
| fetchSize | After setting a value, the drive will be excited to return after the number of result sets reaches this value. The default is not set, and the drive decides itself. | Positive integer | Drive decision |
| statementType statementType | statement, prepared statement, callable statement. Prepared statements, callable statements | STATEMENT PREPARED CALLABLE | PREPARED |
| resultSetType | forward_only, scroll_sensitive, scroll_insensitive Forwarding only, scrolling sensitive, case-insensitive scrolling | FORWARD_ONLY SCROLL_SENSITIVE SCROLL_INSENSITIVE | Drive decision |
2.2 insert
A simple insert statement:
<!-- Insert Student--> <insert id="insertStudent" parameterType="StudentEntity"> INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (#{studentID}, #{studentName}, #{studentSex}, #{studentBirthday}, #{classEntity.classID}) </insert>insert can use the automatic primary key policy supported by the database, set useGeneratedKeys=”true”, and then set keyProperty to the corresponding column, and that’s done. For example, the StudentEntity above uses auto-generated to generate a primary key for the id column. You can also use the selectKey element. The following example uses the mysql database nextval('student') as a custom function to generate a key.
<!-- Insert student automatic primary key--> <insert id="insertStudentAutoKey" parameterType="StudentEntity"> <selectKey keyProperty="studentID" resultType="String" order="BEFORE"> select nextval('student') </selectKey> INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (#{studentID}, #{studentName}, #{studentSex}, #{studentBirthday}, #{classEntity.classID}) </insert>| property | describe | Get the value | default |
| id | In this mode, a unique identifier can be referenced by other statements | ||
| parameterType | The full class name or alias for the parameter passed to this statement | ||
| flushCache | If set to true, the cache will be cleared every time the statement is called. The select statement is set to false by default | true|false | false |
| useCache | If set to true, the result set of the statement will be cached. The select statement is set to false by default true|false false timeout Sets the maximum time the drive waits for response before throwing an exception. The default is to no value, and the drive decides itself. | true|false | false |
| timeout | Set the maximum time the drive waits for a response before throwing an exception. The default is to be set without a value, and the drive itself decides | Positive integer | Not set |
| fetchSize | After setting a value, the drive will be excited to return after the number of result sets reaches this value. The default is not set, and the drive decides itself. | Positive integer | Drive decision |
| statementType statementType | statement, prepared statement, callable statement. Prepared statements, callable statements | STATEMENT PREPARED CALLABLE | PREPARED |
| useGeneratedKeys | Tell MyBatis to use JDBC's getGeneratedKeys method to get the primary key generated by the database (MySQL, SQLSERVER, etc. Relational databases will have fields that are automatically generated). Default: false | true|false | false |
| keyProperty | Identify a value to be returned by the key that is set to MyBatis into getGeneratedKeys, or use a selectKey for the insert statement Sub-element. |
| property | describe | Get the value |
| keyProperty | The attribute that needs to be set for the result generated by the selectKey statement. | |
| resultType | Generate result types. MyBatis allows the use of basic data types, including String and int types. | |
| Order | It can be set to BEFORE or AFTER. If it is set to BEFORE, it will first select the primary key, then set the keyProperty, and then execute the insert statement; if it is set to AFTER, it will first run the insert statement and then run the selectKey statement, which is usually the sequence mechanism embedded in the insert statement in the internal call database (such as Oracle). | BEFORE AFTER |
| statementType statementType | As above, MyBatis supports STATEMENT, PREPARED, and CALLABLE statement forms, corresponding to Statement, PreparedStatement and CallableStatement responses. | STATEMENT PREPARED CALLABLE |
2.3 update, delete
A simple update:
<!-- Update student information--> <update id="updateStudent" parameterType="StudentEntity"> UPDATE STUDENT_TBL SET STUDENT_TBL.STUDENT_NAME = #{studentName}, STUDENT_TBL.STUDENT_SEX = #{studentSex}, STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, STUDENT_TBL.CLASS_ID = #{classEntity.classID} WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; </update>A simple delete:
<!-- Delete Student--> <delete id="deleteStudent" parameterType="StudentEntity"> DELETE FROM STUDENT_TBL WHERE STUDENT_ID = #{studentID} </delete>
Update and delete statement attribute configuration details:
| property | describe | Get the value | default |
| id | In this mode, a unique identifier can be referenced by other statements | ||
| parameterType | The full class name or alias for the parameter passed to this statement | ||
| flushCache | If set to true, the cache will be cleared every time the statement is called. The select statement is set to false by default | true|false | false |
| useCache | If set to true, the result set of the statement will be cached. The select statement is set to false by default true|false false timeout Sets the maximum time the drive waits for response before throwing an exception. The default is to no value, and the drive decides itself. | true|false | false |
| timeout | Set the maximum time the drive waits for a response before throwing an exception. The default is to be set without a value, and the drive itself decides | Positive integer | Not set |
| fetchSize | After setting a value, the drive will be excited to return after the number of result sets reaches this value. The default is not set, and the drive decides itself. | Positive integer | Drive decision |
| statementType statementType | statement, prepared statement, callable statement. Prepared statements, callable statements | STATEMENT PREPARED CALLABLE | PREPARED |
<!-- Multiplex sql statement to query all fields of the student table --> <sql id="selectStudentAll"> SELECT ST.STUDENT_ID, ST.STUDENT_NAME, ST.STUDENT_SEX, ST.STUDENT_BIRTHDAY, ST.CLASS_ID FROM STUDENT_TBL ST </sql>
In this way, you can directly reference and use it in the select statement. Change the above select statement to:
<!-- Query the student, according to id --> <select id="getStudent" parameterType="String" resultMap="studentResultMap"> <include refid="selectStudentAll"/> WHERE ST.STUDENT_ID = #{studentID} </select> 2.5 parameters
Parameters have been used in many places above, such as query, modification, deletion conditions, insertion, modified data, etc. The basic data types that MyBatis can use and the complex data types of Java.
Basic data types, String, int, date, etc.
However, using basic data types can only provide one parameter, so you need to use Java entity class or Map type as parameter type. Its attributes can be obtained directly through #{}.
2.5.1 Basic type parameters
Search the student list based on admission time:
<!-- Query the student list, according to the admission time --> <select id="getStudentListByDate" parameterType="Date" resultMap="studentResultMap"> SELECT * FROM STUDENT_TBL ST LEFT JOIN CLASS_TBL CT ON ST.CLASS_ID = CT.CLASS_ID WHERE CT.CLASS_YEAR = #{classYear}; </select> List<StudentEntity> studentList = studentMapper.getStudentListByClassYear(StringUtil.parse("2007-9-1")); for (StudentEntity entityTemp: studentList) { System.out.println(entityTemp.toString()); } 2.5.2 Java entity type parameters
Search the student list by name and gender. Use entity classes as parameters:
<!-- Query student list, like name, = gender, parameter entity type--> <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') AND ST.STUDENT_SEX = #{studentSex} </select> StudentEntity entity = new StudentEntity(); entity.setStudentName("Li"); entity.setStudentSex("Male"); List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity); for (StudentEntity entityTemp: studentList) { System.out.println(entityTemp.toString()); } 2.5.3Map parameters
Search the student list by name and gender. Use Map as parameters:
<!-- Query student list, = Gender, parameter map type--> <select id="getStudentListWhereMap" parameterType="Map" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_SEX = #{sex} AND ST.STUDENT_SEX = #{sex} </select> Map<String, String> map = new HashMap<String, String>(); map.put("sex", "female"); map.put("name", "Li"); List<StudentEntity> studentList = studentMapper.getStudentListWhereMap(map); for (StudentEntity entityTemp: studentList) { System.out.println(entityTemp.toString()); } 2.5.4 Implementation of Multi-parameters
If you want to pass in multiple parameters, you need to add @Param annotation to the parameters of the interface. Given an example:
Interface writing:
public List<StudentEntity> getStudentListWhereParam(@Param(value = "name") String name, @Param(value = "sex") String sex, @Param(value = "birthday") Date birthday, @Param(value = "classEntity") ClassEntity classEntity);
SQL writing method:
<!-- Query student list, like name, =gender, =birthday, =class, multi-parameter method--> <select id="getStudentListWhereParam" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST <where> <if test="name!=null and name!='' "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{name}),'%') </if> <if test="sex!= null and sex!= '' "> AND ST.STUDENT_SEX = #{sex} </if> <if test="birthday!=null"> AND ST.STUDENT_BIRTHDAY = #{birthday} </if> <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> AND ST.CLASS_ID = #{classEntity.classID} </if> </where> </select>Make a query:
List<StudentEntity> studentList = studentMapper.getStudentListWhereParam(", "",StringUtil.parse("1985-05-28"), classMapper.getClassByID("20000002")); for (StudentEntity entityTemp: studentList) { System.out.println(entityTemp.toString()); } 2.5.5 String substitution method
By default, using #{} syntax causes MyBatis to generate the PreparedStatement property and use the PreparedStatement parameter (=?) to safely set the value. Try to be fast and safe, and are also frequently used. But sometimes you may want to substitute unchanged strings into SQL statements directly. For example, for ORDER BY, you might use this: ORDER BY ${columnName} but MyBatis will not modify and circumvent this string.
Note: It is very unsafe to receive and apply a user input into an unchanged statement in this way. This will allow the user to implant corrupt code, so either require the field not to allow the client to enter, or you directly check its legitimacy.
2.6 cache cache
MyBatis contains a powerful, configurable, customizable cache mechanism. MyBatis 3's cache implementation has been improved many times, which is both powerful and easier to configure. By default, the cache is not enabled. In addition to session cache, it can improve performance and solve global dependencies. Turn on Level 2 cache, you only need to add a simple line to the SQL mapping file: <cache/>
The function of this simple sentence is as follows:
(1). All select statements in the mapping file will be cached.
(2). All insert, update and delete statements in the mapping file will clear the cache.
(3). The cache is recycled using the "seldom used recently" algorithm (4). The cache will not be cleared by the set time.
(5). Each cache can store 1024 lists or references to objects (regardless of the query result).
(6). The cache will be used as a "read/write" cache, meaning that the retrieved object is not shared and is safe for the caller. There will be no other calls (7). or potential modifications to the thread.
For example, create a FIFO cache to clear once in 60 seconds, store 512 object results or list references, and return results read-only. Because modifying them in unused threads may result in reference conflicts.
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"> </cache>
You can also share the same cache configuration or instance in different namespaces. In this case, you can use cache-ref to reference another cache.
<cache-ref namespace="com.liming.manager.data.StudentMapper"/>
Cache statement attribute configuration details:
| property | illustrate | Get the value | default value |
| Eviction | Caching policy: LRU - Recent least usage: Remove objects that have not been used in the last long period. FIFI- First-in First-out: Move out the earlier object in the queue SOFT - Soft Reference: Use garbage collection mechanism to remove objects based on soft reference rules WEAK - Weak reference: Use garbage collection mechanism to forcefully remove objects based on weak reference rules | LRU FIFI SOFT WEAK | LRU |
| flushInterval | Represents a reasonable millisecond total time. The default is not set, so if you use uninterval clearing, you can only call statements to clear it. | Positive integer | Not set |
| size | The size of the cached object | Positive integer | 1024 |
| readOnly | The read-only cache will return the same instance to all callers. Therefore, none of them can be modified, which can greatly improve performance. Writable cache will pass through the sequence to return a copy of a cached object. This will be slower, but safer. So the default value is false. | true|false | false |