"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