共计 2543 个字符,预计需要花费 7 分钟才能阅读完成。
1. 参数解决
在上个博客中,咱们能够看到 UserMapper.java 和 UserMapper.xml 文件中有这相应的对应,在这里我先将这两个文件的代码展现进去。
1.1 文件展现
UserMapper.java
package com.jt; | |
import org.apache.ibatis.annotations.Param; | |
import org.apache.ibatis.annotations.Select; | |
import java.util.Map; | |
public interface UserMapper {public User selectUser(int id);// 单个参数传递 | |
public User selectUserByIdAndName(@Param("id") Integer id,@Param("username") String username);// 多个参数传递 | |
public User selectUserByIdAndNameUseMap(Map map);// 多个参数传递 | |
public Integer insertUser(User user); | |
public boolean updateUser(User user); | |
public Integer deleteUserById(int id); | |
} |
UserMapper.xml 文件
<?xml version="1.0" encoding="UTF-8" ?> | |
<!DOCTYPE mapper | |
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
<mapper namespace="com.jt.UserMapper"> | |
<select id="selectUser" resultType="com.jt.User"> | |
select * from test01 where id = #{id} | |
</select> | |
<select id="selectUserByIdAndName" resultType="com.jt.User"> | |
select * from test01 where id = #{id} and username=#{username} | |
</select> | |
<select id="selectUserByIdAndNameUseMap" resultType="com.jt.User"> | |
select * from test01 where id = #{id} and username=#{username} | |
</select> | |
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> | |
insert into test01 (username,password) values(#{username},#{password}) | |
</insert> | |
<update id="updateUser" parameterType="com.jt.User"> | |
update test01 set username=#{username} where id=#{id} | |
</update> | |
<delete id="deleteUserById"> | |
delete from test01 where id=#{id} | |
</delete> | |
</mapper> |
1.2 文件剖析
在 UserMapper.java 接口文件中,传递的参数有,单个参数,多个参数的状况。
1.2.1 单个参数
间接应用 #{} 或者 ${} 去接管参数即可
public User selectUser(int id);// 单个参数传递 | |
<select id="selectUser" resultType="com.jt.User"> | |
select * from test01 where id = #{id} | |
</select> |
1.2.2 多个参数参数
总结 #{} 或者 ${} 这种形式取值,实质是在一个 Map 对象中依据 key 值去获取参数值。
(1)应用 @Param 定义在 xml 文件中获取参数的 key 值
public User selectUserByIdAndName(@Param("id") Integer id,@Param("username") String username);// 多个参数传递 | |
<select id="selectUserByIdAndName" resultType="com.jt.User"> | |
select * from test01 where id = #{id} and username=#{username} | |
</select> |
(2)或者不必 @Param, 在 xml 文件中应用 #{param1} 这种模式来获取相应参数的值
public User selectUserByIdAndName(Integer id,String username);// 多个参数传递 | |
<select id="selectUserByIdAndName" resultType="com.jt.User"> | |
select * from test01 where id = #{param1} and username=#{param2} | |
</select> |
(3)应用 pojo 类来传递参数
public Integer insertUser(User user); | |
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> | |
insert into test01 (username,password) values(#{username},#{password}) | |
</insert> |
(4) 应用 map 来传递参数
public User selectUserByIdAndNameUseMap(Map map);// 多个参数传递 | |
<select id="selectUserByIdAndNameUseMap" resultType="com.jt.User"> | |
select * from test01 where id = #{id} and username=#{username} | |
</select> |
1.2.3 #{} 和 ${} 的区别
{} 应用了预编译
${} 没有应用预编译,安全性绝对比拟低。
下图能够清晰的解释两者的区别。
正文完