多平台统一管理软件接口,如何实现多平台统一管理软件接口
210
2023-07-12
Mybatis传递多个参数进行SQL查询的用法
PS:ibatis3如何传递多个参数有两个方法:一种是使用java.Map,另一种是使用JavaBean。
当只向xxxMapper.xml文件中传递一个参数时,可以简单的用“_parameter”来接收xxxMapper.java传递进来的参数,并代入查询,比如说这样:
(1)xxxMapper.java文件中这样定义:
List
(2)这时在对应的xxxMapper.xml文件中可以使用“_parameter”来接收这个参数:
parameterType="java.lang.Boolean">
select DEPARTURE_AIRPORT from USR_AIR_LINE union select
ARRIVAL_AIRPORT from USR_AIR_LINE
union select REL_DEPARTURE_AIRPORT from USR_AIR_LINE union
select
REL_ARRIVAL_AIRPORT from USR_AIR_LINE
但是,如果在xxxMapper.java文件中传递进来多个参数,就不能使用上面这种形式来接收参数,这时可以有两种方案来解决这个问题:
一 向xml文件中传递进去一个Map
具体实例如下:
(1)xxxMapper.java文件中这样定义:
List
(2)在用到上面定义的具体实现类中给Map传参:
public List
HashMap
params.phttp://ut("page", page);
params.put("airline", airline);
return airlineMapper.findAll(params);
}
(3)此时对应的xxxMapper.xml文件使用“java.util.Map”来接收这个Map集合:
value="#{'id':'ID','departureAirport':'DEPARTURE_AIRPORT','relDepartureAirport':'REL_DEPARTURE_AIRPORT','arrivalAirport':'ARRIVAL_AIRPORT','relArrivalAirport':'REL_ARRIVAL_AIRPORT','popStatus':'POP_STATUS','status':'STATUS','creator':'CREATOR','createTime':'CREATE_TIME'}" /> value="#{'ID':'id','DEPARTURE_AIRPORT':'departureAirport','REL_DEPARTURE_AIRPORT':'relDepartureAirport','ARRIVAL_AIRPORT':'arrivalAirport','REL_ARRIVAL_AIRPORT':'relArrivalAirport','POP_STATUS':'popStatus','STATUS':'status','CREATOR':'creator','CREATE_TIME':'createTime'}" /> <![CDATA[ select x.* from ( select z.*, rownum numbers from ( ]]> select from USR_AIR_LINE DEPARTURE_AIRPORT = #{airline.departureAirport} and ARRIVAL_AIRPORT=#{airline.arrivalAirport} and REL_DEPARTURE_AIRPORT = #{airline.relDepartureAirport} and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport} and POP_STATUS = #{airline.popStatus} and STATUS = #{airline.status} order by ${orderfield} ${page.sortOrder} <![CDATA[ ) z where rownum < ]]> #{page.to} <![CDATA[ ) x where x.numbers >= ]]> #{page.from} 注:上面的实例实现的是分页查询数据。我们可以发现使用Map来传递参数这种形式并不好,因为这样使得在接口中只有一个Map参数,其他人进行维护的时候并不清楚到底需要向这个Map里面传递什么参数进去 二 通过给参数添加@Param注解来解决问题: (1)给xxxMapper.java文件的方法中的参数添加@Param注解,这个注解中的值对应xml文件中使用到的参数名称: Airline selectEffectiveAirline( @Param("departureAirport") String departureAirport, @Param("arrivalAirport") String arrivalAirport, @Param("status") BigDecimal status); (2)此时xxxMapper.xml文件中对应的地方就可以正常使用在@Param注解中对应的值了: select from USR_AIR_LINE DEPARTURE_AIRPORT = #{departureAirport} and ARRIVAL_AIRPORT=#{arrivalAirport} and STATUS = #{status} 注:需要注意的是if条件判断中的参数和在SQL语句中写法是不一样的,if判断中的变量是没有添加#{ }的 下面在单独给大家介绍下通过Mybatis传递多个参数的两个方法 Map传递多个参数 parameterType 可以是别名或完全限定名,map或者java.util.Map,这两个都是可以的 select t.ID, t.title, t.content FROM blog t where t.title = #{h_title} and t.content =#{h_content} public void testSelectByMap() { SqlSession session = sqlSessionFactory.openSession(); Map param.put("h_title", "oracle"); param.put("h_content", "使用序列"); Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param); session.close(); System.out.println("blog title:"+blog.getTitle()); } 通过JavaBean传递多个参数 select t.ID, t.title, t.content from blog t wheret.title = #{title} and t.content =#{content} public void testSelectByBean() { SqlSession session = sqlSessionFactory.openSession(); Blog blog=new Blog(); blog.setTitle("oracle"); blog.setContent("使用序列!"); Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog); session.close(); System.out.println("new Blog ID:"+newBlog.getId()); }
value="#{'id':'ID','departureAirport':'DEPARTURE_AIRPORT','relDepartureAirport':'REL_DEPARTURE_AIRPORT','arrivalAirport':'ARRIVAL_AIRPORT','relArrivalAirport':'REL_ARRIVAL_AIRPORT','popStatus':'POP_STATUS','status':'STATUS','creator':'CREATOR','createTime':'CREATE_TIME'}" />
value="#{'ID':'id','DEPARTURE_AIRPORT':'departureAirport','REL_DEPARTURE_AIRPORT':'relDepartureAirport','ARRIVAL_AIRPORT':'arrivalAirport','REL_ARRIVAL_AIRPORT':'relArrivalAirport','POP_STATUS':'popStatus','STATUS':'status','CREATOR':'creator','CREATE_TIME':'createTime'}" /> <![CDATA[ select x.* from ( select z.*, rownum numbers from ( ]]> select from USR_AIR_LINE DEPARTURE_AIRPORT = #{airline.departureAirport} and ARRIVAL_AIRPORT=#{airline.arrivalAirport} and REL_DEPARTURE_AIRPORT = #{airline.relDepartureAirport} and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport} and POP_STATUS = #{airline.popStatus} and STATUS = #{airline.status} order by ${orderfield} ${page.sortOrder} <![CDATA[ ) z where rownum < ]]> #{page.to} <![CDATA[ ) x where x.numbers >= ]]> #{page.from} 注:上面的实例实现的是分页查询数据。我们可以发现使用Map来传递参数这种形式并不好,因为这样使得在接口中只有一个Map参数,其他人进行维护的时候并不清楚到底需要向这个Map里面传递什么参数进去 二 通过给参数添加@Param注解来解决问题: (1)给xxxMapper.java文件的方法中的参数添加@Param注解,这个注解中的值对应xml文件中使用到的参数名称: Airline selectEffectiveAirline( @Param("departureAirport") String departureAirport, @Param("arrivalAirport") String arrivalAirport, @Param("status") BigDecimal status); (2)此时xxxMapper.xml文件中对应的地方就可以正常使用在@Param注解中对应的值了: select from USR_AIR_LINE DEPARTURE_AIRPORT = #{departureAirport} and ARRIVAL_AIRPORT=#{arrivalAirport} and STATUS = #{status} 注:需要注意的是if条件判断中的参数和在SQL语句中写法是不一样的,if判断中的变量是没有添加#{ }的 下面在单独给大家介绍下通过Mybatis传递多个参数的两个方法 Map传递多个参数 parameterType 可以是别名或完全限定名,map或者java.util.Map,这两个都是可以的 select t.ID, t.title, t.content FROM blog t where t.title = #{h_title} and t.content =#{h_content} public void testSelectByMap() { SqlSession session = sqlSessionFactory.openSession(); Map param.put("h_title", "oracle"); param.put("h_content", "使用序列"); Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param); session.close(); System.out.println("blog title:"+blog.getTitle()); } 通过JavaBean传递多个参数 select t.ID, t.title, t.content from blog t wheret.title = #{title} and t.content =#{content} public void testSelectByBean() { SqlSession session = sqlSessionFactory.openSession(); Blog blog=new Blog(); blog.setTitle("oracle"); blog.setContent("使用序列!"); Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog); session.close(); System.out.println("new Blog ID:"+newBlog.getId()); }
value="#{'ID':'id','DEPARTURE_AIRPORT':'departureAirport','REL_DEPARTURE_AIRPORT':'relDepartureAirport','ARRIVAL_AIRPORT':'arrivalAirport','REL_ARRIVAL_AIRPORT':'relArrivalAirport','POP_STATUS':'popStatus','STATUS':'status','CREATOR':'creator','CREATE_TIME':'createTime'}" />
<![CDATA[
select x.* from (
select z.*, rownum numbers from (
]]>
select
from
USR_AIR_LINE
DEPARTURE_AIRPORT = #{airline.departureAirport}
and ARRIVAL_AIRPORT=#{airline.arrivalAirport}
and REL_DEPARTURE_AIRPORT =
#{airline.relDepartureAirport}
and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport}
and POP_STATUS = #{airline.popStatus}
and STATUS = #{airline.status}
order by ${orderfield} ${page.sortOrder}
<![CDATA[ ) z where rownum < ]]>
#{page.to}
<![CDATA[ ) x where x.numbers >= ]]>
#{page.from}
注:上面的实例实现的是分页查询数据。我们可以发现使用Map来传递参数这种形式并不好,因为这样使得在接口中只有一个Map参数,其他人进行维护的时候并不清楚到底需要向这个Map里面传递什么参数进去
二 通过给参数添加@Param注解来解决问题:
(1)给xxxMapper.java文件的方法中的参数添加@Param注解,这个注解中的值对应xml文件中使用到的参数名称:
Airline selectEffectiveAirline(
@Param("departureAirport") String departureAirport,
@Param("arrivalAirport") String arrivalAirport,
@Param("status") BigDecimal status);
(2)此时xxxMapper.xml文件中对应的地方就可以正常使用在@Param注解中对应的值了:
select
from
USR_AIR_LINE
DEPARTURE_AIRPORT = #{departureAirport}
and ARRIVAL_AIRPORT=#{arrivalAirport}
and STATUS = #{status}
注:需要注意的是if条件判断中的参数和在SQL语句中写法是不一样的,if判断中的变量是没有添加#{ }的
下面在单独给大家介绍下通过Mybatis传递多个参数的两个方法
Map传递多个参数
parameterType 可以是别名或完全限定名,map或者java.util.Map,这两个都是可以的
select t.ID, t.title, t.content
FROM blog t
where t.title = #{h_title}
and t.content =#{h_content}
public void testSelectByMap() {
SqlSession session = sqlSessionFactory.openSession();
Map
param.put("h_title", "oracle");
param.put("h_content", "使用序列");
Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param);
session.close();
System.out.println("blog title:"+blog.getTitle());
}
通过JavaBean传递多个参数
select t.ID, t.title, t.content
from blog t
wheret.title = #{title}
and t.content =#{content}
public void testSelectByBean() {
SqlSession session = sqlSessionFactory.openSession();
Blog blog=new Blog();
blog.setTitle("oracle");
blog.setContent("使用序列!");
Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog);
session.close();
System.out.println("new Blog ID:"+newBlog.getId());
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~