Mybatis parameter transfer method

  • Four ways to pass multiple parameters:
  • Sequence parameters: public User selectUser(String name,int deptId); <select id="selectUser" resultType="user">select * from user where user_name=#{0} and dept_id = #{1}</select> The numbers in #{} represent the order you wear as parameters. It is not recommended to use the .sql layer to express unintuitive
  • @Param annotation parameter: public User selectUser( @Param("userName")String name, @Param("deptId")int deptId); <select id="selectUser" resultType="user">select * from user where user_name =#{userName} and dept_id = #{deptId}</select> The name in #{} corresponds to the name modified in the annotation @Param brackets. This situation is more intuitive when there are not many parameters and is recommended for use
  • Map parameter method: public User selectUser(Map<String,Object> params); <select id="selectUser" resultType="user" parameterType="map">select * from user where user_name=#{userName} and dept_id = #{deptId}</select> The name in #{} corresponds to the key name in the Map. This method is suitable for multiple parameters, and the parameters are variable and can be flexibly passed.
  • Java Bean passing parameters::public User selectUser(User user); <select id="selectUser" resultType="user" parameterType="user">select * from user where user_name=#{userName} and dept_id = #{deptId} </select> The names in #{} correspond to the member properties in the User class. This method is very intuitive, but it needs to build an entity class, it is not easy to expand, you need to add attributes, and use it according to the situation
  • Reprint (Hollis)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125044&siteId=291194637