通用mapper集成
pom文件引入依赖tk.mybatis
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
mapper接口要继承Mapper(tk.mybatis.mapper.common.Mapper)
public interface UserDao extends Mapper<UserPO> {
}
需要注意:Mapper不是mybatis中的mapper(org.apache.ibatis.annotations.Mapper),而是tk.mybatis.mapper.common.Mapper中的。
.mapper对应的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.industry.api.sys.user.dao.UserDao">
<resultMap id="BaseResultMap" type="com.industry.api.sys.user.domain.po.UserPO">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="login_name" jdbcType="VARCHAR" property="loginName" />
<result column="user_password" jdbcType="VARCHAR" property="userPassword" />
</resultMap>
</mapper>
通用mapper常用方法汇总
select
根据实体中的属性值进行查询,查询条件使用等号
List<T> selct (T record) ;
根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
T selectByPrimaryKey(Object key);
查询全部结果,select(null)方法能达到同样的效果
List<T> selectAll( );
根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
T selectOne(T record);
根据实体中的属性查询总数,查询条件使用等号
int selectCount(T record);
insert
保存一个实体,null的属性也会保存,不会使用数据库默认值
int insert(T record);
保存一个实体,null的属性不会保存,会使用数据库默认值
int insertSelective(T record);
update
根据主键更新实体全部字段,null值会被更新
int updateByPrimaryKey(T record);
根据主键更新属性不为null的值
int updateByPrimaryKeySelective(T record);
delete
根据实体属性作为条件进行删除,查询条件使用等号
int delete(T record);
根据主键字段进行删除,方法参数必须包含完整的主键属性
int deleteByPrimaryKey(Object key);
Example
使用方法
Example example = new Example(需要CRUD的类(与数据库对应实体类).class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id",id); // id已知 RUD的判断条件=
设置条件
example.orderBy(字段名).asc(); 添加升序排列条件
example.orderBy(字段名).desc(); 添加降序排序条件
example. setDistinct(false); 去除重复,boolean型,true为选择不重复的记录
criteria. andIsNull(字段名); 添加字段xx为null的条件
criteria. andIsNotNull(字段名); 添加字段xx不为null的条件
criteria.andEqualTo(字段名,value); 添加xx字段等于value条件
criteria.andNotEqualTo(字段名,value); 添加xx字段不等于value条件
criteria.andGreaterThan(字段名,value); 添加xx字段大于value条件
criteria.andGreaterThanOrEqualTo(字段名,value); 添加xx字段大于等于vaue条件
criteria.andLessThan(字段名,value); 添加xx字段小于value条件
criteria.andLessThanOrEqualTo(字段名,value); 添加xx字段小于等于value条件
criteria.andIn(字段名,list); 添加xx字段值在List条件
criteria. andNotIn(字段名,list); 添加xx字段值不在List条件
criteria.andLike(字段名,“%”+value+”%”); 添加xx字段值为vaue的模糊查询条件
criteria.andNotLike(字段名,“%”+value+”%”); 添加xx字段值不为 value的模糊查询条件
criteria.andBetween(字段名,value1,value2); 添加xx字段值在value1和value2之间条件
criteria.andNotBetween(字段名,value1,value2); 添加xx字段值不在value1和value2之间条件
根据Example条件进行查询
List<T> selectByExample(Object example);
根据Example条件进行查询总数
int selectCountByExample(Object example);
查询指定字段方法
Example example = new Example(User.class);
example.selectProperties("loginName").and().andEqualTo("userId",userId);
User user = userMapper.selectOneByExample(example);
根据Example条件更新实体record包含的全部属性,null值会被更新
int updateByExample(@Param("record") T record, @Param("example") Object example);
根据Example条件更新实体record包含的不是null的属性值
int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
根据Example条件删除数据
int deleteByExample(Object example);