1. primer
mybatis lazy loading, mainly used in a complex entity class data types of properties, including one and one-to-many relationship. This kind of property is often also correspond to another data tables, and demand do not necessarily need the actual query data table, then the time delay in loading table query with respect to even have a great advantage, to achieve the purpose loaded on demand . This cost has great significance in improving the access speed and reduce system resources.
2. Double-table query
BACKGROUND: user entity class contains a character User Role entity class attributes
User entity class attributes
private Integer id; // id private String userCode; // user code private String the userName; // User name private String the userPassword; // user password private Integer userRole; // associated user id field role, and the roles table smbms_role corresponding private Role Role; // one to one correspondence between the Private Integer gender; // sex Private a date Birthday; // date of birth Private String phone; // phone Private String address; // address Private String userRoleName; // user role name Private Integer createdBy; // creator of Private a Date creationDate; // create time Private Integer modifyBy; // updated by Private a Date ModifyDate; // update time
Role entity class attributes,
Private Integer the above mentioned id; // the above mentioned id Private String roleCode; // character encoding Private String roleName; // role names Private Integer createdBy; // creator of Private a Date creationDate; // create time Private Integer modifyBy; // updated by Private a Date ModifyDate ; // update
xml sql statement mapping file
<!-- 演示 一对一 association 的连表查询 start --> <resultMap type="Role" id="roleMap"> <id property="id" column="r_id" /> <result property="roleCode" column="roleCode" /> <result property="roleName" column="roleName" /> </resultMap> <resultMap type="User" id="userRoleResult"> <id property="id" column="id" /> <result property="userCode" column="userCode" /> <result property="userName" column="userName" /> <association property="role" javaType="Role" resultMap="roleMap" /> </resultMap> <!-- 根据用户表中角色id 和角色表做联表查询 --> <select id="getUserListByRoleId" parameterType="int" resultMap="userRoleResult"> select u.* ,r.id as r_id,r.roleCode,r.roleName from smbms_user u,smbms_role r where u.userRole = #{userRoleId} and u.userRole = r.id </select>
<!-- 演示 一对一 association 的连表查询 end -->
Test Methods
@Test public void selectUserByIdsAndRoleId() { List<User> users = sqlSession.getMapper(UserMapper.class).selectUserByIdsAndRoleId(3, Arrays.asList(4, 3, 7,12)); for (User userElement : users) { System.out.println(userElement); } }
3. Delayed loading
Lazy loading can be configured in the main configuration file mybatis-config.xml, the configuration here is global configuration, the default delay loading process all queries SQL.
< Settings > <-! Enable lazy loading -> < Setting name = "lazyLoadingEnabled" value = "to true" /> <-! Close actively loaded -> < Setting name = "aggressiveLazyLoading" value = "false" / > </ Settings >
Also, if the configuration of the global satisfied, may be added to collect or add attributes assocation tag fetchType = "lazy" (delay loading) or fetchType = "eager" (immediate loading) In a specific SQL statement mapping,
Loading the SQL herein cover SQL loading of global configuration, pseudocode
<collection property="addressList" ofType="Address" column="id" fetchType="lazy|eager"/>
The mapping code xml sql
<!--演示 一对多 collection --> <resultMap type="User" id="userAddressResult"> <id property="id" column="id" /> <result property="userCode" column="userCode" /> <result property="phone" column="phone" /> <result property="userName" column="userName" /> <collection property="addressList" ofType= "Address" column = "the above mentioned id" the SELECT = "lazyFindAddressListByUserId" fetchType = "lazy" /> </ resultMap > <-! Delayed demand loading sql statement (ie may not use this sql) -> < the SELECT the above mentioned id = "lazyFindAddressListByUserId" the parameterType = "int" the resultType = "the address" > SELECT * WHERE smbms_address from the userId ID = # {} </ SELECT > <-! ID to obtain a plurality of user addresses according to a user -> < SELECT ID = "getAddressListByUserId" parameterType="Integer" resultMap="userAddressResult"> select * from smbms_user where id = #{userId} </select>
Not using the test method addressList address information
@Test public void getAddressListByUserId() { List<User> users = sqlSession.getMapper(UserMapper.class).getAddressListByUserId(5); for (User userElement : users) { //不使用addressList属性 System.out.println(userElement.getPhone() +" "+userElement.getUserCode()); // System.out.println(userElement); } }
Console displays only one sql statement, in fact, only a user queries the table
Using the test method addressList address information
@Test public void getAddressListByUserId() { List<User> users = sqlSession.getMapper(UserMapper.class).getAddressListByUserId(5); for (User userElement : users) { // 使用addressList属性 // System.out.println(userElement.getPhone() +" "+userElement.getUserCode()); System.out.println(userElement); } }
The console displays only two sql statement, in fact, only the first query the user table, and then the user id to query the address table