美文网首页
持久层框架mybatis使用——(二)配置文件

持久层框架mybatis使用——(二)配置文件

作者: 我菠菜今天就是要为所欲为 | 来源:发表于2020-10-04 10:14 被阅读0次

mybatis框架中有三种配置文件,分别是:

  • 主配置文件:mybatis-config.xml主要对框架进行配置
  • 数据库配置文件:dbconfig.properties保存数据库连接信息
  • 持久层映射文件:xxxMapper.xml配置数据库查询逻辑

下面依次进行说明。

mybatis-config.xml

配置文件头

<?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">

头文件主要声明了配置文件遵循的xml文档版本号、字体格式、以及依赖的dtd规范地址。

那么dtd规范是什么呢?dtd简单点说就是规范了xml文档的结构,节点及节点中参数信息,方便验证文档的可用性。

configuration标签

configuration标签,表示其中的内容为我们对框架的配置。

properties标签

properties标签,表示我们需要引用的参数信息。

其中properties标签的参数resource表示我们要引用的配置文件。

比如我们要引入数据库配置文件dbconfig.properties

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

properties标签还有子标签property,用于我们自定义参数信息。比如我们定义自定义参数db.userName,并使用${db.userName}来使用这个参数。

<properties>
     <property name="db.userName" value="aaa"/>
</properties>

这里需要注意的是,如果我们自定义的参数和引用的配置文件中参数重复,那么自定义的参数将会被覆盖!!!

environments标签

environments标签是数据库环境标签,包含参数default用于指定默认使用的数据库环境。

子标签environment用于定义一个数据库环境,包含参数id,用于配置当前环境的id。

<!--配置数据库环境-->
<environments default="development">
    <environment id="development">
    </environment>
</environments>

transactionManager标签

transactionManager标签用于指定事务类型,java环境下为JDBC,该标签为environment的子标签。并且在spring框架整合中无需配置

<environment id="development">
    <transactionManager type="JDBC"/>
</environment>

dataSource标签

用于配置数据源信息,spring框架整合中会交给spring管理。该标签为environment的子标签。

<environment id="development">
    <dataSource type="POOLED">
        <!--引入数据库信息-->
        <property name="driver" value="${db.driverName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.userName}"/>
        <property name="password" value="${db.password}"/>
    </dataSource>
</environment>

mappers标签

configuration下的标签,用来引入指定数据表和实体类的对应关系及查询逻辑,包含

  • 引入映射文件方式mapper参数resource
  • 引入接口并自动生成映射方式mapper参数class
  • 指定包文件方式package
<configuration>
    <!--配置数据库映射文件-->
    <mappers>
        <mapper resource="mapper/UserInfo.xml"/>
        <mapper class="mapper/UserInfo.class"/>
        <package name="com.dao"/>
    </mappers>
</configuration>
  • 使用映射文件的方式需要自己实现dao层接口
  • 使用接口方式会自动寻找同名xml文件并实现接口
  • 使用包引入就是批量接口引入

需要注意的是,如果使用了包引入的方式,就不要使用class方式引入,否则会导致自动生成出错。

附上完整的配置文件示例

<?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>
    <!--引入数据库配置文件-->
    <properties resource="config/dbconfig.properties">
        <!--会被外边的配置文件覆盖-->
        <property name="db.userName" value=""/>
    </properties>
    <!--配置数据库环境-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--引入数据库信息-->
                <property name="driver" value="${db.driverName}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.userName}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--配置数据库映射文件-->
    <mappers>
        <mapper resource="mapper/UserInfo.xml"/>
        <package name="com.dao"/>
    </mappers>
</configuration>

dbconfig.properties

这个不用过多解释了,直接上示例

#创建数据库配置文件
db.driverName=org.sqlite.JDBC
db.url=jdbc:sqlite:src/main/resources/data/sql.db
db.userName=
db.password=

xxxMapper.xml

先看示例

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mapper.UserMapper">
    <select id="select_user" parameterType="int" resultType="com.beans.UserInfo">
    select * from userInfo where id= #{id}
    </select>
    <select id="getAllUser" resultType="com.beans.UserInfo">
    select * from userInfo
    </select>
    <!--<select id="getUserByName" parameterType="string" resultType="com.beans.UserInfo">
    select * from userInfo where userName like concat('%',#{userName},'%')
    </select>-->
    <select id="getUserByName" parameterType="string" resultType="com.beans.UserInfo">
    select * from userInfo where userName like #{userName}
    </select>

    <insert id="add_user" parameterType="com.beans.UserInfo">
    insert into userInfo (userName,password) values (#{userName},#{password} )
    </insert>

    <delete id="del_user" parameterType="int">
    delete from userInfo where id=#{id}
    </delete>

    <update id="update_user" parameterType="com.beans.UserInfo">
    update userInfo set userName=#{userName},password=#{password} where id=#{id}
    </update>
</mapper>

其中namespace标签,表示这个mapper.xml文件对应的接口的命名空间。

selectupdateinsertdelete标签表示操作的类型。在标签中可以直接写sql语句。

id一般对应接口中的方法名,需要唯一。

parameterType 参数类型,如果是引用类型,需要写全命名空间。

resultType返回值类型,如果是引用类型,需要写全命名空间。返回的如果是list,框架会自动包装。

以上,就是这几个配置文件的概述,详细配置会在后续详细操作中记录。

相关文章

网友评论

      本文标题:持久层框架mybatis使用——(二)配置文件

      本文链接:https://www.haomeiwen.com/subject/iyrwuktx.html