对于接口中传入参数有以下几种方式:
传递一个参数//根据用户id查询 Account selectById(Integer aid); 12
接口xml
<!--根据id查询--> <select id="selectById" resultType="Account"> select * from account where aid=#{aid} </select> 1234
测试代码
@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)); } 12345678
测试结果
==> Preparing: select * from account where aid=? ==> Parameters: 1(Integer) <== Columns: aid, aname, alias_name, age <== Row: 1, 李瑞, 瑞瑞, 18 <== Total: 1 12345
接口
//使用这种方式必须遵守约定 AccountMapper.xml中#{para1},#{param2} Account selectByName(String name,String alias); 12
接口xml
<!--两个参数查询使用方式: aname=#{param1} and alias_name=#{param2}--> <select id="selectByName" resultType="Account"> select * from account where aname=#{param1} and alias_name=#{param2} </select> 1234
测试代码
@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)); } 12345678
测试结果
==> Preparing: select * from account where aname=? and alias_name=? ==> Parameters: 李杰(String), 杰伦(String) <== Columns: aid, aname, alias_name, age <== Row: 4, 李杰, 杰伦, 21 <== Total: 1 12345 两个参数使用@Param
接口
//使用注解解决两个参数的问题 Account selectByName2(@Param("aname") String name,@Param("alias_name") String alias); 12
接口xml
<!--两个参数查询使用方式:@Param--> <select id="selectByName2" resultType="Account"> select * from account where aname=#{aname} and alias_name=#{alias_name} </select> 1234
测试代码
@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)); } 12345678
测试结果
==> Preparing: select * from account where aname=? and alias_name=? ==> Parameters: 李瑞(String), 瑞瑞(String) <== Columns: aid, aname, alias_name, age <== Row: 1, 李瑞, 瑞瑞, 18 <== Total: 1 12345 Map方式处理参数过多问题
接口
//使用Map方式处理参数过多的问题 Account selectByName3(Map<String,Object> map); 12
接口xml
<!-- 使用map解决参数过多的问题--> <!-- #{}花括号里面的值要和map.put中key一一对应--> <select id="selectByName3" resultType="Account"> select * from account where aname=#{aname} and alias_name=#{alias_name} </select> 12345
测试代码
@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)); } 1234567891011
测试结果
==> Preparing: select * from account where aname=? and alias_name=? ==> Parameters: 李华(String), 华华(String) <== Columns: aid, aname, alias_name, age <== Row: 2, 李华, 华华, 27 <== Total: 1 12345 对象的方式解决参数过多问题
接口
//使用参数为对象的方式解决参数过多问题 Account selectByName4(Account account); 12
接口xml
<select id="selectByName4" resultType="Account"> select * from account where aname=#{aname} and alias_name=#{aliasname} </select> 1234
测试代码
@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)); } 123456789101112
测试结果
==> Preparing: select * from account where aname=? and alias_name=? ==> Parameters: 李智(String), 智智(String) <== Columns: aid, aname, alias_name, age <== Row: 3, 李智, 1234 实体类中封装了多个对象
实体类
public class QueryVo { private Account account; //等等 public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } } 123456789101112
接口
//现在有一种情况 如果要查询的是多张表那么就是需要将多张表的实体类封装到一个新的实体类中 Account selectByName5(QueryVo queryVo); 12
接口xml
<!--使用QueryVo 封装实体类--> <select id="selectByName5" resultType="Account"> select * from account where aname=#{account.aname} and alias_name=#{account.aliasname} </select> 1234
测试代码
@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)); } 12345678910111213
测试结果
==> Preparing: select * from account where aname=? and alias_name=? ==> Parameters: 李瑞(String), 瑞瑞(String) <== Columns: aid, aname, alias_name, age <== Row: 1, 李瑞, 瑞瑞, 18 <== Total: 1 12345
相关知识
JDBC与MyBatis:数据库访问技术的变迁【后端 15】
写了个数据查询为空的 Bug,你会怎么办?
sklearn.svm.svc参数解析
[你必须知道的.NET]第十二回:参数之惑ms.translationtype: HT
在线鲜花销售系统的设计与实现(JSP+java+springmvc+mysql+MyBatis)
嫁接植物体中RNA分子长距离传递研究进展
CA1054:URI 参数不应为字符串(代码分析)
植物营养速测仪实现四项营养参数的快速无损检测
【错误】jsp查询字符串中空格导致的异常问题!
15亿参数的GPT
网址: mybatis中多个参数传递的问题 https://m.huajiangbk.com/newsview547410.html
上一篇: 融合贝店+玛伊微享淘+花生日记= |
下一篇: 信息差=钱,获取信息差的渠道有哪 |