Ssm-mybatis-10:mybatis sqlsession Getmapper () and Simple tool class Mybatisutils

Source: Internet
Author: User

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

Getmapper function, get to the interface, directly through the point of call method, so as not to directly hand-hit way to write the wrong method name, (strong type of way)

Example:

Ibookdao mapper = Session.getmapper (Ibookdao.  Class);            List<Book> list = Mapper.findallbook ();

The tool class, the main thing is to get sqlsession object, the wording

Package Cn.dawn.util;import Org.apache.ibatis.io.resources;import org.apache.ibatis.session.sqlsession;import Org.apache.ibatis.session.sqlsessionfactory;import Org.apache.ibatis.session.sqlsessionfactorybuilder;import Java.io.ioexception;import Java.io.InputStream;/** * Created by Dawn on 2018/2/24.*/ Public classMybatisutils {StaticString Path ="Mybatis-config.xml"; StaticInputStream E; StaticSqlsessionfactorybuilder Builder =NewSqlsessionfactorybuilder (); StaticSqlsessionfactory Factory; Static {        Try{e=resources.getresourceasstream (path); Factory=Builder.build (e); } Catch(IOException E1) {e1.printstacktrace (); }    }     Public Staticsqlsession getsession () {returnfactory.opensession (); }}

How does he use this method?

As follows:

Sqlsession session= mybatisutils.getsession ();

Ssm-mybatis-10:mybatis sqlsession Getmapper () and Simple tool class Mybatisutils

Related Article

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.