1 查询

1.1 根据id查询

Employee employee = emplopyeeDao.selectById(1);

1.2 根据条件查询一条数据

Employee employeeCondition = new Employee();
employeeCondition.setId(1);
employeeCondition.setLastName("更新测试");
//若是数据库中符合传入的条件的记录有多条,那就不能用这个方法,会报错
Employee employee = emplopyeeDao.selectOne(employeeCondition);

注:这个方法的sql语句就是where id = 1 and last_name = 更新测试,若是符合这个条件的记录不止一条,那么就会报错。

1.3 根据条件查询多条条据

当符合指定条件的记录数有多条时,上面那个方法就会报错,就应该用这个方法。

Map<String,Object> columnMap = new HashMap<>();
columnMap.put("last_name","东方不败");//写表中的列名
columnMap.put("gender","1");
List<Employee> employees = emplopyeeDao.selectByMap(columnMap);
System.out.println(employees.size());

注:查询条件用map集合封装,columnMap,写的是数据表中的列名,而非实体类的属性名。比如属性名为lastName,数据表中字段为last_name,这里应该写的是last_name。selectByMap方法返回值用list集合接收。

1.4 分页查询

List<Employee> employees = emplopyeeDao.selectPage(new Page<>(1,2),null);
System.out.println(employees);

注:selectPage方法就是分页查询,在page中传入分页信息,后者为null的分页条件,这里先让其为null,讲了条件构造器再说其用法。这个分页其实并不是物理分页,而是内存分页。也就是说,查询的时候并没有limit语句。等配置了分页插件后才可以实现真正的分页。

1.5 多条件构造器

以上基本的 CRUD 操作,我们仅仅需要继承一个 BaseMapper 即可实现大部分单表 CRUD 操作。BaseMapper 提供了多达 17 个方法供使用, 可以极其方便的实现单一、批量、分页等操作,极大的减少开发负担。但是mybatis-plus的强大不限于此,请看如下需求该如何处理:
需求:
我们需要分页查询 tb_employee 表中,年龄在 18~50 之间性别为男且姓名为 xx 的所有用户,这时候我们该如何实现上述需求呢?
使用MyBatis : 需要在 SQL 映射文件中编写带条件查询的 SQL,并用PageHelper 插件完成分页. 实现以上一个简单的需求,往往需要我们做很多重复单调的工作。
使用MP: 依旧不用编写 SQL 语句,MP 提供了功能强大的条件构造器 ------ EntityWrapper。

接下来就直接看几个案例体会EntityWrapper的使用。

1、分页查询年龄在18 - 50且gender为0、姓名为tom的用户:

List<Employee> employees = emplopyeeDao.selectPage(new Page<Employee>(1,3),
new EntityWrapper<Employee>()
.between("age",18,50)
.eq("gender",0)
.eq("last_name","tom")
);

注:由此案例可知,分页查询和之前一样,new 一个page对象传入分页信息即可。至于分页条件,new 一个EntityWrapper对象,调用该对象的相关方法即可。between方法三个参数,分别是column、value1、value2,该方法表示column的值要在value1和value2之间;eq是equals的简写,该方法两个参数,column和value,表示column的值和value要相等。注意column是数据表对应的字段,而非实体类属性字段。

2、查询gender为0且名字中带有老师、或者邮箱中带有a的用户:

List<Employee> employees = emplopyeeDao.selectList(
new EntityWrapper<Employee>()
.eq("gender",0)
.like("last_name","老师")
//.or()//和or new 区别不大
.orNew()
.like("email","a")
);

注:未说分页查询,所以用selectList即可,用EntityWrapper的like方法进行模糊查询,like方法就是指column的值包含value值,此处like方法就是查询last_name中包含“老师”字样的记录;“或者”用or或者orNew方法表示,这两个方法区别不大,用哪个都可以,可以通过控制台的sql语句自行感受其区别。

3、查询gender为0,根据age排序,简单分页:

List<Employee> employees = emplopyeeDao.selectList(
new EntityWrapper<Employee>()
.eq("gender",0)
.orderBy("age")//直接orderby 是升序,asc
.last("desc limit 1,3")//在sql语句后面追加last里面的内容(改为降序,同时分页)
);

注:简单分页是指不用page对象进行分页。orderBy方法就是根据传入的column进行升序排序,若要降序,可以使用orderByDesc方法,也可以如案例中所示用last方法;last方法就是将last方法里面的value值追加到sql语句的后面,在该案例中,最后的sql语句就变为select ······ order by desc limit 1, 3,追加了desc limit 1,3所以可以进行降序排序和分页。

1.6 自定义分页查询

1.6.1 引入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
<!--使用spring boot2整合 pagehelper-spring-boot-starter必须排除一下依赖
因为pagehelper-spring-boot-starter也已经在pom依赖了mybatis与mybatis-spring
所以会与mybatis-plus-boot-starter中的mybatis与mybatis-spring发生冲突
-->
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>


```sql
/**
* 分页查询
*
* @param page
* @param neighbourhood
* @return 实例对象
*/
public PageInfo<NeighbourhoodResult> pageAll(Integer page, Integer size, Neighbourhood neighbourhood) {
1.6.2 分页插件使用
PageHelper.startPage(page, size);
List<NeighbourhoodResult> results = neighbourhoodMapper.query(neighbourhood);

//PageInfo中包装了分页信息
PageInfo<NeighbourhoodResult> info = new PageInfo<>(results);

return info;

2 公共字段自动填充

我们知道,当我们进行插入或者更新操作时,没有设置值的属性,那么在数据表中要么是为null,要么是保留原来的值。有的时候我们我们没有赋值但是却不想让其为空,比如name属性,我们插入时会默认赋上“林志玲”,更新时会默认赋值上“朱茵”,那么就可以用公共字段自动填充。

使用@TableField注解标记填充字段

@TableField(fill = FieldFill.INSERT_UPDATE)//插入和更新时填充
private String name;

3 时间戳

数据库中的时间戳,可以通过注解来实现

@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "录入时间")
private java.util.Date createTime;

设置时间

info.setNeighbourhoodId(neighbourhoodPage.getId());
info.setCreateTime(new Date());
info.setUpdateTime(new Date());

4 多表级联查询

<resultMap id="BaseResultMap" type="org.jeecg.modules.fysys.entity.NeighbourhoodResult">
<!--@Table -->
<result property="id" column="n_id" jdbcType="INTEGER"/>
<result property="name" column="n_name" jdbcType="VARCHAR"/>
<result property="communityId" column="community_id" jdbcType="INTEGER"/>
<result property="communityName" column="community_name" jdbcType="VARCHAR"/>
<result property="remark" column="n_remark" jdbcType="VARCHAR"/>
<result property="streetId" column="streetId" jdbcType="INTEGER"/>
<result property="streetName" column="street_name" jdbcType="VARCHAR"/>
<!--详情表-->
<association property="neighbourhoodInfo" javaType="org.jeecg.modules.fysys.entity.NeighbourhoodInfo">
<!--@Table -->
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="isWall" column="is_wall" jdbcType="INTEGER"/>
<result property="securityLevel" column="security_level" jdbcType="INTEGER"/>
<result property="buildingType" column="building_type" jdbcType="INTEGER"/>
<result property="deliveryTime" column="delivery_time" jdbcType="INTEGER"/>
<result property="propertyFee" column="property_fee" jdbcType="NUMERIC"/>
<result property="monitoringCount" column="monitoring_count" jdbcType="INTEGER"/>
<result property="electronicFence" column="electronic_fence" jdbcType="INTEGER"/>
<result property="electronicFenceIsok" column="electronic_fence_isok" jdbcType="INTEGER"/>
<result property="entranceGuard" column="entrance_guard" jdbcType="INTEGER"/>
<result property="vehicleBrake" column="vehicle_brake" jdbcType="INTEGER"/>
<result property="areaCovered" column="area_covered" jdbcType="OTHER"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
<result property="addr" column="addr" jdbcType="VARCHAR"/>
<result property="neighbourhoodId" column="neighbourhood_id" jdbcType="INTEGER"/>
<result property="createUserId" column="create_user_id" jdbcType="INTEGER"/>
<result property="createTime" column="create_time" jdbcType="OTHER"/>
<result property="updateUserId" column="update_user_id" jdbcType="INTEGER"/>
<result property="updateTime" column="update_time" jdbcType="OTHER"/>
</association>


</resultMap>
<!--通过实体作为筛选条件查询-->
<select id="query" resultMap="BaseResultMap">
SELECT
n.id AS n_id,
n.NAME AS n_name,
n.remark AS n_remark,
c.id AS community_id,
c.NAME AS community_name,
c.streetId,
s.name as street_name,
ni.*
FROM
neighbourhood n
LEFT JOIN neighbourhood_info ni ON n.ID = ni.neighbourhood_id
LEFT JOIN community c ON n.community_id = c.id
LEFT JOIN street s ON s.Id = c.streetId

<where>
<if test="name != null and name != ''">
and n.name like concat('%',concat(#{name, jdbcType=VARCHAR}, '%'))
</if>
<if test="communityId != null">
and n.community_id = #{communityId,jdbcType=INTEGER}
</if>
<if test="remark != null and remark != ''">
and n.remark = #{remark,jdbcType=VARCHAR}
</if>
</where>
order by create_time desc
</select>