Maybtis自动生成Mapper文件

1.demo位置:

D:\12PROGRAMMER34\2kc-projectSets\kaytune32-selfStudyOfProfessionalCourse\002-MybatisAutoGeneratorMapperFile

2.better-mybatis-generator插件方式:

2.1 基本使用(不勾选生成example实体

https://jingyan.baidu.com/article/3052f5a1bacf31d6f21f863b.html

IDea安装better-mybatis-generator插件并连接好数据库,

右键点击要生成的表(可多选但建议选一个),右键后在菜单下拉列表点击mybatis-generate,

配置生成文件要放置的目录、主键、是否生成@Repository注解、是否添加别名,是否生成example实体[不建议勾选生成example实体,因为内部方法不便捷]等信息:

2.2 如果勾选生成example实体

MyBatis - MyBatis Generator 生成的Mapper接口以及Example的实例函数及详解_Xu小开的博客-CSDN博客

使用例子:

 CourseDao.java:

package com.kaytune.dao;

import com.kaytune.pojo.Course;
import com.kaytune.pojo.CourseExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CourseDao {
    /*按自定义条件计数【Example_Where_Clause】*/
    long countByExample(CourseExample example);

    /*按自定义条件删除【Example_Where_Clause】*/
    int deleteByExample(CourseExample example);

    //按主键删除
    int deleteByPrimaryKey(String cid);

    //插入数据为null的【也插入(返回值为ID】useGeneratedKeys="true,可参看写的CSDN文章)
    int insert(Course record);

    //插入数据为null的【不插入(返回值为ID】useGeneratedKeys="true,可参看写的CSDN文章)
    int insertSelective(Course record);

    //按自定义条件查询【Example_Where_Clause】
    List<Course> selectByExample(CourseExample example);

    //按主键查询
    Course selectByPrimaryKey(String cid);

    /*按自定义条件更新【会判断Course record属性参数是否为null,null的不更新,不为null的都更新包括主键】【Update_By_Example_Where_Clause】*/
    int updateByExampleSelective(@Param("record") Course record, @Param("example") CourseExample example);

    /*按自定义条件更新【不会判断Course record属性参数是否为null,null的也更新为null-全部更新包括主键】【Update_By_Example_Where_Clause】*/
    int updateByExample(@Param("record") Course record, @Param("example") CourseExample example);

    //按主键更新(不更新主键,会判断Course record非主属性参数是否为null,null的不更新)
    int updateByPrimaryKeySelective(Course record);

    //按主键更新(不更新主键,不会判断Course record非主属性参数是否为null,null的也更新为null)
    int updateByPrimaryKey(Course record);
}

CourseDao.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="com.kaytune.dao.CourseDao">
  <resultMap id="BaseResultMap" type="com.kaytune.pojo.Course">
    <id column="course_CId" jdbcType="VARCHAR" property="cid" />
    <result column="course_Cname" jdbcType="VARCHAR" property="cname" />
    <result column="course_TId" jdbcType="VARCHAR" property="tid" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    course.CId as course_CId, course.Cname as course_Cname, course.TId as course_TId
  </sql>
  <select id="selectByExample" parameterType="com.kaytune.pojo.CourseExample" resultMap="BaseResultMap">
    select
    <if test="distinct"><!--distinct是boolean类型的值-->
      distinct
    </if>
    <include refid="Base_Column_List" />
    from course course
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="limit != null">
      <if test="offset != null">
        limit ${offset}, ${limit}
      </if>
      <if test="offset == null">
        limit ${limit}
      </if>
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from course course
    where course.CId = #{cid,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from course
    where CId = #{cid,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.kaytune.pojo.CourseExample">
    delete from course course
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" keyColumn="CId" keyProperty="cid" parameterType="com.kaytune.pojo.Course" useGeneratedKeys="true">
    insert into course (Cname, TId)
    values (#{cname,jdbcType=VARCHAR}, #{tid,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="CId" keyProperty="cid" parameterType="com.kaytune.pojo.Course" useGeneratedKeys="true">
    insert into course
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="cname != null">
        Cname,
      </if>
      <if test="tid != null">
        TId,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="cname != null">
        #{cname,jdbcType=VARCHAR},
      </if>
      <if test="tid != null">
        #{tid,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.kaytune.pojo.CourseExample" resultType="java.lang.Long">
    select count(*) from course course
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update course course
    <set>
      <if test="record.cid != null">
        course.CId = #{record.cid,jdbcType=VARCHAR},
      </if>
      <if test="record.cname != null">
        course.Cname = #{record.cname,jdbcType=VARCHAR},
      </if>
      <if test="record.tid != null">
        course.TId = #{record.tid,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update course course
    set course.CId = #{record.cid,jdbcType=VARCHAR},
      course.Cname = #{record.cname,jdbcType=VARCHAR},
      course.TId = #{record.tid,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.kaytune.pojo.Course">
    update course
    <set>
      <if test="cname != null">
        Cname = #{cname,jdbcType=VARCHAR},
      </if>
      <if test="tid != null">
        TId = #{tid,jdbcType=VARCHAR},
      </if>
    </set>
    where CId = #{cid,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.kaytune.pojo.Course">
    update course
    set Cname = #{cname,jdbcType=VARCHAR},
      TId = #{tid,jdbcType=VARCHAR}
    where CId = #{cid,jdbcType=VARCHAR}
  </update>
</mapper>

 CourseDaoTest.java:

package com.kaytune.dao;

import com.kaytune.pojo.CourseExample;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.jupiter.api.Assertions.*;

@RunWith(SpringRunner.class)//作用在于,可以注入bean,否则空指针
@SpringBootTest
class CourseDaoTest {
    @Autowired
    CourseDao courseDao;

    @Test
    void countByExample() {
        CourseExample courseExample = new CourseExample();
        CourseExample.Criteria criteria = courseExample.createCriteria();
        criteria.andCidGreaterThan("02");//Cid>"02"
        criteria.andCidBetween("01","10");//
        criteria.andCidEqualTo("03");//Cid='03'
        //..
        long l = courseDao.countByExample(courseExample);
        System.out.println(l);
//==> Preparing: select count(*) from course course WHERE ( course.CId > ? and course.CId between ? and ? and course.CId = ? )
//==> Parameters: 02(String), 01(String), 10(String), 03(String)
//<== Columns: count(*)
//<== Row: 1
//<== Total: 1
//可以参看博客https://blog.csdn.net/xiaozhenzi66/article/details/81117458?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165033357516780269831379%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165033357516780269831379&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-81117458.142^v9^control,157^v4^control&utm_term=better-mybtis-generator%E6%8F%92%E4%BB%B6Example%E5%AE%9E%E4%BD%93%E7%9A%84%E4%BD%BF%E7%94%A8&spm=1018.2226.3001.4187
    }

    @Test
    void deleteByExample() {
    }

    @Test
    void deleteByPrimaryKey() {
    }

    @Test
    void insert() {
    }

    @Test
    void insertSelective() {
    }

    @Test
    void selectByExample() {
    }

    @Test
    void selectByPrimaryKey() {
    }

    @Test
    void updateByExampleSelective() {
    }

    @Test
    void updateByExample() {
    }

    @Test
    void updateByPrimaryKeySelective() {
    }

    @Test
    void updateByPrimaryKey() {
    }
}

 3.mybatis-generator插件方式:

pom文件《

<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <configuration>
    <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
    <overwrite>true</overwrite>
    <verbose>true</verbose>
  </configuration>
</plugin>

》这种插件方式在凯淳CouponDoctor工程中有实例,与下面文章中介绍的不太一样。

mybatis-generator详细配置及使用,爬坑记录 - 百度文库

需要添加mybatis-generator-core依赖,配置generatorConfig.xml文件,

使用maven的mybatis-generator.generate命令生成文件,这篇文章最后有介绍。

为防止文章丢失,截图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值