Optimized the configuration details in the MyBatis configuration file.

Source: Internet
Author: User

Optimized the configuration details in the MyBatis configuration file.

This article focuses on optimizing the configuration in the MyBatis configuration file.

1. The configuration for connecting to the database is separately stored in a properties File

Previously, we directly wrote the database connection configuration information in the conf. xml file of MyBatis, as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default = "development"> <environment id = "development"> <transactionManager type = "JDBC"/> <! -- Configure database connection information --> <dataSource type = "POOLED"> <property name = "driver" value = "com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306/mybatis "/> <property name =" username "value =" root "/> <property name =" password "value =" XDP "/> </dataSource> </ environment> </environments> </configuration>

In fact, we can write the database connection configuration information in a properties file, and then reference the properties file in the conf. xml file. The specific practices are as follows:

1. Create a New db. properties file in the src directory, as shown in:

In the db. properties file, write the database driver used to connect to the database. The connection URL, user name, and password are as follows:

1 driver = com. mysql. jdbc. Driver
2 url = jdbc: mysql: // localhost: 3306/mybatis
3 name = root
4 password = XDP

2. reference the db. properties file in the conf. xml file of MyBatis as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Reference the db. properties configuration file --> <properties resource = "db. properties"/> <! -- Development: development Mode work: working mode --> <environments default = "development"> <environment id = "development"> <transactionManager type = "JDBC"/> <! -- Configure database connection information --> <dataSource type = "POOLED"> <! -- The value property value references db. properties configuration file configuration value --> <property name = "driver" value = "$ {driver}"/> <property name = "url" value = "$ {url} "/> <property name =" username "value =" $ {name} "/> <property name =" password "value =" $ {password} "/> </dataSource> </environment> </environments> </configuration>

2. Define aliases for object classes to simplify SQL ing of references in xml files

Previously, when ing the reference object class in an SQL ing xml file, we need to write the full class name (package name + class name) of the object class, as shown below:

<! -- Create user --> <insert id = "addUser" parameterType = "me. gacl. domain. user "> insert into users (name, age) values (# {name}, # {age}) </insert>

ParameterType = "me. gacl. domain. user "the full class name of the entity class User I. gacl. domain. user, It is very troublesome to write such a long string every time, and we hope it can be abbreviated as below

<insert id="addUser2" parameterType="_User">  insert into users(name,age) values(#{name},#{age})</insert>

ParameterType = "_ User" is much easier to write. To achieve this effect, we need to go to conf. the object class = "me. gacl. domain. user "defines an individual name as" _ User ". The specific procedure is as follows:

Add the following configuration to the <configuration> </configuration> label in the conf. xml file:

<typeAliases>  <typeAlias type="me.gacl.domain.User" alias="_User"/></typeAliases>

In this way, it can be me. gacl. domain. the User class defines an individual name named _ User. Later _ User represents me. gacl. domain. user class. In this way, all SQL ing xml files need to reference me. gacl. domain. the User class can be replaced by _ User, which simplifies object class reference.

Besides <typeAlias type = "me. gacl. domain. user "alias =" _ User "/> in addition to setting aliases for an object class separately, you can also set aliases for all object classes in a package in the following way, as follows:

<! -- Configure the object class alias. The object class name is configured to replace the object class with the object class alias when the object class is referenced, --> <typeAliases> <! -- Configure an alias _ User for the object class me. gacl. domain. User --> <! -- <TypeAlias type = "me. gacl. domain. User" alias = "_ User"/> --> <! -- For me. gacl. all object classes under the domain package are configured with aliases. By default, MyBatis sets aliases by removing simple class names such as me after the class is in the package. gacl. domain. the alias of the User object class is set to User --> <package name = "me. gacl. domain "/> </typeAliases>

<Package name = "me. gacl. domain"/> indicates that the alias is set for all object classes under the package. MyBatis sets the alias by default by removing the simple class name after the package where the class is located. For example, the alias of the me. gacl. domain. User object class will be set to User.

Summary

The above is all the details about how to optimize the configuration in the MyBatis configuration file. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.