1:使用JDBC获取参数的两种方式
- 字符串拼接方式
- 占位符方法
@Test
public void testJDBC() throws Exception{
String name="";
Class.forName("");
Connection connection = DriverManager.getConnection("", "", "");
//字符串拼接
// PreparedStatement preparedStatement = connection.prepareStatement("select * from goods where name= '"+name+"'");
//占位符赋值
PreparedStatement preparedStatement = connection.prepareStatement("select * from goods where name= ?");
preparedStatement.setString(1, name);
}
2:MyBatis的${}和#{}
- ${}:本质字符串拼接
- #{}:本质占位符赋值
3:当方法参数为单个字面量
只有一个参数,通过#{任意值}或${任意值}获取参数
Mapper方法:User getUserByName(String name);
Mapper映射:
1:使用${}
<select id="getUserByName" resultType="User">
select * from t_user where username='${name}'
</select>
编译后的sql语句:
select * from t_user where username='admin'
2:使用#{}
<select id="getUserByName" resultType="User">
select * from t_user where username=#{name}
</select>
编译后的sql语句:
select * from t_user where username=?
4:当参数为多个时
当方法存在多个参数时,MyBatis会将多个参数封装为一个map,map的key为arg,或param
Mapper方法:User getUserByIdName(String userName,String passWord);
Mapper映射:
1:使用${}
<select id="getUserByIdName" resultType="User">
select * from t_user where username='${param1}' and password='${arg1}'
</select>
编译后sql语句:
select * from t_user where username='admin' and password='123456'
2:使用#{}
<select id="getUserByIdName" resultType="User">
select * from t_user where username=#{arg0} and password=#{arg1}
</select>
编译后sql语句:
select * from t_user where username=? and password=?
此时存在两个参数,所以arg0,arg1或者param1,param2
5:将参数封装为一个map,自定义键
当我们自定义map的键后,通过${自定义键的值}或者#{自定义键的值}获取参数
Mapper方法:User checkLogin(Map<String,Object> map);
Map对象:
Map<String,Object> map=new HashMap<>();
map.put("userName","admin");
map.put("passWord","123456");
Mapper映射:
1:使用${}
<select id="checkLogin" resultType="User">
select * from t_user where username = '${userName}' and password = '${passWord}'
</select>
编译后sql语句:
select * from t_user where username = 'admin' and password = '123456'
2:使用#{}
<select id="checkLogin" resultType="User">
select * from t_user where username = #{userName} and password = #{passWord}
</select>
编译后sql语句:
select * from t_user where username = ? and password = ?
6:参数为实体类类型
当参数为实体类类型时,直接通过#{属性名},${属性名}来获取参数
实体类:(提供成员变量的get和set方法)
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String sex;
private String email;
}
Mapper方法:void insertUser(User user);
Mapper映射:
1:使用${}
<insert id="insertUser">
insert into t_user values(null,'${username}','${password}','${age}','${sex}','${email}')
</insert>
编译后sql语句:
insert into t_user values(null,'test','123','11','男','email123')`
2:使用#{}
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
</insert>
编译后sql语句:
insert into t_user values(null,?,?,?,?,?)
7:@Param注解命名参数
我们通过给方法参数添加@Param(“Value”)注解,为参数命名
Mapper方法:User checkLoginByParam(@Param("username") String userName, @Param("password") String passWord);
Mapper映射:
1:使用${}
<select id="checkLoginByParam" resultType="User">
select * from t_user where username='${username}' and password='${password}'
</select>
编译后sql语句:
select * from t_user where username='admin' and password='123456'
2:使用#{}
<select id="checkLoginByParam" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
编译后sql语句:
select * from t_user where username=? and password=?
注:仍然可以使用Param默认键
8:总结
MyBatis获取参数可以总结为两种方式:
1:如果参数为实体类类型,则直接使用#{属性名},${属性名}获取参数
2:如果参数不是实体类类型,则使用@Param()注解,对参数命名