This paper is about a fuzzy query and then returns a list collection.
The case of this article is based on the previous article, so the main modification of two places can be.
1.user.xml
2.mybatis_first.java
The User.xml code is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--nanmespace: Name space. The function is to classify the SQL management, understand SAL separation Note: Using Mapper Proxy method, namespace has a special important role -<Mappernamespace= "Test"> <!--get user information by ID - <!--Configuring many SQL statements in a mapping file - <!--ID: Identifies the SQL in the mapping file; The SQL statement is encapsulated into the Mappedstatement object, so the ID is called the statement id;parmentertype: Specifies the type of the input parameter, and the int type #{} specified here represents a placeholder symbol; #{id}: Where the ID represents the input parameter, the parameter name is the ID, if the input parameter is a simple type, the parameter name in #{} can be arbitrary, can be value or other name Resulttype: The specified SQL output result of the mapped Java object type, Select Specifies that Resulttype represents a Java object that maps a single record; - <!--based on the user's name fuzzy query Resulttype refers to a single record of the type of Java mapped, regardless of whether the query is multiple or single - <SelectID= "Finduserbyusername"ParameterType= "Java.lang.String"Resulttype= "Cn.itcast.mybatis.po.User">SELECT * from user where username like '%${value}% '</Select> </Mapper>
The code for Mybatis_first.java is as follows:
PackageCn.itcast.mybatis.first;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.List;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportOrg.junit.Before;Importorg.junit.Test;ImportCn.itcast.mybatis.po.User; Public classMybatis_first {//Session Factory Privatesqlsessionfactory sqlsessionfactory; //these things are necessary, so put them in before.@Before Public voidCreatesqlsessionfactory ()throwsIOException {//configuration fileString resource= "Sqlmapconfig.xml"; InputStream InputStream=Resources.getresourceasstream (Resource); //use Sqlsessionfactorybuilder to create Sqlsessionfactory from the configuration file.sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); } @Test//querying users ' information by ID Public voidTestfindusebyid () {//DB Session InstanceSqlsession sqlsession=NULL; Try { //create a DB session instance sqlsession;sqlsession=sqlsessionfactory.opensession (); //User user=sqlsession.selectone ("Test.finduserbyid", ten);List<User> listuser=sqlsession.selectlist ("Test.finduserbyusername", "Xiao Ming"); System.out.println ("The number of employees that fuzzy queries get is" +listuser.size ()); //System.out.println (user);}Catch(Exception e) {e.printstacktrace (); } finally{ //If the sqlsession instance is created, then shut him off. if(sqlsession!=NULL) {sqlsession.close (); } } } }
The result of the operation is that the number of employees in the fuzzy query is 3
The result of the operation is correct.
06mybatis_ returns a list collection based on the user's name Blur query