Some considerations in the MyBatis configuration

Source: Internet
Author: User

Recently in the study of MyBatis, online access to information, and according to other people's samples to test, there will always be some mistakes, here the configuration process some considerations to comb.

First, guide package (developed with Eclipse)

1, if you create a normal project, you need to create a new folder in the project directory (generally Lib), and then need to manually guide the package, the specific action is: Select the package right--build path-add to Build Path. The previous folder cannot be deleted because it actually loads the paths of these packages.

2, if you create a new Web project, only need to put the relevant package into the WEB-INF/LIB, it will automatically guide the package, the same LIB package can not be deleted.

Ii.. xml file Configuration

1, the configuration file name can be customized, the placement of the path can also be customized, but if you are not placed in the SRC root path, for example, placed in the src/config/ Mybatis.xml, when creating Sqlsessionfactory instances, Resources.getresourceasreader ("Config/mybatis.xml") Cannot add "/" before config, If placed under the SRC root path, write the file name directly here.

2, configuration files in the configuration items are sequential, in order: Properties,settings,typealiases,typehandlers,objectfactory,objectwrapperfactory,plugins , environments,databaseidprovider,mappers, that option is not configured, its location is empty, but the order must follow, otherwise it will error.

3, the Properties option, you can import the external to the properties end of the configuration file. The connection information of the database is usually placed in the configuration file, when the configuration DataSource, value= "${driver}", this form, here driver corresponds to the configuration file Driver = Com.mysql.jdbc.Driver

4, in the configuration of mapper, there are several ways, respectively, resource, URL, class, package

Resource form:

<mapper resource= "Com/tiantian/mybatis/model/blogmapper.xml"/>

This path is the path package name + file name of your XML mapping file.

URL form:


<mapper url= "File:///var/mappers/BlogMapper.xml"/>


This path corresponds to a file on the network, note the file://prefix + path + filename

Class Form:


<mapper class= "Org.mybatis.builder.BlogMapper"/>


This class is actually an interface that writes the full name of the interface.

Package format:


<package name= "Org.mybatis.builder"/>


5, the actual project uses the most is interface-oriented programming, that is, an interface corresponding to an XML map file, if the interface and mapping file corresponding to it? The answer is the XML mapping file in the mapper namespace= "Com.mybatis.inter.IUserOperation", this namespace must be the full name of the interface, cannot be an alias, a simple name, otherwise the runtime will error. In general, we will write the interface name and the mapping file name as the same, and under the same package, so feel namespace is the full name of the XML file.


Some considerations in the MyBatis configuration

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: 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.