mybatis parametric form

1 Use map

<select id="selectRole" parameterType="map" resultType="RoleMap"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

In the interface are defined as follows

List<Role> selectRole(Map map);

Note: This method is simple to use, the drawbacks is the business association is not strong view parameters need to see the code readability decline.

 

2 using the transmission parameters annotations

xml file sql statement is defined as follows

<select id="findRoleByAnnotation" resultType="roleMap"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

Interface follows

List<Role> findRoleByAnnotation(@Param("roleName") String rolename, @Param("note") String note);

Description: Name mybatis provided by @Param know # {roleName} Representative rolename. Greatly improving the readability parameters. Applicable to the case of a small number of parameters. If too many parameters, it is recommended to use javabean way.

 

3 in excess parameter, the transmission parameters used javabean

Parameters class RoleParam

public class RoleParam {
    private String roleName;
    private String note; public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } }

xml file sql query

<select id="findByJavaBean" resultType="roleMap" parameterType="com.huitong.service.command.RoleParam"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

Interface file

List<Role> findByJavaBean(RoleParam roleparam);

Description: When the number of parameters more than 5 recommended javabean manner.

1 Use map

<select id="selectRole" parameterType="map" resultType="RoleMap"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

In the interface are defined as follows

List<Role> selectRole(Map map);

Note: This method is simple to use, the drawbacks is the business association is not strong view parameters need to see the code readability decline.

 

2 using the transmission parameters annotations

xml file sql statement is defined as follows

<select id="findRoleByAnnotation" resultType="roleMap"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

Interface follows

List<Role> findRoleByAnnotation(@Param("roleName") String rolename, @Param("note") String note);

Description: Name mybatis provided by @Param know # {roleName} Representative rolename. Greatly improving the readability parameters. Applicable to the case of a small number of parameters. If too many parameters, it is recommended to use javabean way.

 

3 in excess parameter, the transmission parameters used javabean

Parameters class RoleParam

public class RoleParam {
    private String roleName;
    private String note; public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } }

xml file sql query

<select id="findByJavaBean" resultType="roleMap" parameterType="com.huitong.service.command.RoleParam"> SELECT id, roleName, note FROM role WHERE roleName LIKE Concat('%',#{roleName},'%') and note like Concat('%',#{note},'%') </select>

Interface file

List<Role> findByJavaBean(RoleParam roleparam);

Description: When the number of parameters more than 5 recommended javabean manner.

Guess you like

Origin www.cnblogs.com/xiaoshen666/p/10978392.html