After the previous article, I think the main understanding of Mybatis topic is enough, but I think there is still a little blurry about the use of Mybatis. Personally, I think that mastering the Mybatis framework requires understanding three files. The first is the Mybatis-comfig.xml file to be discussed later, and the Mapper.xml, as well as the Mapper class we defined. After understanding these three things, and then having the basis of SQL and Java, whether it is using XML-based methods or Java-based Configuration methods, it will be much simpler.
Without further ado, let’s understand the first important file together: the Mybatis-config.xml file.
First, let’s take a look at a blank complete Mybatis-config.xml file (this name can be operated freely, you must remember that this is the Mybatis configuration file)
You can directly create a blank xml file, then go to the official website to copy a header file and paste it in.
<?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> </configuration
The above is the basic Mybatis-config.xml file. We need to add the configuration we need during development. Here is a basic configuration file that adds it. Through this:
<?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> <typeAliases> <typeAlias alias="Student" type="com.mybatis3.domain.Student" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="admin" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis3/mappers/StudentMapper.xml" /> </mappers> </configuration>
The tags we can see from the above content include the following, but what do these mean?
<configuration>: The information declared in the tag is configuration information
<typeAliases>: Declare the information in this tag as aliases
<typealias>: If you declare the object to use alias (full path) annotation with Java, you can use @Alias annotation to declare
<environments>: Declare the environment variable in this tag, default represents the default environment variable, and an environment represents a JDBC connection database. If there are many databases, we need to use different environment variables
<environment>: Declare environment variables
<transactionManager>: Declare the transaction manager its type (type) is: JDBC (jdbc-based transaction) and MANAGED (managed transaction)
<dataSource>: Declare data source. The types of data source are NOPOOLED, POOLED, and JIDN. If the data volume is small, use ONPOOLED. POOLED is generally used in the testing and development process. JIDN is used in actual operation.
<property>: Some properties of jdbc connection
<mappers>: Declare the Mapper classes we define, or associations
<mapper>: Declare the path to the mapper
What is mentioned above are the basic tags to be used when using Mybatis. You must master them. You may not understand the use of the environments above. How to obtain an environment. Let’s take a look at an example:
InputStream in = Resource.getResourceAsInputSteam("mybatis.config"); //Define the default environment defaultSqlSessionFactory = new SqlSessionFactoryBuilder().build(in); //Other environment otherSqlSessionFactory = new SqlSessionFactoryBuilder().build(in, "environment ID");The defaultSqlSessionFactory above must be declared by itself (note)
So what other tags are there besides the tags above? Let's take a look at other commonly used tags below
<Settings>: Declare some global variables
<properties>: Declare the key and value of the property file, but if the .properties file is used, it will be overwritten.
<typeHandlers>: Customize our incoming parameter type processor, need extends BaseTypeHandler <custom response type>
Finally, let’s take a look at Mybatis default setting configuration:
<setting name="cacheEnabled" value="true" />//Whether to use cache<setting name="lazyLoadingEnabled" value="true" />//Whether to be lazy<setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="false" /> <setting name="autoMappingBehavior" value="PARTIAL" /> <setting name="defaultExecutorType" value="SIMPLE" /> <setting name="defaultStatementTimeout" value="25000" /> <setting name="safeRowBoundsEnabled" value="false" /> <setting name="mapUnderscoreToCamelCase" value="false" /> [java] view plain copy<setting name="localCacheScope" value="SESSION" /> <setting name="jdbcTypeForNull" value="OTHER" /> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
Summarize
The above is a detailed analysis of the config.xml configuration file in Mybatis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!