21.05.04
mybatis传入参数
传入参数:从Java代码中把数据传入到mapper文件的sql语句中
paramterType:写在mapper文件中的一个属性。表示dao接口中方法的参数的数据类型。
1.一个简单类型的参数:
简单类型:mybatis把java的基本数据类型个String都叫简单类型。
在mapper文件获取简单类型的一个参数的值,使用#{任意字符}
例:通过id查询student
StudentDao中的方法代码
//一个简单的参数
public Student selectStudentById(Integer id);
StudentDao.xml中的代码
<select id="selectStudentById" parameterType="java.lang.Integer" resultType="com.node.domain.Student">
select id,name,email,age from student where id=#{id}
</select>
测试代码
@Test
public void testSelectById(){
SqlSession sqlSession= MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
Student student=dao.selectStudentById(1002);
System.out.println(student);
}
2.多个参数,使用@Param命名参数
在形参定义的前面加入@Param(“自定义参数名称”)
例:通过name和age查询student
StudentDao中的方法代码
//多个参数,使用java对象作为接口中方法的参数
List<Student> selectMultiObject(QueryParam param);
StudentDao.xml中的代码
<select id="selectMultiParam" resultType="com.node.domain.Student">
select id,name,email,age from student where name=#{myname} or age=#{myage}
</select>
测试代码
@Test
public void testSelectMultiParam(){
SqlSession sqlSession= MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
List<Student> students=dao.selectMultiParam("lisi",20);
for(Student stu:students){
System.out.println(stu);
}
}
3.多个参数,使用java对象的属性值,作为参数实际值
使用对象法:#{属性名,javaType=类型名称,jdbcType=数据类型} 很少用
javaType:指java中的属性数据类型
jdbcType:在数据库中的数据类型
例:通过name和age查询student
创建java对象的代码
package com.node.vo;
public class QueryParam {
private String paramName;
private Integer paramAge;
public String getParamName() {
return paramName;
}
public void setParamName(String paramName) {
this.paramName = paramName;
}
public Integer getParamAge() {
return paramAge;
}
public void setParamAge(Integer paramAge) {
this.paramAge = paramAge;
}
@Override
public String toString() {
return "QueryParam{" +
"paramName='" + paramName + '\'' +
", paramAge=" + paramAge +
'}';
}
}
StudentDao中的方法代码
//多个参数,使用java对象作为接口中方法的参数
List<Student> selectMultiObject(QueryParam param);
StudentDao.xml中的代码
<!--
<select id="selectMultiObject" resultType="com.node.domain.Student">
select id,name,email,age from student where
name=#{paramName,javaType=java.lavg.String,jdbcType=VARCHAR}
or age=#{paramAge,javaType=java.lang.Integer,jdbcType=INTEGER}
</select>
-->
测试代码
@Test
public void testSelectMultiObject(){
SqlSession sqlSession= MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
QueryParam param=new QueryParam();
param.setParamAge(20);
param.setParamName("sunliu");
List<Student> students=dao.selectMultiObject(param);
for(Student stu:students){
System.out.println(stu);
}
}
mybatis定义的别名
左边是别名 ,右边是java类型
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_intege | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | Arraylist |
collection | Collection |
iterator | Iterator |
bigdecimal | BigDecimal |
– | – |
通常使用这种方法的简化方式:#{属性名},javaType,jdbcType的值mybatis反射能获取,不用提供
简化后的xml代码
<select id="selectMultiObject" resultType="com.node.domain.Student">
select id,name,email,age from student where id=#{paramName} or age=#{paramAge}
</select>
4.多个参数(简单类型的),按位置传值
mybatis3.4之前使用#{0}
mybatis3.4之后使用#{arg0}
StudentDao方法代码
//多个参数(简单类型),用位置传值
List<Student> selectMultiPosition(String name,Integer age);
StudentDao.xml代码
<select id="selectMultiPosition" resultType="com.node.domain.Student">
select id,name,email,age from student where id=#{arg0} or age=#{arg1}
</select>
测试代码
@Test
public void testSelectMultiPosition(){
SqlSession sqlSession= MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
List<Student> students=dao.selectMultiPosition("lisi",20);
for(Student stu:students){
System.out.println(stu);
}
}
# 和 $
区别:
1.#使用?在sql语句中做占位的,使用PreparedStatement执行sql,效率高
2.#能避免sql注入,更安全
3.$不使用占位符,是字符串的连接方式,使用Statement对象执行sql,效率低
4.$有sql注入的风险,缺乏安全性。
5.$可以替换表名或列名
$会将值一起编译,#编译sql,值后期再设置,预防了sql的注入
例:用$替换列名
StudentDao方法代码
//用$替换列名
List<Student> selectUse$Order(@Param("colName") String colName);
StudentDao.xml代码
<select id="selectUse$Order" resultType="com.node.domain.Student">
select * from student order by ${colName}
</select>
测试代码
@Test
public void testSelectMultiUse$Order(){
SqlSession sqlSession= MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
List<Student> students=dao.selectUse$Order("id");
for(Student stu:students){
System.out.println(stu);
}
}