Link database based on MyBatis framework

Source: Internet
Author: User
Tags access database

Based on the MyBatis framework

Features: Give MyBatis frame semi-finished products stand on Giants shoulders connect Access database, encapsulate as tool class easy to call connection reduce code redundancy increase efficiency support multithreading concurrent access

Public class Batisutil {

Private Static Final threadlocal<sqlsession> THL = new threadlocal<sqlsession> ();

  Private Static Sqlsessionfactorybuilder Builder =null;

  Private Static Reader Reader =null;

Static read code block mybatils core configuration file

Static {

  Try {

1 Builder

    Builder = new sqlsessionfactorybuilder ();

2 Factory

    Reader = Resources. Getresourceasreader ("Mybatis-config.xml");

} catch (Exception e) {

E.printstacktrace ();

}

}

Public Static Sqlsession getsession () {

sqlsession session =null;

  Try {

session=thl. get ();

    if (session==null) {

Sqlsessionfactory factory = builder. Build (reader);

3 Getting session

Session = Factory.opensession ();

    THL. Set (session);

}

} catch (Exception e) {

E.printstacktrace ();

    Throw New RuntimeException (e);

}

  return session;

}

Public Static void Release (sqlsession session) {

I f (session!=null) {

Session.close ();

    thl. Remove ();

}

}

linking other databases requires only a more Change the corresponding configuration file can not need to re-modify the source file

For example: MySQL

URL: "Jdbc:mysql://localhost:3306/userdb"

Dirver:com.mysql.jdbc.Driver

SQL Server 2008

URL: "jdbc:sqlserver://localhost:1433"

Driver:com.microsoft.jdbc.sqlserver.SQLServerDriver

Link database based on MyBatis framework

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.