对于接口中传入参数有以下几种方式:
- 传递一个参数
接口
//根据用户id查询
Account selectById(Integer aid);
接口xml
<!--根据id查询-->
<select id="selectById" resultType="Account">
select * from account where aid=#{aid}
</select>
测试代码
@Test
public void selectById(){
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Account account = mapper.selectById(1);
System.out.println(JSON.toJSONString(account,true));
}
测试结果
==> Preparing: select * from account where aid=?
==> Parameters: 1(Integer)
<== Columns: aid, aname, alias_name, age
<== Row: 1, 李瑞, 瑞瑞, 18
<== Total: 1
接口
//使用这种方式必须遵守约定 AccountMapper.xml中#{para1},#{param2}
Account selectByName(String name,String alias);
接口xml
<!--两个参数查询使用方式: aname=#{param1} and alias_name=#{param2}-->
<select id="selectByName" resultType="Account">
select * from account where aname=#{param1} and alias_name=#{param2}
</select>
测试代码
@Test
public void selectByName() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Account account = mapper.selectByName("李杰","杰伦");
System.out.println(JSON.toJSONString(account, true));
}
测试结果
==> Preparing: select * from account where aname=? and alias_name=?
==> Parameters: 李杰(String), 杰伦(String)
<== Columns: aid, aname, alias_name, age
<== Row: 4, 李杰, 杰伦, 21
<== Total: 1
- 两个参数使用@Param
接口
//使用注解解决两个参数的问题
Account selectByName2(@Param("aname") String name,@Param("alias_name") String alias);
接口xml
<!--两个参数查询使用方式:@Param-->
<select id="selectByName2" resultType="Account">
select * from account where aname=#{aname} and alias_name=#{alias_name}
</select>
测试代码
@Test
public void selectByName2() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Account account = mapper.selectByName2("李瑞", "瑞瑞");
System.out.println(JSON.toJSONString(account, true));
}
测试结果
==> Preparing: select * from account where aname=? and alias_name=?
==> Parameters: 李瑞(String), 瑞瑞(String)
<== Columns: aid, aname, alias_name, age
<== Row: 1, 李瑞, 瑞瑞, 18
<== Total: 1
- Map方式处理参数过多问题
接口
//使用Map方式处理参数过多的问题
Account selectByName3(Map<String,Object> map);
接口xml
<!-- 使用map解决参数过多的问题-->
<!-- #{}花括号里面的值要和map.put中key一一对应-->
<select id="selectByName3" resultType="Account">
select * from account where aname=#{aname} and alias_name=#{alias_name}
</select>
测试代码
@Test
public void selectByName3() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Map<String,Object> map = new HashMap<String, Object>();
map.put("aname","李华");
map.put("alias_name","华华");
Account account = mapper.selectByName3(map);
System.out.println(JSON.toJSONString(account, true));
}
测试结果
==> Preparing: select * from account where aname=? and alias_name=?
==> Parameters: 李华(String), 华华(String)
<== Columns: aid, aname, alias_name, age
<== Row: 2, 李华, 华华, 27
<== Total: 1
- 对象的方式解决参数过多问题
接口
//使用参数为对象的方式解决参数过多问题
Account selectByName4(Account account);
接口xml
<select id="selectByName4" resultType="Account">
select * from account where aname=#{aname} and alias_name=#{aliasname}
</select>
测试代码
@Test
public void selectByName4() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Account a = new Account();
a.setAname("李智");
a.setAliasname("智智");
Account account = mapper.selectByName4(a);
System.out.println(JSON.toJSONString(account,true));
}
测试结果
==> Preparing: select * from account where aname=? and alias_name=?
==> Parameters: 李智(String), 智智(String)
<== Columns: aid, aname, alias_name, age
<== Row: 3, 李智,
- 实体类中封装了多个对象
实体类
public class QueryVo {
private Account account;
//等等
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
接口
//现在有一种情况 如果要查询的是多张表那么就是需要将多张表的实体类封装到一个新的实体类中
Account selectByName5(QueryVo queryVo);
接口xml
<!--使用QueryVo 封装实体类-->
<select id="selectByName5" resultType="Account">
select * from account where aname=#{account.aname} and alias_name=#{account.aliasname}
</select>
测试代码
@Test
public void selectByName5() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态创建接口的实现类
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
QueryVo queryVo = new QueryVo();
Account a = new Account();
a.setAname("李瑞");
a.setAliasname("瑞瑞");
queryVo.setAccount(a);
Account account = mapper.selectByName5(queryVo);
System.out.println(JSON.toJSONString(account,true));
}
测试结果
==> Preparing: select * from account where aname=? and alias_name=?
==> Parameters: 李瑞(String), 瑞瑞(String)
<== Columns: aid, aname, alias_name, age
<== Row: 1, 李瑞, 瑞瑞, 18
<== Total: 1