typeAliases以及mappers

typeAliases

我们来观察一下CarMapper.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">
<mapper namespace="mapper.CarMapper">

    <select id="selectByType" resultType="Car">
        select
            car_num as carNum,
            brand,
            guide_price as guidePrice,
            produce_time as produceTime,
            car_type as carType
        from t_car
        where brand like CONCAT('%', #{brand}, '%')
    </select>

    <select id="selectAll" resultType="Car">
        select
            car_num as carNum,
            brand,
            guide_price as guidePrice,
            produce_time as produceTime,
            car_type as carType
        from t_car
    </select>

</mapper>

resultType属性用来指定查询结果集的封装类型,这个名字太长,可以起别名吗?

可以。 在mybatis-config.xml文件中使用typeAliases标签来起别名,包括两种方式:

第一种方式:typeAlias

<typeAliases>
  <typeAlias type="pojo.Car" alias="Car"/>
</typeAliases>
  • 首先要注意typeAliases标签的放置位置:

    • 1.放在environments标签的前面

    • 2.放在properties的后面

  • typeAliases标签中的typeAlias可以写多个。

  • typeAlias:

    • type属性:指定给哪个类起别名

    • alias属性:别名。

      • alias属性不是必须的,如果缺省的话,type属性指定的类型名的简类名作为别名。

      • alias是大小写不敏感的。也就是说假设alias="Car",再用的时候,可以CAR,也可以car,也可以Car。

第二种方式:package

 如果一个包下的类太多,每个类都要起别名,会导致typeAlias标签配置较多,所以mybatis用提供package的配置方式,只需要指定包名(只需要!!!),该包下的所有类都自动起别名,别名就是简类名。并且别名不区分大小写。

Mybatis核心配置文件:

<?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>
    <typeAliases>
        <package name="pojo" />
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/testmybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="gege5211314"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper class="mapper.CarMapper"/>
    </mappers>
</configuration>

在sql的映射文件如下: 

<?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="mapper.CarMapper">

    <select id="selectByType" resultType="Car">
        select
            car_num as carNum,
            brand,
            guide_price as guidePrice,
            produce_time as produceTime,
            car_type as carType
        from t_car
        where brand like CONCAT('%', #{brand}, '%')
    </select>

    <select id="selectAll" resultType="Car">
        select
            car_num as carNum,
            brand,
            guide_price as guidePrice,
            produce_time as produceTime,
            car_type as carType
        from t_car
    </select>

</mapper>

mappers

SQL映射文件的配置方式包括四种:

  • resource:从类路径中加载

  • url:从指定的全限定资源路径中加载

  • class:使用映射器接口实现类的完全限定类名

  • package:将包内的映射器接口实现全部注册为映射器

 

resource

这种方式是从类路径中加载配置文件,所以这种方式要求SQL映射文件必须放在resources目录下或其子目录下。

 <mappers>
        <mapper resource="carMapper.XML"/>
 </mappers>

url

这种方式显然使用了绝对路径的方式,这种配置对SQL映射文件存放的位置没有要求,随意。但是移植性差

<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  <mapper url="file:///var/mappers/BlogMapper.xml"/>
  <mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>

class

如果使用这种方式必须满足以下条件:

  • SQL映射文件和mapper接口放在同一个目录下。

    • 在resources目录下新建:com/powernode/mybatis/mapper【这里千万要注意:不能这样新建 com.powernode.mybatis.dao

    • 将CarMapper.xml文件移动到mapper目录下

    • 修改mybatis-config.xml文件

      <mappers>
        <mapper class="com.powernode.mybatis.mapper.CarMapper"/>
      </mappers>
  • SQL映射文件的名字也必须和mapper接口名一致。e77ebc906fed4742ac793cfee43a491f.png

package

果class较多,可以使用这种package的方式,但前提条件和上一种方式一样。

<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
  <package name="com.powernode.mybatis.mapper"/>
</mappers>

插入数据时获取自动生成的主键

在关系型数据库(如MySQL、PostgreSQL、SQL Server等)中,当表的某个列(通常是主键)设置为自动递增(AUTO_INCREMENTSERIAL),每次插入新数据时,数据库会自动生成一个唯一的值作为该行的主键。获取自动生成的主键,是指在执行插入操作后,我们希望程序能拿到数据库生成的这个主键值,用于后续的逻辑处理,比如:

  • 将这个主键作为外键插入其他表。
  • 返回给客户端显示插入结果。
  • 更新与这个主键相关的记录。

场景示例

  1. 插入用户信息后,获取该用户的ID用于后续操作。
  2. 订单系统中插入新订单后,获取订单号进行物流或支付的后续操作。

总结

通过配置 <insert> 标签中的 useGeneratedKeys="true"keyProperty="id",MyBatis 能够在插入数据后,自动将数据库生成的主键值回填到 Java 对象中。这在开发中非常常用,尤其是在需要管理复杂主键关联关系或立即使用生成主键的场景中。

xml中的sql语句:

<insert id="insertUseGeneratedKeys" useGeneratedKeys="true" keyProperty="id">
  insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
  • 不使用 useGeneratedKeys:数据库生成了主键,但 Java 对象的 id 还是 null,可能会影响后续逻辑。
  • 使用 useGeneratedKeys:MyBatis 自动获取主键并赋值给对象的 id,简化了代码逻辑。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值