MyBatis——(5)MyBatis_映射配置文件_参数处理
文章目录
1,单个参数:mybatis不会做特殊处理
#{参数名/任意名}:取出参数值。
按照MyBatis——(5)MyBatis_映射配置文件_增删改查以及获取自增主键值
的思路
我们只需要修改EmployeeMapper.java和EmployeeMapper.xml和测试类MyBatisTest.java即可
EmployeeMapper.java
public interface EmployeeMapper {
public Employee getEmpByName(String name);
}
EmployeeMapper.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.atstudying.mybatis.dao.EmployeeMapper">
<!--
<!-- public Employee getEmpByName(String name); -->
<select id="getEmpByName" resultType="com.atstudying.mybatis.bean.Employee">
select * from employee where lastName=#{1};
</select>
>
MyBatisTest.java
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//单个参数
Employee employee=mapper.getEmpByName("zlj");
System.out.println(employee);
}finally{
openSession.close();
}
}
结果
结果我们知道在EmployeeMapper.xml中的查询语句中无论#[]中的值是否是EmployeeMapper.java中传入参数的值,仍然可以查询到结果,因为单个参数:mybatis不会做特殊处理
2,多个参数:mybatis会做特殊处理。
多个参数会被封装成 一个map,
key:param1…paramN,或者参数的索引也可以
value:传入的参数值
#{}就是从map中获取指定的key的值;
EmployeeMapper.java
public interface EmployeeMapper {
public Employee getEmpByIdAndLastName1(Integer id,String lastName);
public Employee getEmpByIdAndLastName2(@Param("id")Integer id,@Param("lastName")String lastName);
public Employee getEmpByIdAndLastName3(Integer id,String lastName);
}
EmployeeMapper.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.atstudying.mybatis.dao.EmployeeMapper">
<!-- public Employee getEmpByIdAndLastName1(Integer id,String lastName);-->
<select id="getEmpByIdAndLastName1" resultType="com.atguigu.mybatis.bean.Employee">
select * from employee where id = #{id} and last_name=#{lastName}
</select>
<!-- public Employee getEmpByIdAndLastName2(Integer id,String lastName);-->
<select id="getEmpByIdAndLastName2" resultType="com.atguigu.mybatis.bean.Employee">
select * from employee where id = #{id} and last_name=#{lastName}
</select>
<!-- public Employee getEmpByIdAndLastName3(Integer id,String lastName);-->
<select id="getEmpByIdAndLastName3" resultType="com.atguigu.mybatis.bean.Employee">
select * from employee where id = #{param1} and last_name=#{param2}
</select>
>
MyBatisTest.java
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//多个参数
Employee employee2=mapper.getEmpByIdAndLastName2(1, "zlj");
System.out.println(employee2);
Employee employee3=mapper.getEmpByIdAndLastName3(1, "zlj");
System.out.println(employee3);
Employee employee1=mapper.getEmpByIdAndLastName1(1, "zlj");
System.out.println(employee1);
}finally{
openSession.close();
}
}
结果
结果可知道多个参数传进,多个参数会被封装成 一个map
key——》param1…paramN,或者参数的索引也可以
value——》:传入的参数值(#{param1})
#{}就是从map中获取指定的key的值;
多个参数:mybatis会做特殊处理
异常:
org.apache.ibatis.binding.BindingException:
Parameter ‘id’ not found.
Available parameters are [1, 0, param1, param2]
操作:
方法: public Employee getEmpByIdAndLastName1(Integer id,String lastName);
取值:#{id},#{lastName}
如上操作就会报上述异常
3,命名参数 :明确指定封装参数时map的key;@Param(“id”)
多个参数会被封装成 一个map,
key:使用@Param注解指定的值
value:参数值
#{指定的key}取出对应的参数值
在上述中已经提到代码演示,这里不再赘述
4,POJO
如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入pojo;
#{属性名}:取出传入的pojo的属性值
EmployeeMapper.java
public interface EmployeeMapper {
//以下测试POJO演示
public Employee getEmpByPOJO(Employee employee);
EmployeeMapper.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.atstudying.mybatis.dao.EmployeeMapper">
<!-- 以下测试POJO演示-->
<!-- public Employee getEmpByPOJO(Employee employee); -->
<select id="getEmpByPOJO" resultType="com.atguigu.mybatis.bean.Employee">
select * from employee where id=${id} and lastName=#{lastName}
</select>
>
MyBatisTest.java
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//POJO演示
Employee employee=new Employee(1,"zlj","[email protected]","add");
Employee employee2=mapper.getEmpByPOJO(employee);
System.out.println(employee2);
}finally{
openSession.close();
}
}
结果
结果说明,如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入pojo;
#{属性名}:取出传入的pojo的属性值
5,Map
如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入map
#{key}:取出map中对应的值
EmployeeMapper.java
public interface EmployeeMapper {
//以下测试Map演示
public Employee getEmpByMap(Map<String, Object> map);}
EmployeeMapper.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.atstudying.mybatis.dao.EmployeeMapper">
<!-- 以下测试Map演示-->
<!-- public Employee getEmpByMap(Map<String, Object> map); -->
<select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from ${tableName} where id=${id} and last_name=#{lastName}
</select>
>
MyBatisTest.java
@Test
public void test04() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//Map演示
Map<String, Object> map = new HashMap<>();
map.put("id", 2);
map.put("lastName", "Tom");
map.put("tableName", "employee");
Employee employee = mapper.getEmpByMap(map);
System.out.println(employee);
}finally{
openSession.close();
}
}
结果
结果说明我们成功传入了map的值,#{key}:取出map中对应的值
6,参数值的获取
MyBatis——小知识:MyBatis_映射配置文件_参数值获取
7,#{}:更丰富的用法
下一篇: 总结:Mybatis映射文件中常用的标签