Mybatis how to POJO as an argument sql

Today at work, you need to get the user's registration information into the database, the practice is to start the model all the attributes as a parameter DAO interface, then think wrong, if there are 100 property, then I would not be this interface there are 100 parameters passed in?

So I consider the whole argument as DTO Model or DAO interface to pass SQL parameters, specific implementation is as follows:

    (1) Definition DAO interface:

@Repository. 1 
2 {public interface ProjectDtoMapper 
. 3 
. 4 void to Add (@Param ( "projectDto") ProjectDto projectDto); // this must be added the comment, or not be found in the configuration file DTO objects 
. 5 
6}

    (2) defined service:

Copy the code
 1 @Service
 2 public class ProjectService {
 3     @Resource
 4     private ProjectDtoMapper projectDtoMapper;
 5 
 6     public void addProject(ProjectDto projectDto) {
 7         projectDtoMapper.addProject(projectDto);
 8     }
 9 
10  }
Copy the code

    (3) Mapper.xml configuration (important):

Copy the code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.kingnetdc.kingnetio.innerapi.service.db.dao.ProjectDtoMapper">
 4 
 5     <insert id="addProject" parameterType="com.kingnetdc.kingnetio.innerapi.service.db.dto.background.ProjectDto">
 6         INSERT INTO dana.auth_projects(project_id,project_name,sort,last_edit_date) VALUES
 7             (#{projectDto.project_id},#{projectDto.project_name},#{projectDto.sort},#{projectDto.last_edit_date})
 8     </insert>
 9 
10 </mapper>
Copy the code

Note: namespace here must be the pathname + class name DAO interface, the parameterType is the name of the path name corresponding to the DTO + class name, parameters written # {} in that the above annotation interface additional alias attribute.

 

Original Address: https:? //Www.cnblogs.com/jy107600/p/7112748.html utm_source = itdadao & utm_medium = referral

 

Guess you like

Origin www.cnblogs.com/it-deepinmind/p/11779285.html