目录
Mybatis 升级 Mybatis Plus
将实体做为条件参数 带到Mapp.xml 中的自定义SQL
Mybatis 写法
通过 pagehelper 进行分页
Entity
public class ActivityTracking implements Serializable {
private static final long serialVersionUID = -1794277981889967750L;
/**
*主健ID
*/
private String id;
/**
*行为类型编码
*/
private String typeCode;
/**
*行为类型名称
*/
private String typeName;
/**
*行为方式(Add、Update、Delete)
*/
private String activityMethodCode;
/**
*行为方式(新增、修改、删除)
*/
private String activityMethodName;
......get\set
}
Mapper
@Repository
public interface ActivityTrackingMapper {
/**
* 获取 日志列表
*
* @return
*/
List<ActivityTracking> list(ActivityTracking activityTracking);
}
Service
import com.github.pagehelper.PageInfo;
import com.vipsoft.api.base.entity.ActivityTracking;
import com.vipsoft.api.base.util.PageParam;
public interface IActivityTrackingService {
/**
* 获取 日志列表
*
* @return
*/
PageInfo list(PageParam pageParam, ActivityTracking activityTracking);
}
@Service
public class ActivityTrackingServiceImpl implements IActivityTrackingService {
/**
* 获取 日志列表
*
* @param pageParam
* @return
*/
@Override
public PageInfo list(PageParam pageParam, ActivityTracking activityTracking) {
PageHelper.startPage(pageParam);
List<ActivityTracking> activityTrackingList = trackingMapper.list(activityTracking);
return new PageInfo(activityTrackingList);
}
}
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.vipsoft.api.base.bisiness.mapper.ActivityTrackingMapper">
<resultMap id="BaseResultMap" type="com.vipsoft.api.base.entity.ActivityTracking">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="type_code" property="typeCode" jdbcType="VARCHAR"/>
<result column="type_name" property="typeName" jdbcType="VARCHAR"/>
<result column="activity_method_code" property="activityMethodCode" jdbcType="VARCHAR"/>
<result column="activity_method_name" property="activityMethodName" jdbcType="VARCHAR"/>
<result column="business_table" property="businessTable" jdbcType="VARCHAR"/>
<result column="business_table_key" property="businessTableKey" jdbcType="VARCHAR"/>
....
</resultMap>
<select id="list" resultType="com.vipsoft.api.base.entity.ActivityTracking">
select * from sys_activity_tracking
<where>
<if test="typeName != null and typeName != ''">
AND type_name = #{typeName,jdbcType=VARCHAR}
</if>
<if test="businessKey != null and businessKey != ''">
AND business_key = #{businessKey,jdbcType=VARCHAR}
</if>
</where>
</select>
</mapper>
Test
@SpringBootTest
public class DemoTest {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IActivityTrackingService activityTrackingService;
@Test
void listTest() {
ActivityTracking activityTracking = new ActivityTracking();
activityTracking.setTypeName("A"); //将条件传到 Mapper.xml 中,构建SQL
PageParam pageParam = new PageParam();
pageParam.setPageNum(1);
pageParam.setPageSize(10);
PageInfo pageList = activityTrackingService.list(pageParam, activityTracking);
logger.info("List: {}", JSON.toJSON(pageList));
}
}
Mybatis Plus
Mybatis 不用自定义SQL的话,可以不用 Mapper.xml
Entity
不需要改变。如果使用框架方法,需要加上 @TableName、@TableId、@TableField 等属性
Mapper
//@Repository 使用 Mybatis Plus 可以不用 @Repository
public interface ActivityTrackingMapper extends BaseMapper<ActivityTracking> {
//page对应的 selectList 方法,所以 XML中的 select id 名称要写对
}
Service
public interface IActivityTrackingService extends IService<ActivityTracking> {
//不需要定义接口,使用框架中的服务接口 => page
}
@Service
public class ActivityTrackingServiceImpl extends ServiceImpl<ActivityTrackingMapper, ActivityTracking> implements IActivityTrackingService {
//不需要实现
}
Mapper.xml
ew.entity.typeName
这步是重点。不这样写,取不到前面的参数
<!-- selectList 这边的ID 不能写错,否则就不能重写框架的实现了 -->
<select id="selectList" resultType="com.vipsoft.api.base.entity.ActivityTracking">
select * from sys_activity_tracking
<where>
<if test="ew.entity.typeName != null and ew.entity.typeName != ''">
AND type_name = #{ew.entity.typeName,jdbcType=VARCHAR}
</if>
<if test="ew.entity.businessKey != null and ew.entity.businessKey != ''">
AND business_key = #{ew.entity.businessKey,jdbcType=VARCHAR}
</if>
</where>
</select>
Test
@SpringBootTest
public class DemoTest {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IActivityTrackingService activityTrackingService;
@Test
void plusListTest() {
// 这一步是重点。配合XML中的。ew.entity.typeName 取值
ActivityTracking activityTracking = new ActivityTracking();
activityTracking.setTypeName("A"); //将条件传到 Mapper.xml 中,构建SQL
QueryWrapper<ActivityTracking> queryWrapper = new QueryWrapper<>(activityTracking);
// 如果用自定义SQL.不能以这种形式传参,否则会报 org.apache.ibatis.ognl.NoSuchPropertyException: com.baomidou.mybatisplus.core.conditions.query.QueryWrapper.XXX
// queryWrapper.like("type_name", "A");
Page page = new Page(1, 10);
IPage pageList = activityTrackingService.page(page, queryWrapper);
logger.info("List: {}", JSON.toJSON(pageList));
}
}
标签:xml,ActivityTracking,activityTracking,pageParam,Plus,Mybatis,public
From: https://www.cnblogs.com/vipsoft/p/18616924