mybatis框架——配置文件详解

我不是女神ヾ 2022-05-18 03:54 367阅读 0赞

一、全局配置文件

1、概览

全局配置文件(SqlMapConfig.xml)的配置内容和顺序如下(顺序不能乱):

  • Properties(属性)
  • Settings(全局参数设置)
  • typeAliases(类型别名)
  • typeHandlers(类型处理器)
  • objectFactory(对象工厂)
  • plugins(插件)
  • environments(环境信息集合)
  • environment(单个环境信息)
  • transactionManager(事物)
  • dataSource(数据源)
  • mappers(映射器)

2、常用配置

1.Properties

  1. 即配置加载properties文件,首先创建db.properties
  2. db.driver=com.mysql.jdbc.Driver
  3. db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
  4. db.username=root
  5. db.password=root

那么配置文件:

  1. <configuration>
  2. <!-- 加载java的配置文件或者声明属性信息 -->
  3. <properties resource="db.properties">
  4. </properties>
  5. <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
  6. <environments default="development">
  7. <environment id="development">
  8. <!-- 配置JDBC事务控制,由mybatis进行管理 -->
  9. <transactionManager type="JDBC"></transactionManager>
  10. <!-- 配置数据源,采用mybatis连接池 -->
  11. <dataSource type="POOLED">
  12. <property name="driver" value="${db.driver}" />
  13. <property name="url" value="${db.url}" />
  14. <property name="username" value="${db.username}" />
  15. <property name="password" value="${db.password}" />
  16. </dataSource>
  17. </environment>
  18. </environments>
  19. </configuration>

注意,如果:

  1. <properties resource="db.properties">
  2. <property name="db.username" value="123" />
  3. </properties>

还是会加载properties文件中的username,因为先加载配置内容,再加载properties文件,后者会覆盖前者。

parameterType的值会和properties的属性值发生冲突。

2.typeAliases

  1. po类进行别名的定义,其中mybatis支持的别名有:

























































































               别名                  原名
_byte byte
_long long 
_short short 
_int int 
_integer int 
_double double 
_float float 
_boolean boolean 
string  String 
byte Byte 
long Long 
short Short 
int Integer 
integer Integer 
double Double 
boolean Boolean 
float Float 
date Date 
decimal BigDecimal 
bigdecimal BigDecimal 

当然也可以自定义别名:

  1. <!-- 自定义别名 -->
  2. <typeAliases>
  3. <!-- 单个别名定义 -->
  4. <!-- <typeAlias type="com.itheima.mybatis.po.User" alias="user"/> -->
  5. <!-- 批量别名定义(推荐) -->
  6. <!-- package:指定包名称来为该包下的po类声明别名,默认的别名就是类名(首字母大小写都可) -->
  7. <package name="com.itheima.mybatis.po" />
  8. </typeAliases>

那么在映射文件中就可以使用user来替代其全限定名。

3.mappers

在引入映射文件时有如下几种格式:

  • :使用相对于类路径的资源【如:
  • :使用完全限定路径【如:
  • :使用mapper接口的全限定名【如:
  • (推荐):注册指定包下的所有映射文件【如:

注意:后两种方法要求mapper接口和mapper映射文件要名称相同,且放到同一个目录下。

二、映射文件

1、输入映射

1.简单类型

参考《mybatis框架——基本应用》中根据用户ID查询用户信息的映射文件

2.Pojo类型

参考入门中添加用户的映射文件

3.包装pojo类型

  1. 在综合查询时,可能会根据用户信息、商品信息、订单信息等作为条件进行查询,用户信息中的查询条件由:用户的名称和性别进行查询。需要先创建pojo的包装类:
  2. public class UserQueryVO {
  3. private User user;//用户信息
  4. //setget
  5. }

然后在映射文件中:

  1. <!-- 综合查询,查询用户列表 -->
  2. <select id="findUserList" parameterType="com.itheima.mybatis.po.UserQueryVO" resultType="user">
  3. SELECT * FROM user WHERE
  4. username LIKE '%${user.username}%'
  5. AND sex=#{user.sex}
  6. </select>

在Mapper接口中:

  1. public List<User> findUserList(UserQueryVO vo);

4.Map类型

  1. 同传递POJO对象一样,mapkey相当于pojo的属性。映射文件如下:
  2. <!-- 传递hashmap综合查询用户信息 -->
  3. <select id="findUserByHashmap" parameterType="hashmap" resultType="user">
  4. select * from user where id=#{id} and username like '%${username}%'
  5. </select>

2、输出映射

1.resultType

  1. 使用resultType进行结果映射时,需要查询出的列名和映射的对象的属性名一致,才能映射成功。如果查询的列名和对象的属性名全部不一致,那么映射的对象为空;如果查询的列名和对象的属性名有一个一致,那么映射的对象不为空,但是只有映射正确那一个属性才有值。

说明:如果查询的sql的列名有别名,那么这个别名就是和属性映射的列名。

1.简单类型

  1. 注意,对简单类型的结果映射也是有要求的,查询的列必须是一列,才能映射为简单类型。综合查询时,需要根据综合查询的添加查询用户的总数,首先映射文件:
  2. <select id="findUserCount" parameterType="com.itheima.mybatis.po.UserQueryVO" resultType="int">
  3. SELECT count(*) FROM user WHERE
  4. username LIKE '%${user.username}%'
  5. AND sex=#{user.sex}
  6. </select>

在Mapper接口中:

  1. //综合查询用户总数
  2. public int findUserCount(UserQueryVO vo);

2.Pojo对象和pojo列表

参考入门程序之根据用户ID查询用户信息和根据用户名称模糊查询用户列表

2.resultMap

  1. 使用resultMap进行结果映射时,不需要查询的列名和映射的属性名必须一致。但是需要声明一个resultMap,来对列名和属性名进行映射。如,对以下sql查询的结果集进行对象映射:
  2. Select id id_,username username_,sex sex_ from user where id = 1;

在映射文件中:

  1. <resultMap type="user" id="UserRstMap">
  2. <id column="id_" property="id" />
  3. <result column="username_" property="username" />
  4. <result column="sex_" property="sex" />
  5. </resultMap>
  6. <select id="findUserRstMap" parameterType="int" resultMap="UserRstMap">
  7. Select id id_,username username_,sex sex_ from user where id = #{id}
  8. </select>

说明:

id标签:专门为查询结果中唯一列映射

result标签:映射查询结果中的普通列

在Mapper接口中:

  1. public User findUserRstMap(int id);

3、动态sql

  1. mybatis中,它提供了一些动态sql标签,可以让程序员更快的进行mybatis的开发,这些动态sql可以通过sql的可重用性。常用的动态sql标签:if标签、where标签、sql片段、foreach标签。

1.If标签/where标签

  1. 在综合查询时,查询条件由用户来输入,用户名称可以为空,需要满足这种情况下的sql编写:
  2. <!-- 综合查询,查询用户列表 -->
  3. <select id="findUserList" parameterType="com.itheima.mybatis.po.UserQueryVO" resultType="user">
  4. SELECT * FROM user
  5. <where>
  6. <if test="user != null">
  7. <if test="user.username != null and user.username != ''">
  8. AND username LIKE '%${user.username}%'
  9. </if>
  10. <if test="user.sex != null and user.sex != ''">
  11. AND sex = #{user.sex}
  12. </if>
  13. </if>
  14. </where>
  15. </select>

解释:

  • where标签:默认去掉后面第一个AND,如果没有参数,则把自己干掉(还有一个set标签,去掉最后面的’,’号)
  • if标签:可以对输入的参数进行判断

  • test:指定判断表达式

除了可以将代码直接写在该处,还可以写在sql块中,然后调用:

  1. <sql id="whereClause">
  2. <if test="user != null">
  3. <if test="user.username != null and user.username != ''">
  4. AND username LIKE '%${user.username}%'
  5. </if>
  6. <if test="user.sex != null and user.sex != ''">
  7. AND sex = #{user.sex}
  8. </if>
  9. </if>
  10. </sql>
  11. <select id="findUserList" parameterType="com.itheima.mybatis.po.UserQueryVO"
  12. resultType="user">
  13. SELECT * FROM user
  14. <where>
  15. <!-- 引入sql片段 -->
  16. <include refid="whereClause" />
  17. </where>
  18. </select>

注意:sql片段内,最好不用将where和select关键字声明在内。

2.Foreach标签

  1. 可以循环传入参数值。综合查询时,会根据用户ID集合进行查询(批量删除),如下面SQL
  2. SELECT * FROM USER WHERE id IN (1,2,10)

首先修改包装pojo:

  1. public class UserQueryVO {
  2. private User user;//用户信息
  3. private List<Integer> idList;//ID集合
  4. }

然后在sql片段中需要拼凑出:AND id IN (#{id},#{id},#{id})

  1. <sql id="whereClause">
  2. <if test="idList != null">
  3. AND id IN
  4. <foreach collection="idList" item="id" open="(" close=")"
  5. separator=",">
  6. #{id}
  7. </foreach>
  8. </if>
  9. </sql>

解释:

  • collection:表示pojo中集合属性的属性名称
  • item:为遍历出的结果声明一个变量名称
  • open:遍历开始时,需要拼接的字符串
  • close:遍历结束时,需要拼接的字符串

  • separator:遍历中间需要拼接的连接符

如果直接传递的是List集合,则:

  1. <select id="findUsersByIdList" parameterType="java.util.List" resultType="user">
  2. SELECT * FROM USER
  3. <where>
  4. <if test="list != null and list.size > 0">
  5. <foreach collection="list" item="id" open="AND id IN (" close=")" separator=",">
  6. #{id}
  7. </foreach>
  8. </if>
  9. </where>
  10. </select>

发表评论

表情:
评论列表 (有 0 条评论,367人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis配置文件详解

    配置文件和映射文件还有挺多的属性我还没有讲的,现在就把它们一一补全 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为Mapped