SSM-MyBatis-10:Mybatis中SqlSession的getMapper()和簡單的工具類MyBatisUtils

來源:互聯網
上載者:User

標籤:body   nts   utils   on()   ssi   try   ace   package   trace   

 

 

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------

 

 

getMapper的作用,擷取到介面,直接通過點的方式調用方法,以免直接手打的方式寫錯方法名,(強型別的方式)

例子:

 

IBookDAO mapper = session.getMapper(IBookDAO.class);            List<Book> list = mapper.findAllBook();

 

那個工具類,主要是擷取SqlSession對象,寫法

 

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 class MyBatisUtils {    static String path = "mybatis-config.xml";    static InputStream e;    static SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();    static SqlSessionFactory factory;    static {        try {            e = Resources.getResourceAsStream(path);            factory = builder.build(e);        } catch (IOException e1) {            e1.printStackTrace();        }    }    public static SqlSession getSession(){        return factory.openSession();    }}

 

他這個方法怎麼使用呢?

如下:

 

SqlSession session= MyBatisUtils.getSession();

 

SSM-MyBatis-10:Mybatis中SqlSession的getMapper()和簡單的工具類MyBatisUtils

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.