Today at work, you need to insert the user's registration information into the database, the beginning is to use all the model properties as the parameters of the DAO interface, and then think of something wrong, if there are 100 properties, then I this interface is not to have 100 parameters to pass in?
So I consider the whole model or DTO as the parameters of the DAO interface, to the SQL parameter, the concrete implementation is as follows:
(1) define the DAO Interface:
1 @Repository 2 Public Interface projectdtomapper {34 void AddProject (@Param ("Projectdto") projectdto projectdto);//This annotation must be added here, or the Dto object will not be found in the configuration file 5 6 }
(2) Define service:
1 @Service2 Public classProjectservice {3 @Resource4 PrivateProjectdtomapper Projectdtomapper;5 6 Public voidAddProject (projectdto projectdto) {7 Projectdtomapper.addproject (projectdto);8 }9 Ten}
(3) Mapper.xml configuration (Important):
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 <Mappernamespace= "Com.kingnetdc.kingnetio.innerapi.service.db.dao.ProjectDtoMapper">4 5 <InsertID= "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) VALUES7 (#{projectdto.project_id},#{projectdto.project_name},#{projectdto.sort},#{projectdto.last_edit_date}) 8 </Insert>9 Ten </Mapper>
Note: The namespace here must be the path name of the DAO interface + the class name, ParameterType is the path name of the corresponding DTO + class name, #{} parameter notation is an alias in the annotations added in the above interface. The name of the property.
How to pass Pojo as a parameter to SQL in MyBatis