Three, MyBatis series: Mapper mapping to read database records by Mapper mapping file

Source: Internet
Author: User

When you use MyBatis to fetch records from a database, you write only one mapping XML file. This is the strength of mybatis, which is said to save 95% more code than normal JDBC.
First to see how to obtain records, engineering configuration, please refer to the "one, MyBatis series: The First MyBatis project":

1. Mapper Configuration file contents
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en"3 "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">4 <Mappernamespace= "Test">5     <SelectID= "Getuserbyid"ParameterType= "int"Resulttype= "Map">6 SELECT * FROM user where id = #{value}7     </Select>8     <SelectID= "Getusers"Resulttype= "Map">9 SELECT * from userTen     </Select> One </Mapper>

Understand the node elements:

    • Mapper: The root node of the mapping file, all configurations are filled in;
    • Namespace: The namespace to which the mapping belongs;
    • Select: Represents the section used to read records;
      • ID: The unique identifier under the namespace;
      • ParameterType: The type of the parameter passed in the SQL statement, the types that can be received are simple types, pojo types, map types, and so on;
      • Resulttype: The return type of the statement can be a simple type, a pojo type, a map type, and so on;
      • SELECT * FROM User: A standard SQL statement written by a user to query for desired results;
      • #{value}: If the parameter type passed in is a simple type, then value can be written in any letter;

Note that ParameterType does not accept multiple parameters, and if more than one parameter is present, it needs to be encapsulated as a POJO type.

2. Write a unit test to read the data record
1 @Test2  Public voidGetuserstest ()throwsIOException {3InputStream stream = Resources.getresourceasstream ("Mybatisconfig.xml");4Sqlsessionfactory factory =NewSqlsessionfactorybuilder (). Build (stream);5Sqlsession sqlsession =factory.opensession ();6     Try {7         //reads a record and returns the MAP result type.8Map<object, object> user = Sqlsession.selectone ("Test.getuserbyid", 1);9 System.out.println (user);Ten          One         //reads more than one record, returning the list<map> result type. AList<map<object, object>> users = sqlsession.selectlist ("Test.getusers"); - System.out.println (users);  -}finally { the sqlsession.close (); -     } -}

To view the log output:

DEBUG [main]-Logging initialized using ' class Org.apache.ibatis.logging.log4j.Log4jImpl ' adapter.
DEBUG [main]-Pooleddatasource forcefully closed/removed all connections.
DEBUG [main]-Opening JDBC Connection
DEBUG [main]-Created connection 603650290.
DEBUG [main]-Setting autocommit to False on JDBC Connection [[email protected]]
DEBUG [main]-==> preparing:select * from user where id =?
DEBUG [main]-==> parameters:1 (Integer)
DEBUG [main]-<== total:1
{birthday=2014-07-10, address= Beijing, sex=2, id=1, username= Harry, attrs={"Parameter 1": "Parameter value 1"}}
DEBUG [main]-==> preparing:select * from user
DEBUG [main]-==> Parameters:
DEBUG [main]-<== total:6
[{birthday=2014-07-10, address= Beijing, sex=2, id=1, username= Harry, attrs={"Parameter 1": "Parameter value 1"}}, {birthday=2014-07-10, address= BEIJING, Sex=1, id=10, Username= Zhang San, attrs={"Parameter 1": "Parameter value 1"}}, ... "
DEBUG [main]-resetting autocommit to True on JDBC Connection [[email protected]]
DEBUG [main]-Closing JDBC Connection [[email protected]
DEBUG [main]-returned connection 603650290 to pool.

Three, MyBatis series: Mapper mapping to read database records by Mapper mapping file

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.