Introduction to mybatis configuration file_ Power node Java college sorting

When defining sqlsessionfactory, you need to specify the mybatis master configuration file:

XML code

The sub tags of configuration in the mybatis configuration file include: configuration | --- properties | --- settings | --- typealiases | --- typehandlers | --- objectfactory | --- plugins | --- environments | --- environment | --- | --- transactionmanager | -- -- -- -- -- --|__ dataSource |__ mappers

Properties property

Properties and Java Properties. Configure the resource specification of properties The path of properties, and then configure the name and value of property under the Properties tab The corresponding property value in the properties file.

XML code

Settings settings

This is an important step for mybatis to modify the details of the operation running process. The table below describes these settings, their meanings and default values.

For example:

XML code

Typealiases type aliases

Type aliases are short for Java types. It is simply associated with XML configuration, shorthand for verbose Java class names. For example:

XML code

With this configuration, "studententity" can be used anywhere instead of "com. Manager. Data. Model. Studententity".

For normal Java types, there are many built-in type aliases. They are case insensitive. Due to the overloaded names, pay attention to the special handling of native types.

Typehandlers type handles

Whether mybatis sets a parameter in the preprocessing statement or takes a value from the result set, the type processor is used to convert the obtained value into Java type in an appropriate way. The following table describes the default processor type.

You can override the type processor or create your own type processor to handle unsupported or nonstandard types. To do this, simply implement the typehandler interface (org. Mybatis. Type), then map the new type handler class to the Java type, and optionally a JDBC type. Then add this type handler to typehandlers.

The newly defined type processor will override the existing type processor that handles Java's string type properties, varchar parameters and results. Note that mybatis does not examine the database meta information to determine which type to use, so you must specify the field of varchar type in the parameter and result mapping to bind to the correct type processor. This is due to the fact that mybatis does not know the data type until the statement is executed.

Java code

Add the typehandler tag to typehandlers in the configuration file.

XML code

Objectfactory object factory

Each time mybatis creates a new instance of the result object, objectfactory is used. The default objectfactory is no different from using the constructor of the target class to create an instance. If there are mapped parameters, the constructor with parameters may also be used. If you override the default operation of objectfactory, you can inherit org apache. ibatis. reflection. factory. Defaultobjectfactory create your own.

The objectfactory interface is simple. It contains two creation methods, one is to handle the default constructor, and the other is to handle the constructor with parameters. Finally, the setproperties method can be used to configure the objectfactory. After initializing your objectfactory instance, the properties defined in the objectfactory element body will be passed to the setproperties method.

Java code

Add the objectfactory tag to the configuration file

XML code

4.6 plugins plug-in

Mybatis allows you to intercept calls executed by mapped statements at a certain point. By default, mybatis allows plug-ins to intercept method calls:

Executor(update,query,flushStatements,commit,rollback,getTransaction,close,isClosed)

ParameterHandler(getParameterObject,setParameters)

ResultSetHandler(handleResultSets,handleOutputParameters)

StatementHandler(prepare,parameterize,batch,update,query)

The details of the methods in these classes can be found by looking at the signature of each method, and their source code is available in the mybatis distribution package. You should understand the behavior of your override method, assuming you do more than monitor calls. If you try to modify or overwrite a given method, you may break the core of mybatis. This is a low-level class and method, and plug-ins should be used with caution. Using plug-ins is a very simple power they provide. Simply implement the interceptor interface to determine the specified signature you want to intercept.

Environments environment

Mybatis can configure multiple environments. This can help you map SQL to multiple databases, etc.

Mappers mapper

Here is the statement that tells mybatis where to find the mapped SQL. You can use the resource reference in the classpath, or use characters to enter the exact URL reference.

For example:

XML code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>