Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢的用法
PS:ibatis3如何傳遞多個(gè)參數(shù)有兩個(gè)方法:一種是使用java.Map,另一種是使用JavaBean。
當(dāng)只向xxxMapper.xml文件中傳遞一個(gè)參數(shù)時(shí),可以簡(jiǎn)單的用“_parameter”來(lái)接收xxxMapper.java傳遞進(jìn)來(lái)的參數(shù),并代入查詢,比如說(shuō)這樣:
(1)xxxMapper.java文件中這樣定義:
List<String> selectAllAirportCode(Boolean mapping);
(2)這時(shí)在對(duì)應(yīng)的xxxMapper.xml文件中可以使用“_parameter”來(lái)接收這個(gè)參數(shù):
<select id="selectAllAirportCode" resultType="java.lang.String" parameterType="java.lang.Boolean"> select DEPARTURE_AIRPORT from USR_AIR_LINE union select ARRIVAL_AIRPORT from USR_AIR_LINE <if test="_parameter == true"> union select REL_DEPARTURE_AIRPORT from USR_AIR_LINE union select REL_ARRIVAL_AIRPORT from USR_AIR_LINE </if> </select>
但是,如果在xxxMapper.java文件中傳遞進(jìn)來(lái)多個(gè)參數(shù),就不能使用上面這種形式來(lái)接收參數(shù),這時(shí)可以有兩種方案來(lái)解決這個(gè)問(wèn)題:
一 向xml文件中傳遞進(jìn)去一個(gè)Map<String, Object>集合,然后xml文件中就可以正常使用Map集合中的各個(gè)參數(shù)了。
具體實(shí)例如下:
(1)xxxMapper.java文件中這樣定義:
List<Airline> findAll(Map<String, Object> parms);
(2)在用到上面定義的具體實(shí)現(xiàn)類(lèi)中給Map傳參:
public List<Airline> findAll(PageInfo page,Airline airline) { HashMap<String,Object> params = new HashMap<String,Object>(); params.put("page", page); params.put("airline", airline); return airlineMapper.findAll(params); }
(3)此時(shí)對(duì)應(yīng)的xxxMapper.xml文件使用“java.util.Map”來(lái)接收這個(gè)Map集合:
<sql id="sqlfileders"> <bind name="fileders" 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'}" /> <bind name="javapropertys" 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'}" /> </sql> <select id="findAll" resultMap="BaseResultMap" parameterType="java.util.Map"> <![CDATA[ select x.* from ( select z.*, rownum numbers from ( ]]> select <include refid="Base_Column_List" /> from USR_AIR_LINE <where> <if test="airline.departureAirport != null"> DEPARTURE_AIRPORT = #{airline.departureAirport} </if> <if test="airline.arrivalAirport != null"> and ARRIVAL_AIRPORT=#{airline.arrivalAirport} </if> <if test="airline.relDepartureAirport != null"> and REL_DEPARTURE_AIRPORT = #{airline.relDepartureAirport} </if> <if test="airline.relArrivalAirport != null"> and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport} </if> <if test="airline.popStatus != null"> and POP_STATUS = #{airline.popStatus} </if> <if test="airline.status != null"> and STATUS = #{airline.status} </if> </where> <if test="page.sortName != null"> <include refid="sqlfileders" /> <bind name="orderfield" value="#this.fileders[page.sortName]" /> order by ${orderfield} ${page.sortOrder} </if> <![CDATA[ ) z where rownum < ]]> #{page.to} <![CDATA[ ) x where x.numbers >= ]]> #{page.from} </select>
注:上面的實(shí)例實(shí)現(xiàn)的是分頁(yè)查詢數(shù)據(jù)。我們可以發(fā)現(xiàn)使用Map來(lái)傳遞參數(shù)這種形式并不好,因?yàn)檫@樣使得在接口中只有一個(gè)Map參數(shù),其他人進(jìn)行維護(hù)的時(shí)候并不清楚到底需要向這個(gè)Map里面?zhèn)鬟f什么參數(shù)進(jìn)去
二 通過(guò)給參數(shù)添加@Param注解來(lái)解決問(wèn)題:
(1)給xxxMapper.java文件的方法中的參數(shù)添加@Param注解,這個(gè)注解中的值對(duì)應(yīng)xml文件中使用到的參數(shù)名稱(chēng):
Airline selectEffectiveAirline( @Param("departureAirport") String departureAirport, @Param("arrivalAirport") String arrivalAirport, @Param("status") BigDecimal status);
(2)此時(shí)xxxMapper.xml文件中對(duì)應(yīng)的地方就可以正常使用在@Param注解中對(duì)應(yīng)的值了:
<select id="selectEffectiveAirline" resultMap="BaseResultMap" parameterType="java.util.Map"> select <include refid="Base_Column_List" /> from USR_AIR_LINE <where> <if test="departureAirport != null"> DEPARTURE_AIRPORT = #{departureAirport} </if> <if test="arrivalAirport != null"> and ARRIVAL_AIRPORT=#{arrivalAirport} </if> <if test="status != null"> and STATUS = #{status} </if> </where> </select>
注:需要注意的是if條件判斷中的參數(shù)和在SQL語(yǔ)句中寫(xiě)法是不一樣的,if判斷中的變量是沒(méi)有添加#{ }的
下面在單獨(dú)給大家介紹下通過(guò)Mybatis傳遞多個(gè)參數(shù)的兩個(gè)方法
Map傳遞多個(gè)參數(shù)
parameterType 可以是別名或完全限定名,map或者java.util.Map,這兩個(gè)都是可以的
<select id="selectBlogByMap" parameterType="map" resultType="Blog"> select t.ID, t.title, t.content FROM blog t where t.title = #{h_title} and t.content =#{h_content} </select> public void testSelectByMap() { SqlSession session = sqlSessionFactory.openSession(); Map<String, Object> param=new HashMap<String, Object>(); 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()); }
通過(guò)JavaBean傳遞多個(gè)參數(shù)
<select id="selectBlogByBean" parameterType="Blog" resultType="Blog"> select t.ID, t.title, t.content from blog t wheret.title = #{title} and t.content =#{content} </select> 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()); }
相關(guān)文章
Java inputstream和outputstream使用詳解
這篇文章主要介紹了Java inputstream和outputstream使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java中反射的"暴破"機(jī)制(SetAccessible方法)詳解
這篇文章主要為大家詳細(xì)介紹了Java中反射的"暴破"機(jī)制,以及如何利用這一機(jī)制實(shí)現(xiàn)訪問(wèn)非公有屬性,方法,和構(gòu)造器,文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08jcrop 網(wǎng)頁(yè)截圖工具(插件)開(kāi)發(fā)
今天給大家介紹一下一個(gè)web 中經(jīng)常會(huì)用到的截圖(如:頭像等)工具,需要的朋友可以了解下2012-11-11Java進(jìn)程內(nèi)緩存框架EhCache詳解
這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-12-12如何解決Idea斷點(diǎn)調(diào)試亂跳的問(wèn)題
這篇文章主要介紹了如何解決Idea斷點(diǎn)調(diào)試亂跳的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11