springboot+mybatis整合的两种方法

前言

springboot+mybatis整合主要体现在Mapper层,关于controller和service层都是没区别的。

注解

@Component
@Mapper
public interface LearnMapper {

    @Insert("insert into learn_resource(author,title,url) values(#{author},#{title},#{url})")
    int add(LearnResource learnResource);

    @Update("update learn_resource set author=#{author},title=#{title},url=#{url} where id = #{id}")
    int update(LearnResource learnResource);

    @Select("select * from learn_resource where id = #{id}")
    @Results(id = "learnMap", value = {
            @Result(column = "id", property = "id", javaType = Long.class),
            @Result(property = "author", column = "author", javaType = String.class),
            @Result(property = "title", column = "title", javaType = String.class)
    })
    LearnResource queryLearnResouceById(@Param("id") Long id);
}

xml

①、在application.properties文件中配置如下

#指定bean所在包
mybatis.type-aliases-package=com.lw.study.springboot.bean
#指定映射文件
mybatis.mapperLocations=classpath:mapper/*.xml

指定Mapper接口

@Mapper
public interface LearnMapper {
    int add(LearnResouce learnResouce);
    int update(LearnResouce learnResouce);
    int deleteByIds(String[] ids);
    LearnResouce queryLearnResouceById(Long id);
    public List<LearnResouce> queryLearnResouceList(Map<String, Object> params);
}

②如下配置文件就和普通mapper文件类似了

<?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="com.lw.study.springboot.mapper.LearnMapper">
  <resultMap id="baseResultMap" type="com.lw.study.springboot.mapper.bean.LearnResouce">
    <id column="id" property="id" jdbcType="BIGINT"  />
    <result column="author" property="author" jdbcType="VARCHAR"/>
    <result column="title" property="title" jdbcType="VARCHAR"/>
    <result column="url" property="url" jdbcType="VARCHAR"/>
  </resultMap>

  <sql id="baseColumnList" >
    id, author, title,url
  </sql>

  <select id="queryLearnResouceList" resultMap="baseResultMap" parameterType="java.util.HashMap">
    select
    <include refid="baseColumnList" />
    from learn_resource
    <where>
      1 = 1
      <if test="author!= null and author !=''">
        AND author like CONCAT(CONCAT('%',#{author,jdbcType=VARCHAR}),'%')
      </if>
      <if test="title != null and title !=''">
        AND title like  CONCAT(CONCAT('%',#{title,jdbcType=VARCHAR}),'%')
      </if>

    </where>
  </select>

  <select id="queryLearnResouceById"  resultMap="baseResultMap" parameterType="java.lang.Long">
    SELECT
    <include refid="baseColumnList" />
    FROM learn_resource
    WHERE id = #{id}
  </select>

  <insert id="add" parameterType="com.dudu.domain.LearnResouce" >
    INSERT INTO learn_resource (author, title,url) VALUES (#{author}, #{title}, #{url})
  </insert>

  <update id="update" parameterType="com.dudu.domain.LearnResouce" >
    UPDATE learn_resource SET author = #{author},title = #{title},url = #{url} WHERE id = #{id}
  </update>

  <delete id="deleteByIds" parameterType="java.lang.String" >
    DELETE FROM learn_resource WHERE id in
    <foreach item="idItem" collection="array" open="(" separator="," close=")">
      #{idItem}
    </foreach>
  </delete>
</mapper>

总结

对比两种方式,注解更加简单,个人还是喜欢xml一点,可能个人习惯吧。

参考:http://tengj.top/2017/04/23/springboot9/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。