06mybatis_ returns a list collection based on the user's name Blur query

Source: Internet
Author: User

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

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.