10_mybatis Developing DAO method--mapper Proxy implementation

Source: Internet
Author: User

"Engineering (several key red boxes)"

"Usermapper.xml"

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "com. Higgin.Mybatis.mapper.UserMapper">    <SelectID= "Finduserbyid"ParameterType= "int"Resulttype= "com. Higgin.Mybatis.po.User ">SELECT * from USER WHERE Id=#{id}</Select> </Mapper>

"Usermapper.java"

 Package com. Higgin.Mybatis.mapper; Import com. Higgin.Mybatis.po.User;  Public Interface usermapper {    // query user information by ID public    finduserbyid(int  throws  Exception;}

"Sqlmapperconfig.xml"

Never configure the Usermapper.xml but forget to load!

    <!--Load Map File -    <mappers>        <MapperResource= "Sqlmap/user.xml"/>                  <mapper  resource= "Mapper/usermapper.xml"   /> <!-- here, don't forget to load-      </mappers>

"Usermappertest.java"

 Packagecom. Higgin.Mybatis.mapper.test;Import Staticorg.junit.assert.*;ImportJava.io.InputStream;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;Importcom. Higgin.Mybatis.mapper.UserMapper;Importcom. Higgin.Mybatis.po.User; Public classUsermappertest {Privatesqlsessionfactory sqlsessionfactory; @Before Public voidSetUp ()throwsException {//Create Sqlsessionfactory//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //get configuration file streamInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile informationsqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestfinduserbyid ()throwsException {sqlsession sqlsession =sqlsessionfactory.opensession ();   // Create a Usermapper object, MyBatis automatically generate Mapper proxy object   usermapper usermapper =sqlsession. Getmapper (usermapper. class                );       //Tuneusing the Usermapper methodUser user = Usermapper.finduserbyid (5);    System.out.println (User.tostring ()); }}

"Run Results"

10_mybatis Developing DAO method--mapper Proxy implementation

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.