LineService.java:
查询分页:使用pagehelper
package com.sgcc.hpc.dc.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgcc.hpc.dc.filter.LineFilter;
import com.sgcc.hpc.dc.mapper.LineMapper;
import com.sgcc.hpc.dc.model.Line;
import com.sgcc.hpc.dc.model.vo.LineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 9:15
* @Description: TODO
*/
@Service
public class LineService {
@Autowired
private LineMapper lineMapper;
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 13:41
* @Description: 添加一条链路
*/
public int addLine(Line line){
return lineMapper.addLine(line);
}
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 13:44
* @Description: 删除一条链路
*/
public int deleteLine(int id){
return lineMapper.deleteLine(id);
}
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 12:57
* @Description: 更新链路信息(根据id)
*/
public int updateLine(Line line){
return lineMapper.updateLine(line);
}
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 9:16
* @Description: 根据当前节点名称和采集协议获取所有投运的链路信息(is_operation=1)
*/
public List<Line> selectLineByNameAndProtocol(String nodeName, String protocolType) {
return lineMapper.selectLineByNameAndProtocol(nodeName, protocolType);
}
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 12:55
* @Description: 查询所有的链路信息,根据查询条件筛选
*/
public PageInfo<Line> selectLine(LineVo lineVo) {
Integer page = lineVo.getPage();
Integer size = lineVo.getSize();
Line line = lineVo.getLine();
if (Objects.isNull(page)){
page=1;
}
if (Objects.isNull(size)){
size=10;
}
PageHelper.startPage(page, size);
List<Line> lines = lineMapper.selectLine(line);
return new PageInfo<>(lines);
}
}
LineMapper.java
package com.sgcc.hpc.dc.mapper;
import com.sgcc.hpc.dc.model.Line;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author: 李孟帅
* @CreateTime: 2021/4/13 9:08
* @Description: TODO
*/
@Repository
@Mapper
public interface LineMapper {
Integer addLine(Line line);
Integer deleteLine(int id);
Integer updateLine(Line line);
List<Line> selectLineByNameAndProtocol(String nodeName, String protocolType);
List<Line> selectLine(Line line);
}
LineMapper.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.sgcc.hpc.dc.mapper.LineMapper">
<resultMap id="BaseResultMap" type="com.sgcc.hpc.dc.model.Line">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="protocol_type" jdbcType="VARCHAR" property="protocolType"/>
<result column="comm_add" jdbcType="VARCHAR" property="commAdd"/>
<result column="is_operation" jdbcType="VARCHAR" property="isOperation"/>
<result column="operation_time" jdbcType="TIMESTAMP" property="operationTime"/>
<result column="node_name" jdbcType="VARCHAR" property="nodeName"/>
<result column="change_time" jdbcType="TIMESTAMP" property="changeTime"/>
<result column="fes_time" jdbcType="TIMESTAMP" property="fesTime"/>
<result column="fes_type" jdbcType="INTEGER" property="fesType"/>
<result column="station_ip_1" jdbcType="VARCHAR" property="stationIp1"/>
<result column="port_1" jdbcType="VARCHAR" property="port1"/>
<result column="station_ip_2" jdbcType="VARCHAR" property="stationIp2"/>
<result column="port_2" jdbcType="VARCHAR" property="port2"/>
<result column="station_code" jdbcType="VARCHAR" property="stationCode"/>
<result column="fes_period" jdbcType="INTEGER" property="fesPeriod"/>
<result column="fes_nodeId" jdbcType="VARCHAR" property="fesNodeId"/>
<result column="need_cer" jdbcType="INTEGER" property="needCer"/>
<result column="cdkey1" jdbcType="VARCHAR" property="cdkey1"/>
<result column="cdkey2" jdbcType="VARCHAR" property="cdkey2"/>
</resultMap>
<insert id="addLine" parameterType="com.sgcc.hpc.dc.model.Line">
INSERT INTO hpc_manage_line(protocol_type,comm_add,is_operation,operation_time,node_name,change_time,fes_time,fes_type,
station_ip_1,port_1,station_ip_2,port_2,station_code,fes_period,fes_nodeId,need_cer,cdkey1,cdkey2)
VALUES(#{protocolType},#{commAdd},#{isOperation},#{operationTime},#{nodeName},#{changeTime},#{fesTime},#{fesType},
#{stationIp1},#{port1},#{stationIp2},#{port2},#{stationCode},#{fesPeriod},#{fesNodeId},#{needCer},#{cdkey1},#{cdkey2})
</insert>
<insert id="deleteLine">
DELETE FROM hpc_manage_line WHERE id=#{id}
</insert>
<update id="updateLine" parameterType="com.sgcc.hpc.dc.model.Line">
UPDATE hpc_manage_line
<set>
<if test="protocolType != null">protocol_type=#{protocolType},</if>
<if test="commAdd != null">comm_add=#{commAdd},</if>
<if test="isOperation != null">is_operation=#{isOperation},</if>
<if test="operationTime != null">operation_time=#{operationTime},</if>
<if test="isOperation != null">is_operation=#{isOperation},</if>
<if test="nodeName != null">node_name=#{nodeName},</if>
<if test="changeTime != null">change_time=#{changeTime},</if>
<if test="fesTime != null">fes_time=#{fesTime},</if>
<if test="fesType != null">fes_type=#{fesType},</if>
<if test="stationIp1 != null">station_ip_1=#{stationIp1},</if>
<if test="port1 != null">port_1=#{port1},</if>
<if test="stationIp2 != null">station_ip_2=#{stationIp2},</if>
<if test="port2 != null">port_2=#{port2},</if>
<if test="stationCode != null">station_code=#{stationCode},</if>
<if test="fesPeriod != null">fes_period=#{fesPeriod},</if>
<if test="fesNodeId != null">fes_nodeId=#{fesNodeId},</if>
<if test="needCer != null">need_cer=#{needCer},</if>
<if test="cdkey1 != null">cdkey1=#{cdkey1},</if>
<if test="cdkey2 != null">cdkey2=#{cdkey2},</if>
</set>
WHERE id=#{id}
</update>
<select id="selectLineByNameAndProtocol" resultMap="BaseResultMap">
SELECT * FROM hpc_manage_line WHERE is_operation = 1 AND node_name = #{nodeName} AND protocol_type = #{protocolType}
</select>
<select id="selectLine" parameterType="com.sgcc.hpc.dc.model.Line" resultMap="BaseResultMap">
SELECT
id,protocol_type,comm_add,is_operation,operation_time,node_name,change_time,fes_time,fes_type,station_ip_1,port_1,station_ip_2,port_2,station_code,
fes_period,fes_nodeId,need_cer,cdkey1,cdkey2
FROM hpc_manage_line
<where>
<if test="protocolType !=null and protocolType != ''">
and protocol_type LIKE CONCAT('%',#{protocolType},'%' )
</if>
<if test="isOperation != null and isOperation != ''">
and is_operation = #{isOperation}
</if>
<if test="nodeName != null">
and node_name LIKE CONCAT('%',#{nodeName},'%' )
</if>
<if test="operationTime != null ">
and operation_time <= #{operationTime}
</if>
<if test="stationCode != null and stationCode !=''">
and station_code LIKE CONCAT('%',#{stationCode},'%' )
</if>
<if test="stationList !=null">
AND station_code IN
<foreach collection="stationList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
ORDER BY operation_time DESC
</select>
</mapper>
application.yml
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.sgcc.hpc.dc.mapper
config-location: classpath:/mybatis-config.xml
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
<setting name="useColumnLabel" value="true" />
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
<setting name="useGeneratedKeys" value="false" />
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
<setting name="autoMappingBehavior" value="PARTIAL" />
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25" />
<setting name="defaultFetchSize" value="100" />
<setting name="safeRowBoundsEnabled" value="false" />
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
<setting name="localCacheScope" value="SESSION" />
<!-- 默认为OTHER,为了解决oracle插入null报错的问题要设置为NULL -->
<setting name="jdbcTypeForNull" value="NULL" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>