方案二 修改查询语句使符合字段: select id, name AS username, email, password from blog_db.user where id=#{id}
2.多参数查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
方案一: 使用默认的param*占位 在接口中配置: User findUserByNameAndPassWd(String name,String passwd); 在xml中配置: <select id="findUserByNameAndPassWd" resultType="model.User"> select * from blog_db.user where name=#{param1} and password=#{param2} </select>
方案二:使用注解注入参数名 使用参数占位 在接口中配置: User findUserByNameAndPassWd2(@Param("name") String name, @Param("passwd") String passwd); 在xml中配置: <select id="findUserByNameAndPassWd2" resultType="model.User"> select * from blog_db.user where name=#{name} and password=#{passwd} </select>
3.Sort的小细节
1 2 3 4
排序的参数不能使用#{} 只能用${} <select id="TestSort" resultType="model.User"> select * from blog_db.user order by ${orderRule} desc </select>
<select id="findById" parameterType="Long" resultMap="blogResult" resultType="bean.Blog"> SELECT blog.id AS blog_id, blog.title AS blog_title, blog.content AS blog_content, user.id AS user_id, user.name AS user_name, user.email AS user_email, user.password AS user_password FROM blog, user WHERE blog.id = #{id} AND user.id=blog.owner_id; </select>
<select id="findById" parameterType="Long" resultMap="userResult" resultType="model.User"> SELECT user.id AS user_id, user.name AS user_name, user.email AS user_email, user.password AS user_password, blog.id AS blog_id, blog.title AS blog_title, blog.content AS blog_content FROM user, blog WHERE user.id = 1 AND user.id=blog.owner_id; </select>
<!--两个查询--> <select id="findById" parameterType="Long" resultMap="blogResult" resultType="model.Blog"> SELECT id AS blog_id, id AS user_id, title AS blog_title, content AS blog_content FROM blog WHERE id = #{id}; </select>
<select id="findOwnerOfBlog" parameterType="int" resultMap="userResult" resultType="model.User"> SELECT id user_id, name user_name, email user_email, password user_password FROM user WHERE id=#{user_id}; </select> </mapper>
<select id="findByUserId" resultType="model.Blog"> select id,owner_id as ownerId,title,content from blog where owner_id = #{param1} order by id asc limit #{param2}, #{param3} </select>
在接口中: List<Blog> findByUserIdWithHelper(Long ownerId, RowBounds rowBounds); 在xml中: <select id="findByUserIdWithHelper" resultType="model.Blog"> SELECT id, owner_id AS ownerId, title, content FROM blog WHERE owner_id = #{param1} ORDER BY id ASC </select>