spring集成mybatis总结

  1. 添加依赖
    添加mybatis-spring会将mybatis代码无缝整合到spring,引入mybatis-spring的jar包<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>x.x.x</version> </dependency>
  2. sessionfactory
    在spring的上下文中定义sessionfactory<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" />
    <property name="mapperlocation"><list></list></ property >
    </bean>
    这里还需一个datasource,一个mapperlocation。datasource是配置数据源,mapperLocation是配置用到的mybatis的mapper文件路径
  3. dao层
    dao可以不写实现类,只写一个接口。配置如下:
    <bean id="smsDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.myyule.room.dao.RoomDao" />
    <property name="sqlSessionFactory" ref="openfireSqlSessionFactory" />
    </bean>
  4. 事务
    配置事务管理,可使用spring的声明式事务管理
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager=” transactionManager”>
    使用@Transactional注解可以在类上面也可以在方法上面,在类上面代表这个类的所有方法都需要事务。

  5. 动态sql
    mybais的强大特性之一就是动态sql

    • if
      <select id="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>
    • choose、when、otherwise
      <select id="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>
    • trim
      <insert id="insert" parameterType="ActivityAffix" useGeneratedKeys="true" keyProperty="affixId">
      insert into myyule_activity_affix
      <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="affixId != null" >
      affix_id,
      </if>
      <if test="activityId != null" >
      activity_id,
      </if>
      <if test="type != null" >
      type,
      </if>
      <if test="createTime != null" >
      create_time,
      </if>
      <if test="value != null" >
      value,
      </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="affixId != null" >
      #{affixId,jdbcType=BIGINT},
      </if>
      <if test="activityId != null" >
      #{activityId,jdbcType=BIGINT},
      </if>
      <if test="type != null" >
      #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
      #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="value != null" >
      #{value,jdbcType=LONGVARCHAR},
      </if>
      </trim>
      </insert>
    • foreach
and song_id in
                <foreach item="item" index="index" collection="songIds"
                    open="(" separator="," close=")">
                    #{item}
                </foreach>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值