How to pass Pojo as a parameter to SQL in MyBatis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.