MyBatis XML配置

多数情况下企业还是喜欢用XML来执行SQL。这也是MyBatis推荐的方式。更灵活,且可以执行逻辑判断。
首先在 application.properties 文件中,添加配置

  1. mybatis.mapper-locations=classpath:com/lzx/comment/dao/*.xml

这个配置用于指定MyBatis XML 文件路径。一般和DAO的包路径一致。当配置好,系统启动,MyBatis框架会自动扫描工程下的指定路径,并完成这个路径下所有XML文件的加载(*会匹配所有,正则表达式)。
多个路径用**,**隔开,如:

mybatis.mapper-locations=classpath:com/youkeda/dao/*.xml,classpath:com/youkeda/comment/dao/*.xml

一般我们都会把代码之外的文件放在resource文件目录下,所以上面配置对应的路径是:

src/main/resources/com/youkeda/comment/dao/

MyBatis XML Mapper

约定:一个DAO类对应一个DAO.xml文件(有些企业把DAO命名为Mapper)。
UserDAO.java——> UserDAO.xml
路径:

src/main/resources/com/lzx/comment/dao/UserDAO.xml

1.头信息

创建xml文件,头部固定格式:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

2.mapper根节点

添加mapper节点

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzx.comment.dao.UserDAO">


</mapper>

namespace 命名空间,对应DAO接口全称。

3.resultMap

resultMap用于处理表和DO对象的属性映射,确保每个字段都可以匹配。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzx.comment.dao.UserDAO">

  <resultMap id="userResultMap" type="com.lzx.comment.dataobject.UserDO">
    <id column="id" property="id"/>
    <result column="user_name" property="userName"/>
  </resultMap>

</mapper>
  • id

唯一标示,一般命名规则为xxxResultMap

  • type

对应DO类的完整路径。

resultMap子节点

  • id

设置数据库主键字段信息,column对应的是表的字段名称,property对应的是DO类属性名称。

  • result

设置数据库其他字段信息……
完整文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzx.comment.dao.UserDAO">

  <resultMap id="userResultMap" type="com.lzx.comment.dataobject.UserDO">
    <id column="id" property="id"/>
    <result column="user_name" property="userName"/>
    <result column="pwd" property="pwd"/>
    <result column="nick_name" property="nickName"/>
    <result column="avatar" property="avatar"/>
    <result column="gmt_created" property="gmtCreated"/>
    <result column="gmt_modified" property="gmtModified"/>
  </resultMap>

</mapper>

有了resultMap之前使用SQL语句 的 as 映射属性就不用啦

resultType和resultMap对比

MyBatis XML Insert语句

为了和之前的注解区别,我们加了一个add方法

@Mapper
public interface UserDAO {
    int add(UserDO userDO);
}

我们打开UserDAO.xml文件添加insert语句。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzx.comment.dao.UserDAO">

    <resultMap id="userResultMap" type="com.lzx.comment.dataobject.UserDO">
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="pwd" property="pwd"/>
        <result column="nick_name" property="nickName"/>
        <result column="avatar" property="avatar"/>
        <result column="gmt_created" property="gmtCreated"/>
        <result column="gmt_modified" property="gmtModified"/>
    </resultMap>
    <insert id="add" parameterType="com.lzx.comment.dataobject.UserDO">
    INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified)
    VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())
  </insert>
</mapper>

**insert**节点两个属性

  • id

同DAO类的方法名,同一个XML内要唯一。id=”add” 与 UserDAO.add一致

  • parameterType

用于传递参数类型,一般是和UserDAO.add(UserDo userdo)方法的参数类型一致。
想要或者插入的主键id值,还需要配置 useGeneratedKeys、keyProperty

<insert id="add" parameterType="com.youkeda.comment.dataobject.UserDO" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified)
    VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())
</insert>

MyBatis XML Update/Delete语句(和Insert类似)

  • 先去掉对应注解

    <update id="update" parameterType="com.youkeda.comment.dataobject.UserDO">
          update user set nick_name=#{nickName},gmt_modified=now() where id=#{id}
      </update>
    
    <delete id="delete">
          delete from user where id=#{id}
      </delete>
    

    注意这里delete节点没有设置 parameterType属性。因为

    int delete(@Param("id") long id);
    

    这个delete方法参数有@param注解组成,默认情况下MyBatis会把这类数据当成Map数据来传递,而parameterType默认类型就是Map,可以省略不写。

    MyBatis XML Select语句

    <select id="findAll" resultMap="userResultMap">
          select * from user
      </select>
    
      <select id="findByUserName" resultMap="userResultMap">
          select * from user where user_name=#{userName} limit 1
      </select>
    

    总结

    基于XML模式开发顺序:

  1. 创建DO对象
  2. 创建DAO接口,配置@Mapper注解
  3. 创建XML文件,并完成resultMap配置(表字段——类属性映射)
  4. 创建DAO方法
  5. 创建对应XML语句