美文网首页
SpringBoot关于在java接口类上面打注解还是在接口的实

SpringBoot关于在java接口类上面打注解还是在接口的实

作者: 墨色尘埃 | 来源:发表于2017-11-08 16:59 被阅读0次

一般不会在接口上放注解,在实现类上注解,这样才符合热插拔式,如果后期更换实现类,直接注解其他实现类就可以。
第一种:ICornerMark接口类-->CornerMarkImp实现类-->CornerMarkController
比如:在ICornerMark接口类上加上@Component而不在CornerMarkImp实现类上加,运行项目的时候会报错。

第二种:LayerMapper-->LayerMapper.xml-->LayerController
LayerMapper
但是在mapper接口上加@Component又不会启动失败。

第一种:
ICornerMark

/**
 * Created by zxm on 2017/11/2.
 * 首页 装移机,修障,营销,智慧家庭角标数字
 */
public interface ICornerMark {

    CornerMarkInfo getCornerMark();
}

CornerMarkImp

@Component
public class CornerMarkImp implements ICornerMark {

    .......
}

CornerMarkController

@RestController
@RequestMapping("/cornerMark")
public class CornerMarkController {

    private Logger log = LoggerFactory.getLogger(CornerMarkController.class);

    @Autowired
    ICornerMark cornerMark;

    @RequestMapping(value = "/getCornerMark", method = RequestMethod.GET)
    public ResponseObj<CornerMarkInfo> getHomePage() {
        CornerMarkInfo info = new CornerMarkInfo();
        try {
            info = cornerMark.getCornerMark();
            log.info("getCornerMark" + info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseObj<>(info, null);
    }

}

第二种:

@Component
public interface LayerMapper {

    /*查询图层列表*/
    List<LayerInfo> selectLayerList();

    /*传入整条sql语句查询查询图层所有点*/
    List<LinkedHashMap<String, Object>> selectLayerPointList(String sql);

    List<LinkedHashMap<String, Object>> superManagerSelect(String sql);
}

LayerMapper.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="zhab.dao.LayerMapper">

    <resultMap type="zhab.model.LayerInfo" id="resultLayerList">
        <result column="LAYER_ID" property="layerId"/>
        <result column="TABLE_NAME" property="tableName"/>
        <result column="LAYER_NAME" property="layerName"/>
        <result column="LAYER_DESC" property="layerDesc"/>
        <result column="CREATER_ID" property="createrId"/>
        <result column="CREATER_NAME" property="createrName"/>
        <result column="DEPARTMENT" property="department"/>
        <result column="LAYER_TYPE" property="layerType"/>
        <result column="AVAILABLE" property="available"/>
        <result column="UPDATE_TIME_" property="updateTime"/>
        <result column="CREATE_TIME_" property="createTime"/>
        <result column="LAYER_ICON_TYPE" property="layerIconType"/>
        <result column="VECTOR_ICON_TYPE" property="vectorIconType"/>
        <result column="VECTOR_ICON_COLOR" property="vctorIconColor"/>
        <result column="VECTOR_ICON_SIZE" property="vectorIconSize"/>
        <result column="FIX_ICON" property="fixIcon"/>
        <result column="CUSTOM_ICON_NAME" property="customIconName"/>
        <result column="ICON_ZOOM" property="iconZoom"/>
        <result column="AUTHORITY_TYPE" property="authorityType"/>
        <result column="SQL_FOR_WINDOW" property="sqlForWindow"/>
        <result column="SQL_FOR_DETAIL" property="sqlForDetail"/>
        <result column="SQL_FOR_TABLE" property="sqlForTable"/>
        <result column="SQL_FOR_SEARCH" property="sqlForSearch"/>
        <result column="SHARE_RANGE" property="shareRange"/>

    </resultMap>

    <select id="selectLayerList" resultMap="resultLayerList">
        SELECT * FROM t_layer_list
    </select>

    <!--<select id="selectLayerPointList" resultMap="resultLayerPointList" statementType="STATEMENT">-->
    <select id="selectLayerPointList" parameterType="String" resultType="java.util.LinkedHashMap">
        ${value}
    </select>

    <select id="superManagerSelect" parameterType="String" resultType="java.util.LinkedHashMap">
        ${value}
    </select>

</mapper>

LayerController

@RestController
@RequestMapping("/layer")
public class LayerController {
    @Autowired
    LayerMapper layerMapper;

    public static final ObjectMapper mapper = new ObjectMapper();

    @Value("${interfaceUrl.ipUrl}")
    protected String ipUrl;
  
    ........
}

相关文章

网友评论

      本文标题:SpringBoot关于在java接口类上面打注解还是在接口的实

      本文链接:https://www.haomeiwen.com/subject/hdjymxtx.html