mybatis实战使用详解

使用配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
# xml路径
mybatis:
  mapper-locations: classpath:mapper/*.xml
#日志
logging:
  level:
    com.example.demo.dao: DEBUG
@MapperScan("com.example.demo.dao")//扫描指定包下接口

使用细节

一对一

<resultMap id="map" type="com.example.demo.entity.PlayTotal">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <!--1.直接手写-->
    <association property="studentList" javaType="com.example.demo.entity.Student">
      <id column="b_id" jdbcType="INTEGER" property="id" />
      <result column="b_name" jdbcType="VARCHAR" property="name" />
      <result column="b_user_id" jdbcType="INTEGER" property="userId" />
    </association>
    <!--2.前缀手写-->
    <association property="studentList" columnPrefix="b_" javaType="com.example.demo.entity.Student">
      <id column="id" jdbcType="INTEGER" property="id" />
      <result column="name" jdbcType="VARCHAR" property="name" />
      <result column="user_id" jdbcType="INTEGER" property="userId" />
    </association>
    <!--3.前缀并且其他地方引入-->
    <association property="studentList" columnPrefix="b_" resultMap="com.example.demo.dao.StudentBaseDao.BaseResultMap">
    </association>
</resultMap>

<select id="select" resultMap="map">
  select a.id, a.name,
  b.id as b_id, b.name as b_name,
  b.user_id as b_user_id
  from play a
  inner join student b
  on a.id = b.user_id
</select>

一对多(基本数据类型)

<resultMap id="map" type="com.example.demo.entity.PlayTotal">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <collection property="studentList" ofType="java.lang.String">
      <constructor>
        <arg column="b_name"/>
      </constructor>
    </collection>
  </resultMap>

  <select id="select" resultMap="map">
    select a.id, a.name,
    b.name as b_name
    from play a
    left join student b
    on a.id = b.user_id
  </select>

一对多

  <resultMap id="map" type="com.example.demo.entity.PlayTotal">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <!--2.前缀手写-->
    <collection property="studentList" columnPrefix="b_" ofType="com.example.demo.entity.Student">
      <id column="id" jdbcType="INTEGER" property="id" />
      <result column="name" jdbcType="VARCHAR" property="name" />
      <result column="user_id" jdbcType="INTEGER" property="userId" />
    </collection>
    <!--3.前缀其他地方引入-->
    <collection property="studentList" columnPrefix="b_" resultMap="com.example.demo.dao.StudentBaseDao.BaseResultMap">
    </collection>

  </resultMap>

  <select id="select" resultMap="map">
    select a.id, a.name,
    b.id as b_id, b.name as b_name,
    b.user_id as b_user_id
    from play a
    left join student b
    on a.id = b.user_id
  </select>

批量插入返回id

  • java代码让数据切割成指定大小,通过java8lombda表达式去增加
<!-- 引入相关依赖 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-collections4</artifactId>
	<version>4.4</version>
</dependency>
ListUtils.partition(list, 3).forEach(x->{
    playBaseDao.batchAdd(x);
});
  <insert id="batchAdd" useGeneratedKeys="true" parameterType="java.util.List" keyProperty="id">
    INSERT INTO play(name)
    VALUES
    <foreach collection="list" item="item" index="index" separator="," >
      (#{item.name,jdbcType=VARCHAR})
    </foreach>
  </insert>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值