mybatis study notes 2


Preface

This article will explain the configuration file of mybatis in more detail


One, Maven dependency

Mybatis required dependencies:

		<dependency>
	            <groupId>org.mybatis</groupId>
	            <artifactId>mybatis</artifactId>
	            <version>3.4.5</version>
	    </dependency>	

mysql required dependencies:

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

Two, SqlMapConfig.xml (main configuration file)

basic configuration

  1. The most basic configuration of using mybatis mainly includes two parts: environment configuration and registration mapping file configuration. The environment configuration mainly fills in some attributes needed to connect to the database.
  2. The mappers tag configuration is to map your sql statement xml configuration file.

The xml file can be configured using mapper's source attribute

<mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml"></mapper>
</mappers>

Use annotations instead of xml files can be configured using mapper class attributes

<mappers> 
	<mapper class="com.itheima.dao.IUserDao"/>
</mappers>

Or use the package tag

<mappers>
    <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
        <!-- package标签用于指定dao接口所在的包,当指定完成后就不需要再写mapper -->
        <package name="com.itheima.dao"/>
    </mappers>

The complete configuration is 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="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务 -->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property>
                <property name="username" value="root"></property>
                <property name="password" value="1234"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件的位置 -->
    <mappers>
    <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
        <!-- package标签用于指定dao接口所在的包,当指定完成后就不需要再写mapper -->
        <package name="com.itheima.dao"/>
    </mappers>
</configuration>

Additional configuration

Configure alias

When the alias is configured, the resultType can be directly written instead of the fully qualified class name when configuring the resultType in the mappers configuration file.
Insert picture description here
For example, after configuring the alias here, you only need to write user instead of com.itheima.domain.User
and the alias is not case-sensitive, you can write user or UsEr

Specific placement:

<typeAliases>
    <!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
    <!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写 -->
        <package name="com.itheima.domain"/>
        
</typeAliases>

Import property files for configuration

  • In addition to directly using the property tag to configure the database connection information in mybatis, it can also be configured using the properties tag.
  • Import the configuration file through the resource attribute (or url attribute) of the properties tag, and then use ${} for the value of the properties tag.

Specific placement:

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

Or url way

    <properties url="file:///D:/project/day02_eesy_02mybatisDao/src/main/resources/jdbcConfig.properties"></properties>

Value method:

<!--配置环境-->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务 -->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>

The contents of the jdbcConfig.properties file:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis
jdbc.username=root
jdbc.password=1234

It is important to note here that the property name of the value of ${} must correspond to the property name of the jdbcConfig.properties file, otherwise it cannot be obtained.

to sum up

Full integrated version configuration:

<?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>
    <!-- 可以在标签内部配置连接数据库的信息,也可以通过属性引用外部配置文件信息
        resource属性:常用的
               用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下。
        url属性:
               要求按照Url的写法来写地址
               协议 主机 端口 URI
    -->

    <properties resource="jdbcConfig.properties">
<!--    <properties url="file:///D:/project/day02_eesy_02mybatisDao/src/main/resources/jdbcConfig.properties">-->

<!--        <property name="driver" value="com.mysql.jdbc.Driver"></property>-->
<!--        <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property>-->
<!--        <property name="username" value="root"></property>-->
<!--        <property name="password" value="0000"></property>-->
    </properties>
    <!-- 使用typeAliases配置别名,它只能配置domain中类的别名
        type:实体类全限定类名
        alias:属性指定别名
        使用了别名后不再区分大小写
    -->
    <typeAliases>
    <!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
    <!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写 -->
        <package name="com.itheima.domain"/>
        
    </typeAliases>
    <!--配置环境-->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务 -->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件的位置 -->
    <mappers>
    <!--<mapper resource="com/itheima/dao/IUserDao.xml"></mapper>-->
        <!-- package标签用于指定dao接口所在的包,当指定完成后就不需要再写mapper -->
        <package name="com.itheima.dao"/>
    </mappers>
</configuration>

Guess you like

Origin blog.csdn.net/u013456390/article/details/112867895