5. Deep Understanding of Mybatis Parameters
5.1 parameterType (input type)
5.1.1 Passing Simple Types
int -- int or java.lang.Integer
string -- string or java.lang.String
Aliases are case-insensitive
5.1.2 Passing pojo objects
com.cpz.domain.User -- username,address,sex,birthday,id
POJO (Plain Ordinary Java Object) is a simple Java object, which is actually ordinary JavaBeans. It is short for avoiding confusion with EJB.
5.2 Conditions are passed through pojo objects
5.2.1 Create QueryVO classes (wrapping User classes)
package com.cpz.domain; public class QueryVO { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
5.2.2 Writing Fuzzy Query Code in UserDao Interface
//Using QueryVO to Complete Fuzzy Query List<User> findUserByQueryVOLike1(QueryVO queryVO);
5.2.3 Writing Fuzzy Query Code in UserDao.xml File
<!--Use QueryVO Complete Fuzzy Query--> <select id="findUserByQueryVOLike1" parameterType="com.cpz.domain.QueryVO" resultType="com.cpz.domain.User"> select * from user where username like #{user.username} </select>
5.2.4 Writing Fuzzy Query Code in TestUserDao
//QueryVO Fuzzy Query @Test public void findUserByQueryVOLike(){ QueryVO queryVO = new QueryVO(); User user = new User(); user.setUsername("%king%"); queryVO.setUser(user); List<User> list = userDao.findUserByQueryVOLike1(queryVO); for (User u : list) { System.out.println(u); } }
5.3 resultType (output type)
5.3.1 inconsistency between field and object attributes in database
Here we modify the original code to modify the User.java, UserDao.java and other files.
Modify the User.java file
package com.cpz.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private int userId;// Primary key ID private String userName;// User Name private String userSex;// Gender private Date userBirthday;// Birthday private String userAddress;// address public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } public Date getUserBirthday() { return userBirthday; } public void setUserBirthday(Date userBirthday) { this.userBirthday = userBirthday; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", userSex='" + userSex + '\'' + ", userBirthday=" + userBirthday + ", userAddress='" + userAddress + '\'' + '}'; } }
Solution of inconsistency between field and attribute name in 5.3.2 (Scheme 1)
Add and modify operations: We choose to modify the parameter values of OGNL expressions so that they are consistent with the attribute names in entity classes.
<!-- Query all --> <select id="findAll" resultType="com.itheima.domain.User"> select * from user </select> <!--Save users--> <insert id="saveUser" parameterType="com.itheima.domain.User"> <!-- After configuring the insert operation, get the insert data id --> <selectKey keyProperty="userId" keyColumn="id" resultType="int" order="AFTER"> select last_insert_id(); </selectKey> insert into user(username,address,sex,birthday)values(#{userName},#{userAddress},#{userSex},#{userBirthday}) </insert>
Query operation: We choose to alias each field with the attribute name in the entity class.
<!-- Query all --> <select id="findAll" resultType="com.itheima.domain.User"> select id as userId,username as userName,address as userAddress,sex as userSex,birthday as userBirthday from user; </select>
5.3.2 Fields and Attribute Names Inconsistence Problem Solution (Scheme 2) (Recommendation) (resultMap)
Step 1: Define <resultMap>in UserDao.xml
<! -- If the field name of the database is inconsistent with the attribute name in the entity class, the result encapsulates null, using resultMap id: Mapping encapsulating primary keys result: Encapsulates mappings of common fields property=": the name of an attribute in an entity column=": The name of the field in the database --> <resultMap id="userMap" type="com.cpz.domain.User"> <id property="userId" column="id"></id> <result property="userName" column="username"></result> <result property="userBirthday" column="birthday"></result> <result property="userSex" column="sex"></result> <result property="userAddress" column="address"></result> </resultMap>
Step 2: Modify the statements in UserDao.xml
Modify the statement resultType="com.cpz.domain.User" to resultMap="userMap", where userMap points to the type in <resultMap>.
<select id="findAll" resultMap="userMap"> select * from user </select>