Fuzzy query of Dynamic SQL

Source: Internet
Author: User
Tags getmessage

There are three ways to study fuzzy queries:

DAO layer
//Can be used List<user> wherelike01 (String user_name);  //Forget list<user> wherelike02 (map<string, object> Map);  //recommended using list<user> wherelike03 (String user_name)
Test class

@Test public void where statement test () {sqlsession sqlsession = null;            try {sqlsession = Mybatisutils.getsqlsession ();        Sqlsession.getmapper (Userdao.class). WHERELIKE01 ("song");            } catch (Exception e) {logger.debug (E.getmessage ());        throw new RuntimeException (E.getmessage ());        } finally {mybatisutils.closesqlsession (sqlsession); }} @Test public void where statement test 02$() {sqlsession sqlsession = null;            try {map<string, object> Map = new hashmap<string, object> ();            Map.put ("Account", "song");            sqlsession = Mybatisutils.getsqlsession ();        Sqlsession.getmapper (Userdao.class). WHERELIKE02 (map);            } catch (Exception e) {logger.debug (E.getmessage ());        throw new RuntimeException (E.getmessage ());        } finally {mybatisutils.closesqlsession (sqlsession); }} @Testpublic void where statement test 03Recommended() {sqlsession sqlsession = null;    try {sqlsession = Mybatisutils.getsqlsession ();    Sqlsession.getmapper (Userdao.class). WHERELIKE03 ("song");      } catch (Exception e) {logger.debug (E.getmessage ());    throw new RuntimeException (E.getmessage ());    } finally {mybatisutils.closesqlsession (sqlsession); }}
In mapper
<mapper namespace= "Com.shxt.dao.UserDao" >       <select id= "wherelike01" parametertype= "string" resulttype= "Com.shxt.model.User" >         Select            *        from            User        WHERE            user_name like #{user_name}       </ select>        <select id= "wherelike02" parametertype= "map" resulttype= "Com.shxt.model.User" >           Select                *            From                USER            WHERE                user_name like ' ${user_name}% '       </select>         <select id= " Wherelike03 "parametertype=" string "resulttype=" Com.shxt.model.User ">           Select                *            from                User            WHERE                user_name like CONCAT(#{user_name}, '% ')       </select></mapper>

  

Fuzzy query of Dynamic SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.