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:
<mappers>
<mapper resource= "Com/tiantian/mybatis/model/blogmapper.xml"/>
</mappers>
This path is the path package name + file name of your XML mapping file.
URL form:
<mappers>
<mapper url= "File:///var/mappers/BlogMapper.xml"/>
</mappers>
This path corresponds to a file on the network, note the file://prefix + path + filename
Class Form:
<mappers>
<mapper class= "Org.mybatis.builder.BlogMapper"/>
</mappers>
This class is actually an interface that writes the full name of the interface.
Package format:
<mappers>
<package name= "Org.mybatis.builder"/>
</mappers>
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