02_ Fuzzy Query

Source: Internet
Author: User

"Engineering and database with 01"

"User.xml"

The user is queried according to the username.

<SelectID= "Finduserbyname"ParameterType= "Java.lang.String"Resulttype= "com. Higgin.Mybatis.po.User ">SELECT * from USER WHERE username like #{value}
</Select><SelectID= "Finduserbyname2"ParameterType= "Java.lang.String"Resulttype= "com. Higgin.Mybatis.po.User ">SELECT * from USER WHERE username like '%${value}% '</Select>

1. According to the user name fuzzy query user information, may return more than one
2.resultType: Specifies the type of Java object to which a single record is mapped
3.${}: Represents the concatenation of the SQL string, which will accept the contents of the parameter without any adornment stitching in the SQL.
4. Using ${} to stitch strings, causing SQL injection, not recommended
5.${value}: Accepts the contents of the input parameter, if the incoming type is a simple type, ${} can only use the value

"Mybatistest.java"

@Test Public voidTestfinduserbyname ()throwsioexception{//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //Get configuration fileInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile informationSqlsessionfactory sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); //get sqlsession through the factorySqlsession sqlsession=sqlsessionfactory.opensession (); //manipulating databases with sqlsession//first parameter: The statement ID in the silver snake file equals: namespace+ "." ID of the +statement//second parameter: Specifies the parameter of the ParameterType type of the skin to be changed in the mapping fileList<user> Userlist=sqlsession.selectlist (" test.finduserbyname2", "Xiaoming "); Not recommended!!! To cause SQL injection for(User user:userlist) {System.out.println (user.tostring ()); }                //Freeing ResourcesSqlsession.close (); }

"The results of the operation are as follows"

"Mybatistest.java"

@Test public    void Testfinduserbyname () throws ioexception{        //mybatis configuration file        String resource= " Sqlmapconfig.xml ";                Get configuration file        inputstream inputstream=resources.getresourceasstream (Resource);                Create a session factory, pass in MyBatis profile information        sqlsessionfactory sqlsessionfactory=new sqlsessionfactorybuilder (). Build ( InputStream);        Through the factory get sqlsession        sqlsession sqlsession=sqlsessionfactory.opensession ();//through the sqlsession operation database// First parameter: The statement ID in the silver snake file equals: namespace+ "." +statement ID//second parameter: Specifies the parameter list<user> userlist=sqlsession.selectlist of the parametertype type of the skin in the new mapping file (" Test.finduserbyname ","% xiaoming"); Recommended for (User user:userlist) {System.out.println (user.tostring ());}//Release resource sqlsession.close ();} 

"Run Results"

02_ Fuzzy Query

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.