LineService.java:

查询分页:使用pagehelper

  1. package com.sgcc.hpc.dc.service;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.sgcc.hpc.dc.filter.LineFilter;
  5. import com.sgcc.hpc.dc.mapper.LineMapper;
  6. import com.sgcc.hpc.dc.model.Line;
  7. import com.sgcc.hpc.dc.model.vo.LineVo;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. import java.util.Objects;
  12. /**
  13. * @Author: 李孟帅
  14. * @CreateTime: 2021/4/13 9:15
  15. * @Description: TODO
  16. */
  17. @Service
  18. public class LineService {
  19. @Autowired
  20. private LineMapper lineMapper;
  21. /**
  22. * @Author: 李孟帅
  23. * @CreateTime: 2021/4/13 13:41
  24. * @Description: 添加一条链路
  25. */
  26. public int addLine(Line line){
  27. return lineMapper.addLine(line);
  28. }
  29. /**
  30. * @Author: 李孟帅
  31. * @CreateTime: 2021/4/13 13:44
  32. * @Description: 删除一条链路
  33. */
  34. public int deleteLine(int id){
  35. return lineMapper.deleteLine(id);
  36. }
  37. /**
  38. * @Author: 李孟帅
  39. * @CreateTime: 2021/4/13 12:57
  40. * @Description: 更新链路信息(根据id)
  41. */
  42. public int updateLine(Line line){
  43. return lineMapper.updateLine(line);
  44. }
  45. /**
  46. * @Author: 李孟帅
  47. * @CreateTime: 2021/4/13 9:16
  48. * @Description: 根据当前节点名称和采集协议获取所有投运的链路信息(is_operation=1)
  49. */
  50. public List<Line> selectLineByNameAndProtocol(String nodeName, String protocolType) {
  51. return lineMapper.selectLineByNameAndProtocol(nodeName, protocolType);
  52. }
  53. /**
  54. * @Author: 李孟帅
  55. * @CreateTime: 2021/4/13 12:55
  56. * @Description: 查询所有的链路信息,根据查询条件筛选
  57. */
  58. public PageInfo<Line> selectLine(LineVo lineVo) {
  59. Integer page = lineVo.getPage();
  60. Integer size = lineVo.getSize();
  61. Line line = lineVo.getLine();
  62. if (Objects.isNull(page)){
  63. page=1;
  64. }
  65. if (Objects.isNull(size)){
  66. size=10;
  67. }
  68. PageHelper.startPage(page, size);
  69. List<Line> lines = lineMapper.selectLine(line);
  70. return new PageInfo<>(lines);
  71. }
  72. }

LineMapper.java

  1. package com.sgcc.hpc.dc.mapper;
  2. import com.sgcc.hpc.dc.model.Line;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Repository;
  6. import java.util.List;
  7. /**
  8. * @Author: 李孟帅
  9. * @CreateTime: 2021/4/13 9:08
  10. * @Description: TODO
  11. */
  12. @Repository
  13. @Mapper
  14. public interface LineMapper {
  15. Integer addLine(Line line);
  16. Integer deleteLine(int id);
  17. Integer updateLine(Line line);
  18. List<Line> selectLineByNameAndProtocol(String nodeName, String protocolType);
  19. List<Line> selectLine(Line line);
  20. }

LineMapper.xml

包含增删改查

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.sgcc.hpc.dc.mapper.LineMapper">
  4. <resultMap id="BaseResultMap" type="com.sgcc.hpc.dc.model.Line">
  5. <result column="id" jdbcType="INTEGER" property="id"/>
  6. <result column="protocol_type" jdbcType="VARCHAR" property="protocolType"/>
  7. <result column="comm_add" jdbcType="VARCHAR" property="commAdd"/>
  8. <result column="is_operation" jdbcType="VARCHAR" property="isOperation"/>
  9. <result column="operation_time" jdbcType="TIMESTAMP" property="operationTime"/>
  10. <result column="node_name" jdbcType="VARCHAR" property="nodeName"/>
  11. <result column="change_time" jdbcType="TIMESTAMP" property="changeTime"/>
  12. <result column="fes_time" jdbcType="TIMESTAMP" property="fesTime"/>
  13. <result column="fes_type" jdbcType="INTEGER" property="fesType"/>
  14. <result column="station_ip_1" jdbcType="VARCHAR" property="stationIp1"/>
  15. <result column="port_1" jdbcType="VARCHAR" property="port1"/>
  16. <result column="station_ip_2" jdbcType="VARCHAR" property="stationIp2"/>
  17. <result column="port_2" jdbcType="VARCHAR" property="port2"/>
  18. <result column="station_code" jdbcType="VARCHAR" property="stationCode"/>
  19. <result column="fes_period" jdbcType="INTEGER" property="fesPeriod"/>
  20. <result column="fes_nodeId" jdbcType="VARCHAR" property="fesNodeId"/>
  21. <result column="need_cer" jdbcType="INTEGER" property="needCer"/>
  22. <result column="cdkey1" jdbcType="VARCHAR" property="cdkey1"/>
  23. <result column="cdkey2" jdbcType="VARCHAR" property="cdkey2"/>
  24. </resultMap>
  25. <insert id="addLine" parameterType="com.sgcc.hpc.dc.model.Line">
  26. INSERT INTO hpc_manage_line(protocol_type,comm_add,is_operation,operation_time,node_name,change_time,fes_time,fes_type,
  27. station_ip_1,port_1,station_ip_2,port_2,station_code,fes_period,fes_nodeId,need_cer,cdkey1,cdkey2)
  28. VALUES(#{protocolType},#{commAdd},#{isOperation},#{operationTime},#{nodeName},#{changeTime},#{fesTime},#{fesType},
  29. #{stationIp1},#{port1},#{stationIp2},#{port2},#{stationCode},#{fesPeriod},#{fesNodeId},#{needCer},#{cdkey1},#{cdkey2})
  30. </insert>
  31. <insert id="deleteLine">
  32. DELETE FROM hpc_manage_line WHERE id=#{id}
  33. </insert>
  34. <update id="updateLine" parameterType="com.sgcc.hpc.dc.model.Line">
  35. UPDATE hpc_manage_line
  36. <set>
  37. <if test="protocolType != null">protocol_type=#{protocolType},</if>
  38. <if test="commAdd != null">comm_add=#{commAdd},</if>
  39. <if test="isOperation != null">is_operation=#{isOperation},</if>
  40. <if test="operationTime != null">operation_time=#{operationTime},</if>
  41. <if test="isOperation != null">is_operation=#{isOperation},</if>
  42. <if test="nodeName != null">node_name=#{nodeName},</if>
  43. <if test="changeTime != null">change_time=#{changeTime},</if>
  44. <if test="fesTime != null">fes_time=#{fesTime},</if>
  45. <if test="fesType != null">fes_type=#{fesType},</if>
  46. <if test="stationIp1 != null">station_ip_1=#{stationIp1},</if>
  47. <if test="port1 != null">port_1=#{port1},</if>
  48. <if test="stationIp2 != null">station_ip_2=#{stationIp2},</if>
  49. <if test="port2 != null">port_2=#{port2},</if>
  50. <if test="stationCode != null">station_code=#{stationCode},</if>
  51. <if test="fesPeriod != null">fes_period=#{fesPeriod},</if>
  52. <if test="fesNodeId != null">fes_nodeId=#{fesNodeId},</if>
  53. <if test="needCer != null">need_cer=#{needCer},</if>
  54. <if test="cdkey1 != null">cdkey1=#{cdkey1},</if>
  55. <if test="cdkey2 != null">cdkey2=#{cdkey2},</if>
  56. </set>
  57. WHERE id=#{id}
  58. </update>
  59. <select id="selectLineByNameAndProtocol" resultMap="BaseResultMap">
  60. SELECT * FROM hpc_manage_line WHERE is_operation = 1 AND node_name = #{nodeName} AND protocol_type = #{protocolType}
  61. </select>
  62. <select id="selectLine" parameterType="com.sgcc.hpc.dc.model.Line" resultMap="BaseResultMap">
  63. SELECT
  64. 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,
  65. fes_period,fes_nodeId,need_cer,cdkey1,cdkey2
  66. FROM hpc_manage_line
  67. <where>
  68. <if test="protocolType !=null and protocolType != ''">
  69. and protocol_type LIKE CONCAT('%',#{protocolType},'%' )
  70. </if>
  71. <if test="isOperation != null and isOperation != ''">
  72. and is_operation = #{isOperation}
  73. </if>
  74. <if test="nodeName != null">
  75. and node_name LIKE CONCAT('%',#{nodeName},'%' )
  76. </if>
  77. <if test="operationTime != null ">
  78. and operation_time &lt;= #{operationTime}
  79. </if>
  80. <if test="stationCode != null and stationCode !=''">
  81. and station_code LIKE CONCAT('%',#{stationCode},'%' )
  82. </if>
  83. <if test="stationList !=null">
  84. AND station_code IN
  85. <foreach collection="stationList" item="item" index="index" open="(" separator="," close=")">
  86. #{item}
  87. </foreach>
  88. </if>
  89. </where>
  90. ORDER BY operation_time DESC
  91. </select>
  92. </mapper>

application.yml

  1. mybatis:
  2. mapper-locations: classpath:mapper/*.xml
  3. type-aliases-package: com.sgcc.hpc.dc.mapper
  4. config-location: classpath:/mybatis-config.xml
  5. pagehelper:
  6. helperDialect: mysql
  7. reasonable: true
  8. supportMethodsArguments: true
  9. params: count=countSql

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <!-- 使全局的映射器启用或禁用缓存。 -->
  8. <setting name="cacheEnabled" value="true" />
  9. <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
  10. <setting name="lazyLoadingEnabled" value="true" />
  11. <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
  12. <setting name="aggressiveLazyLoading" value="true"/>
  13. <!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
  14. <setting name="multipleResultSetsEnabled" value="true" />
  15. <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
  16. <setting name="useColumnLabel" value="true" />
  17. <!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
  18. <setting name="useGeneratedKeys" value="false" />
  19. <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
  20. <setting name="autoMappingBehavior" value="PARTIAL" />
  21. <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
  22. <setting name="defaultExecutorType" value="SIMPLE" />
  23. <setting name="defaultStatementTimeout" value="25" />
  24. <setting name="defaultFetchSize" value="100" />
  25. <setting name="safeRowBoundsEnabled" value="false" />
  26. <!-- 使用驼峰命名法转换字段。 -->
  27. <setting name="mapUnderscoreToCamelCase" value="true" />
  28. <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
  29. <setting name="localCacheScope" value="SESSION" />
  30. <!-- 默认为OTHER,为了解决oracle插入null报错的问题要设置为NULL -->
  31. <setting name="jdbcTypeForNull" value="NULL" />
  32. <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
  33. <!-- 打印查询语句 -->
  34. <setting name="logImpl" value="STDOUT_LOGGING" />
  35. </settings>
  36. </configuration>