1、如何使用BaseMapper进行数据库的操作。

2、使用BaseMapper进行插入实体时如何让UUID的主键自动生成。

Student实体类,其中id属性主键为UUID

package com.huixiaoer.ant.api.model.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; public class Student {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.id
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
@TableId(type= IdType.UUID)
private String id; /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.user_name
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
private String userName; /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.age
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
private Integer age; /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.phone
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
private String phone; /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public Student(String id, String userName, Integer age, String phone) {
this.id = id;
this.userName = userName;
this.age = age;
this.phone = phone;
} /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public Student() {
super();
} /**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column student.id
*
* @return the value of student.id
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public String getId() {
return id;
} /**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column student.id
*
* @param id the value for student.id
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public void setId(String id) {
this.id = id == null ? null : id.trim();
} /**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column student.user_name
*
* @return the value of student.user_name
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public String getUserName() {
return userName;
} /**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column student.user_name
*
* @param userName the value for student.user_name
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
} /**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column student.age
*
* @return the value of student.age
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public Integer getAge() {
return age;
} /**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column student.age
*
* @param age the value for student.age
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public void setAge(Integer age) {
this.age = age;
} /**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column student.phone
*
* @return the value of student.phone
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public String getPhone() {
return phone;
} /**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column student.phone
*
* @param phone the value for student.phone
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
}

StudnetVI实体类,用户从页面接收参数

package com.huixiaoer.ant.api.model.vi;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel(value = "student对象",description = "学生对象student")
public class StudentVI { /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.user_name
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
@ApiModelProperty(value="学生姓名",name="userName",required=true)
private String userName; /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.age
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
@ApiModelProperty(value="年龄",name="age",required=true)
private Integer age; /**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column student.phone
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
@ApiModelProperty(value="手机号",name="phone",required=true)
private String phone; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} }

StudentPlusMapper接口类,实现BaseMapper用来实现Mybatis-Plus的增强功能。

package com.huixiaoer.ant.api.repository.mysql.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huixiaoer.ant.api.model.bean.Student; public interface StudentPlusMapper extends BaseMapper<Student> {
//不需要实现,这时候就可以调用baseMapper的增删改查了
}

StudentService业务接口类

package com.huixiaoer.ant.api.service;

import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.bean.StudentExample;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey; import java.util.List; public interface StudentService {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
long countByExample(StudentExample example); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
int deleteByExample(StudentExample example); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
@Insert({
"insert into student (id, user_name, ",
"age, phone)",
"values (#{id,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, ",
"#{age,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR})"
})
@SelectKey(statement="select uuid_short()", keyProperty="id", before=true, resultType=String.class)
int insert(Student record); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
int insertSelective(Student record); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
List<Student> selectByExample(StudentExample example); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
int updateByExampleSelective(@Param("record") Student record, @Param("example") StudentExample example); /**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table student
*
* @mbg.generated Thu Oct 31 14:09:39 CST 2019
*/
int updateByExample(@Param("record") Student record, @Param("example") StudentExample example);
}

StudentServiceImpl业务接口实现类,这里将mybatis-plus的mapper接口注入其中。

package com.huixiaoer.ant.api.service.impl;

import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.bean.StudentExample;
import com.huixiaoer.ant.api.repository.mysql.mapper.StudentMapper;
import com.huixiaoer.ant.api.repository.mysql.mapper.StudentPlusMapper;
import com.huixiaoer.ant.api.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
@Slf4j
public class StudentServiceImpl implements StudentService { @Autowired
private StudentMapper studentMapper; @Autowired
private StudentPlusMapper studentPlusMapper; @Override
public long countByExample(StudentExample example) {
return 0;
} @Override
public int deleteByExample(StudentExample example) {
return 0;
} @Override
public int insert(Student record) {
return studentPlusMapper.insert(record);
} @Override
public int insertSelective(Student record) {
return studentMapper.insertSelective(record);
} @Override
public List<Student> selectByExample(StudentExample example) {
return null;
} @Override
public int updateByExampleSelective(Student record, StudentExample example) {
return 0;
} @Override
public int updateByExample(Student record, StudentExample example) {
return 0;
}
}

SchoolController类,实现前端和后台的交互。自动注入学生业务实现类。

package com.huixiaoer.ant.api.controller;

import com.huixiaoer.ant.api.common.constant.ResultCode;
import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.vi.StudentVI;
import com.huixiaoer.ant.api.service.impl.StudentServiceImpl;
import com.huixiaoer.ant.api.util.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID; /**
* @author create by yiqiang.wu
* @create 2019/06/12
* @email yiqiang.wu@huixiaoer.com
* @description 登录
*/
@Slf4j
@RestController
@Api(tags = "学校 相关接口")
public class SchoolController { @Autowired
private StudentServiceImpl studentService; @Autowired
private HttpServletRequest request; @ApiOperation(value = "增加学生信息")
@PostMapping(value = "/insert/student")
public CommonResult insertStudent(@RequestBody @ApiParam(name="学生对象",value="传入json格式",required=true) StudentVI studentVI) {
try {
Student student = new Student();
// student.setId(UUID.randomUUID().toString());
student.setUserName(studentVI.getUserName());
student.setAge(studentVI.getAge());
student.setPhone(studentVI.getPhone());
studentService.insert(student);
} catch (Exception e) {
System.out.println(e);
return CommonUtil.buildResponse(ResultCode.SYSTEM_ERROR, ResultCode.SYSTEM_ERROR_MSG);
}
return CommonUtil.buildResponse(ResultCode.SUCCESS, ResultCode.SUCCESS_MSG);
} }

补充一下Mybatis的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.huixiaoer.ant.api.repository.mysql.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.huixiaoer.ant.api.model.bean.Student">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
<constructor>
<arg column="id" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="user_name" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="age" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="phone" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
<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">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
<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">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
id, user_name, age, phone
</sql>
<select id="selectByExample" parameterType="com.huixiaoer.ant.api.model.bean.StudentExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from student
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="com.huixiaoer.ant.api.model.bean.StudentExample">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
delete from student
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insertSelective" parameterType="com.huixiaoer.ant.api.model.bean.Student">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
select uuid_short()
</selectKey>
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
id,
<if test="userName != null">
user_name,
</if>
<if test="age != null">
age,
</if>
<if test="phone != null">
phone,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{id,jdbcType=VARCHAR},
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.huixiaoer.ant.api.model.bean.StudentExample" resultType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
select count(*) from student
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
update student
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=VARCHAR},
</if>
<if test="record.userName != null">
user_name = #{record.userName,jdbcType=VARCHAR},
</if>
<if test="record.age != null">
age = #{record.age,jdbcType=INTEGER},
</if>
<if test="record.phone != null">
phone = #{record.phone,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Oct 31 14:09:39 CST 2019.
-->
update student
set id = #{record.id,jdbcType=VARCHAR},
user_name = #{record.userName,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
phone = #{record.phone,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
</mapper>

各种UUID生成策略,生成的UUID值进行比较。

Mybatis-Plus的BaseMapper的用法的更多相关文章

  1. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  2. MyBatis从入门到精通(2):MyBatis XML方式的基本用法

    本章将通过完成权限管理的常见业务来学习 MyBatis XML方式的基本用法 2.1一个简单的权限控制需求 权限管理的需求: 一个用户拥有若干角色,一个角色拥有若干权限,权限就是对某个模块资源的某种操 ...

  3. MyBatis #{} 和 ${} 引用值的用法

    1.#{} 引用值的用法 UserMapper配置文件: <select id="queryOne" resultType="cn.tedu.mybatis.bea ...

  4. MyBatis从入门到精通(二):MyBatis XML方式的基本用法之Select

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. 明确需求 书中提到的需求是一个基 ...

  5. MyBatis从入门到精通(三):MyBatis XML方式的基本用法之多表查询

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. 多表查询 上篇博客中,我们示例的 ...

  6. MyBatis从入门到精通(四):MyBatis XML方式的基本用法之增删改

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. insert用法 1.1 简单的 ...

  7. MyBatis:choose标签的用法

    <!-- 4.2 choose用法 需求: 在已有的sys_user表中,除了主键id外,我们认为user_name也是唯一的, 所有的用户名都不可以重复.现在进行如下查询:当参数id有值的时候 ...

  8. MyBatis中foreach循环的用法

    一.在了解foreach之前,先了解一下mybatis传入参数及parameterType 1.我们在Dao层向对应的mapper.xml文件传递参数时,可以传递的参数有: ①.基本数据类型(如int ...

  9. MyBatis从入门到精通(第2章):MyBatis XML方式的基本用法【insert用法、update用法、delete用法】

    2.4  insert 用法 2.4.1  简单的 insert方法 在接口 UserMapper.java 中添加如下方法. /** * 新增用户 * @param sysUser * @retur ...

随机推荐

  1. Java内存模型(三)原子性、内存可见性、重排序、顺序一致性、volatile、锁、final

          一.原子性 原子性操作指相应的操作是单一不可分割的操作.例如,对int变量count执行count++d操作就不是原子性操作.因为count++实际上可以分解为3个操作:(1)读取变量co ...

  2. [AGC040C] Neither AB nor BA

    Description 一个长度为 n 的字符串是好的当且仅当它由 'A', 'B', 'C' 组成,且可以通过若干次删除除了"AB"和"BA"的连续子串变为空 ...

  3. Java 错误:Constructor call must be the first statement in a constructor

    今天用学校里的黑马程序员通Java语法 想到了:在有参构造函数中调用无参构造函数 语法是这样的: class Person{ private int age; public Person() { Sy ...

  4. mysql开启和关闭安全模式

    今天在进行数据库UPDATE的时候出了一个问题,我把非主键的字段当作UPDATE的条件(筛选条件)时,提示了如下的语句 Error Code: 1175. You are using safe upd ...

  5. 用Python快速找到出现次数最多的数据

    给你一个文件,每行一个iip?

  6. java native本地方法详解(转)

    文章链接出处: 详解native方法的使用 自己实现一个Native方法的调用 JNI 开始本篇的内容之前,首先要讲一下JNI.Java很好,使用的人很多.应用极 广,但是Java不是完美的.Java ...

  7. 关于Faster-RCNN训练细节

    Faster RCNN训练: 四部训练法: Faster R-CNN,可以大致分为两个部分,一个是RPN网络,另一个是Fast R-CNN网络,前者是一种候选框(proposal)的推荐算法,而后者则 ...

  8. css样式水平居中和垂直居中的方法

    水平居中(包含块中居中) 1. 定宽,左右margin为auto.(常规流块盒.弹性项目[不用定宽]) 例子:在box1盒子上设置宽,再设置margin:auto: <style> .bo ...

  9. jQuery效果--隐藏和显示

    jQuery hide() 和 show() 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: $("#hide").click( ...

  10. vue高亮一级、二级导航

    使用vue开发过程中有的项目会存在多级导航的情况,如下图,这种就存在了两层,那么该如何高亮一级导航,又该如何高亮二级导航这就是今天我要记录的内容. 1.高亮一级导航很简单,代码如下: // 点击一级导 ...