MyBatis Environment Construction

Source: Internet
Author: User

Section I. MyBatis the environment to build

March 1, 2016 Tuesday

13:01

1. Find the jar you need

It is important to note that for all jars to be Bulid the path operation

2. According to the Help document complete

Www.github.com find the official Help document for the corresponding MyBatis

The construction process is as follows:

A. Referencing A package

B. Need to establish a core profile, document type

<! DOCTYPE Configuration

Public "-//mybatis.org//dtd Config 3.0//en"

"Http://mybatis.org/dtd/mybatis-3-config.dtd" >

I. Configuring information for a data source

A. Information about connecting to a database

B. Using the JDBC transaction manager

Database on the persistence layer framework by default, transactional commits are turned off and require you to commit manually

About the transaction, just for the operation to change the database cud operation

II. Configure information about the mapping file to load: class name Mapper.xml

C. A new persistent class [entity Bean] is created, and a mapping file for the custom SQL statement corresponding to the persisted class is created, and in general the file is kept in a directory with your persistence class

D. Conduct the test

Step is fixed

The mode for CUD operation is as follows

public static void Main (string[] args) {

String Path = "Mybatis-config.xml";

Sqlsession sqlsession = null;

try {

1. Read the core configuration file

InputStream InputStream = resources.getresourceasstream (path);

2. Create Sqlsessionfactory--Connection

Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);

3. Create Sqlsession--Statement

sqlsession = Sqlsessionfactory.opensession ();

Because of the framework, all transactions are committed manually

4. Commit a transaction

Sqlsession.commit ();

} catch (IOException e) {

E.printstacktrace ();

5. Transaction rollback

if (sqlsession!=null)

Sqlsession.rollback ();

}finally {

6. Need to close sqlsession, save resources

if (sqlsession!=null) sqlsession.close ();

}

}

The following patterns are used for query operations:

public static void Main (string[] args) {

String Path = "Mybatis-config.xml";

Sqlsession sqlsession = null;

try {

1. Read the core configuration file

InputStream InputStream = resources.getresourceasstream (path);

2. Create Sqlsessionfactory--Connection

Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);

3. Create Sqlsession--Statement

sqlsession = Sqlsessionfactory.opensession ();

} catch (IOException e) {

E.printstacktrace ();

}finally {

4. Need to close sqlsession, save resources

if (sqlsession!=null) sqlsession.close ();

}

}

we have a problem with the query ?

Your result set cannot be copied to the corresponding property when the properties in the persisted class are not consistent with the queried fields in the database

1. The first solution is to query the required fields and alias the fields to properties in the persisted class

For a value corresponding to the namespace in the mapping file, it is recommended that the full path of the class: columns such as Com.shxt.model.User

MyBatis Environment Construction

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.