Mybatis 笔记——输入参数输出参数
Posted Johnny*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 笔记——输入参数输出参数相关的知识,希望对你有一定的参考价值。
输入参数
简单类型
#{任意值}
${value},占位符只能是value
对象类型
- 获取对象值
#{对象属性}
${对象属性}
select name, age from person where perSex = #{sex} or name like #{name}
java中:
Person per = new Person();
per.setSex(1);
per.setName("%z%");
List<Person > persons = personMapper.queryPersonBySexOrName(per )
或者使用${}
select name, age from person where perSex = ${sex} or name like '%${name}%'
java中:
Person per = new Person();
per.setSex(1);
per.setName("z");
List<Person > persons = personMapper.queryPersonBySexOrName(per )
<!-- 输入参数为 级联属性 -->
<select id="queryStudentByaddress" parameterType="student" resultType="student" >
select stuno,stuname,stuage from student where homeaddress = #{address.homeAddress} or schooladdress = '${address.schoolAddress}'
</select>
- 输入对象为HashMap
#{}和 ${}的区别
- 用法
#{}类型转换,自动给String类型加上’’
${} 原样输出,但是适合动态排序
select name,age from person where id= #{value}
select name,age from person where id = '${value}'
动态排序
select name,age from person Order by ${value} desc
- 安全性
#{} 可以防止SQL注入
${}不能
存储过程的调用
- 查询男/女的人数
输入: 性别(男:1; 女:0)
输出:人数
CREATE DEFINER=`root`@`localhost` PROCEDURE `selectPerBySexProcedure`(in sex int, out num int)
begin
select count(*) into num from person where person.perSex = sex;
end
存储过程应用SELECT 语句从person 表中获取person.perSex = sex的记录总数。最后将结果传递给变量num 。
<select id="selectPersonBySexProcedure" parameterType="HashMap" statementType="CALLABLE">
{
CALL selectPerBySexProcedure(
#{mySex,jdbcType=INTEGER,mode=IN},
#{perCount, jdbcType=INTEGER,mode=OUT}
)
}
</select>
public static void selectPersonBySexProcedure() throws IOException {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sqlFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("mySex", 1);//xml中mySex占位符
personMapper.selectPersonBySexProcedure(map);
int count = (int)map.get("perCount");//由于存储过程没有返回值 通过out参数来获得
System.out.println(count);
}
其中 通过statementType="CALLABLE"设置SQL的执行方式是存储过程。 存储过程的输入参数sex需要通过HashMap(即key=mySex)来指定
在使用时,通过hashmap的put方法传入输入参数的值;通过hashmap的Get方法 获取输出参数的值。
- 按id删除指定人
创建存储过程deletePersonByIdProcedure
CREATE DEFINER=`root`@`localhost` PROCEDURE `deletePersonByIdProcedure`(in id int)
begin
delete from person where person.id = id;
end
public static void deletePersonByIdProcedure() throws IOException {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sqlFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("id", 3);
personMapper.deletePersonByIdProcedure(map);
session.commit();
}
<delete id="deletePersonByIdProcedure" parameterType ="HashMap" statementType="CALLABLE">
{
CALL deletePersonByIdProcedure(
#{id, jdbcType=INTEGER, mode=IN}
)
}
</delete>
输出参数
- 简单类型(8个基本+String)
- 输出参数为实体对象类型
- 输出参数为实体对象类型的集合 :虽然输出类型为集合,但是resultType依然写 集合的元素类型(resyltType=“Person”)
- 输出参数为HashMap
<!-- 别名(不要求与属性名一致 只做mapkey时使用)作为map的key -->
<select id="selectAllPersonWithHashMap" resultType="HashMap">
select id 'id', name 'perName', age 'perAge', perSex 'sex' from person
</select>
public static void selectAllPersonWithHashMap() throws IOException {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sqlFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
List<Map<String, Object>> allPerson = personMapper.selectAllPersonWithHashMap();
for(Map<String,Object> perMap: allPerson)
System.out.println(perMap);
}
输出结果
{perAge=22, perName=johnny, sex=0, id=1}
{perAge=98, perName=weiwei, sex=1, id=2}
{perAge=11, perName=j简单, sex=0, id=52}
- 使用resultMap指定输出类型及映射关系
当属性名 和字段名 不一致时,除了使用resultMap以外,还可以使用resultType+HashMap
使用resultMap
<resultMap type="person" id="queryPersonByIdMap">
<!-- 指定类中的属性 和 表中的字段 对应关系 -->
<id property="perNo" column="id" />
<result property="perName" column="name" />
</resultMap>
使用Map的集合
别名作为map的key
personMapper.xml
<!-- 别名作为map的key -->
<select id="selectAllPersonWithHashMap" resultType="HashMap">
select id 'id', name 'perName', age 'perAge', perSex 'sex' from person
</select>
接口
List<Map<String, Object>> selectAllPersonWithHashMap();
测试类:
public static void selectAllPersonWithHashMap() throws IOException {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sqlFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
List<Map<String, Object>> allPerson = personMapper.selectAllPersonWithHashMap();
for(Map<String,Object> perMap: allPerson)
System.out.println(perMap);
}
另一种左边表中字段名 右边属性名
personMapper.xml
<!-- 左边表中字段名 右边属性名 -->
<select id="selectAllPersonByHashMap" resultType="person">
select id 'id', name 'name', age 'age', perSex 'sex' from person
</select>
接口
List<Person> selectAllPersonByHashMap();
测试:
public static void selectAllPersonByHashMap() throws IOException {
InputStream in = Resources.getResourceAsStream("config.xml");
SqlSessionFactory sqlFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = sqlFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
List<Person> allPerson = personMapper.selectAllPersonByHashMap();
for(Person per: allPerson)
System.out.println(per);
}
以上是关于Mybatis 笔记——输入参数输出参数的主要内容,如果未能解决你的问题,请参考以下文章