mybatis和spring整合

typeAliasesPackage自动配置别名

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动配置别名 -->
        <property name="typeAliasesPackage" value="com.m" />
        <!-- <property name="typeAliasesSuperType" value="com.m.entity.Book"/> -->
        <!-- mybatis mapper配置文件 -->
        <property name="mapperLocations" value="classpath:com/m/mappers/*.xml" />
</bean>

当你设置这个 ,那么在Mybatis的Mapper文件里面就可以直接写对应的类名 而不用写全路径名了

设置前:parameterMap=“com.m.City”

设置后:

设置后:

<update id="updateCity" parameterMap="City">
   update
      city
   set
   <if test="provinceId!=null">
      province_id = #{provinceId},
   </if>
   <if test="cityName!=null">
      city_name = #{cityName},
   </if>
   <if test="description!=null">
      description = #{description}
   </if>
   where
      id = #{id}
</update>

————————————————

版权声明:本文为CSDN博主「lostPontifex」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_42528204/article/details/105050097

文章目录

概述

本文小结

概述

mapper.xml中的resultType中经常会用到一些自定义POJO,你可以用完全限定名来指定这些POJO的引用,例如

<select id="selectByStudentSelective" resultType="cn.wideth.entity.domain.Student">
     SELECT T.* FROM student T
</select>

我们需要查询student表中所有学生的信息,定义了一个实体类student来映射表中所有的学生信息,把类的权限定类名放在resultType标签中即可,mybatis底层在返回数据库信息的时候,会根据Class.forName(“cn.wideth.entity.domain.Student”)去生成对象实例;

如果我们想在resultType标签中仅仅使用类名Student可不可以呢

   <select id="selectByStudentSelective" resultType="Student">
        SELECT T.* FROM student T
    </select>

这样也是可以的,只是需要在yml配置文件中提前进行配置

mybatis:

  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: cn.wideth.entity

这样也是可以的,springboot在启动的时候,会加载cn.wideth.entity包以及子包下所有的实体类,进行初始化操作。但是不同子包下面不可以存在同名的实体类。

子包下面存在同名实体类,结果报错了。

本文小结

尽量去使用实体类的全限定类名,这样会减少程序出现问题。

————————————————

版权声明:本文为CSDN博主「wh柒八九」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_31960623/article/details/119081928

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐