mybatis xml配置文件要点说明

时间:2023-03-08 21:47:19
mybatis xml配置文件要点说明
  • mapper映射方式:

1 一一具体列举的方式

2扫描package

如:

<mappers>

<!-- 告知映射文件方式1,一个一个的配置

<mapper resource="com/cy/mybatis/mapper/UserMapper.xml"/>-->

<!-- 告知映射文件方式2,自动扫描包内的Mapper接口与配置文件 -->

<package name="com/cy/mybatis/mapper"/>

</mappers>

typeAliases       定义javabean的别名,通过别名来映射结果
    http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases
 
  • namespace

mybatis的mapper中,namespace与DAO的包名对应,select id与包名中的的函数对应

  • lresultmap
<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>

resultmap定义返回类型,result的id名称指定具体的resultmap

select语句中的返回结果result使用resultmap的id名来映射返回结果

id 和 result 都映射一个单独列的值到简单数据类型(字符 串,整型,双精度浮点数,日期等)的单独属性或字段。 id映射的是数据库中表的ID,而result映射数据库表中的column

property是select语句中对应的对象名

  • 一个基本的spring mybatis控制台应用项目文件结构如下:

model

model interface

modelDAO mybatis xml文件定义

mybatis config mapper xml文件

spring xml配置

  • 资源

http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases

http://www.mybatis.org/spring/zh/mappers.html