Summary of basic configuration tags for MyBaties

  • 2021-10-11 18:49:20
  • OfStack

Basic Configuration Label for MyBaties

1-Two approaches introduced by the global configuration file (xxx. properties)

resource: Introducing resources under the ClassPath url: Introducing resources under a network path or disk path

<properties resource="dbconfig.properties"></properties>

2-settings contains settings

name: Configuration Items

value: Attribute value


<settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

3-typeAliases: Alias handler, which aliases java type

type: Specifies the full class name of the type to be aliased; The default alias is the class name lowercase

alias: Specify a new alias


<typeAlias type="com.atguigu.mybatis.bean.Employee" alias="emp"/> 
3.1 Alias all classes under a package

package: Bulk aliasing all classes under a package

name: Specify the package name (give a default alias (class name lowercase) for every 1 class of the current package and all descendant packages below)


<package name="com.atguigu.mybatis.bean"/>
3.2 Using annotations @Alias Specify a new type for a class

@Alias("emp")
public class Employee {
    ...code...
}

4-Configure multiple MyBatis environments

enviroments: The configured environment is written in it, and default specifies the name of this environment environment: Configure 1 specific environment information with id only 1 identification and transactionManager transaction manager id: Only 1 ID transactionManager: Transaction manager with attribute type type: Transaction Manager Type JDBC MANAGED Custom Transaction Manager dataSource: Data source type: Data source type UNPOOLED POOLED JNDI custom

<environments default="dev_mysql">
   <environment id="dev_mysql">
      <transactionManager type="JDBC"></transactionManager>
      <dataSource type="POOLED">
         <property name="driver" value="${jdbc.driver}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
      </dataSource>
   </environment>

   <environment id="dev_oracle">
      <transactionManager type="JDBC" />
      <dataSource type="POOLED">
         <property name="driver" value="${orcl.driver}" />
         <property name="url" value="${orcl.url}" />
         <property name="username" value="${orcl.username}" />
         <property name="password" value="${orcl.password}" />
      </dataSource>
   </environment>
</environments>

5-databaseIdProvider: Multidatabase support

databaseIdProvider: supports multi-database, its type is DB_VENDOR, and its function is to get the identity of database vendor (driving getDatabaseProductName ()), and mybatis can execute different sql according to the identity of database vendor; property: Name the database name: value:

<databaseIdProvider type="DB_VENDOR">
   <!--  Alias different database vendors  -->
   <property name="MySQL" value="mysql"/>
   <property name="Oracle" value="oracle"/>
   <property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>

Finally, write the query-time statement in mapper. xml and state what database is being used


<mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper">
	<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
       select * from tbl_employee where id = #{id}
    </select>
    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee"
       databaseId="mysql">
       select * from tbl_employee where id = #{id}
    </select>
    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee"
       databaseId="oracle">
       select EMPLOYEE_ID id,LAST_NAME    lastName,EMAIL email 
       from employees where EMPLOYEE_ID=#{id}
    </select>
</mapper>

6-mappers Registers the sql file into the global configuration file

6.1 Register Profile: resource: Reference the sql mapping file under the ClassPath for example: mybatis/mapper/EmployeeMapper. xml url: Reference the sql mapping file under the network path or disk path for example: file://var/mappers/AuthorMapper. xml 6.2 Registration interface:

sql mapping file, mapping file name must be the same as the interface, and placed with the interface in the same directory;


<mapper resource="mybatis/mapper/EmployeeMapper.xml"/>

There is no sql mapping file, all sql are written on the interface with annotations, and then registered in mappers;


public interface EmployeeMapperAnnotation {
	@Select("select * from tbl_employee where id=#{id}")
	public Employee getEmpById(Integer id);
}

<settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
0 6.3 Bulk registration:

Essentially, if the package name is the same, whether the files inside src or outside src will be stored in the same folder in the actual stored procedure


<settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
1

Related articles: