Java日常编码规范和小知识点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CoderYin/article/details/82379320

                       

                        java日常代码规范和开发技巧(日常更新

简介:规范的代码不仅给人一种简单易读,更重要的是可以提高代码的执行效率。


一、不要在for循环中进行数据库操作(如果数据量小的话可以)

介绍:在java后台中,不要在for循环中进行数据库的操作,因为频繁的进行数据库操作不仅影响程序的执行效率同时也浪费系统资源,在当数据量大的情况下可能会导致程序出错。

解决方法:把需要修改的信息统计在一个集合中,一起进行修改。

 

二、不要在for循环中使用+进行字符串拼接操作

介绍:在使用+进行拼接是都会生成一个新的对象,这对于本来资源就十分紧缺的JVM来说就是十分的昂贵,使用stringBuffer或者stringBuilder来进行拼接。

代码案例:

// 错误的使用方法

String s = "";
  for (int i = 0; i < field.length; ++i) {
    s = s + field[i];
  }


// 正确的使用方法

  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < field.length; ++i) {
    buf.append(field[i]);
  }
  String s = buf.toString();

// 注意:stringBuffer和stringBuider使用方法相同,一个是线程安全的,一个是线程不安全的。

       总结:

String:适用于少量的字符串操作的情况

StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况(非线程安全

StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况(线程安全

 

三、在项目中频繁声明的不变参配置到配置文件中去

介绍:在项目中频繁使用或者多次声明的不变参数,配置到配置文件中去。

使用:

使用声明:

   // 获取在线视频监控的SecretKey
    @Value(value = "${secretKey}")
    private String secretKey;

    // 获取配置在线视频监控文件key
    @Value(value = "${accessKey}")
    private String accessKey;

 

四、mybatis where条件传入字符串(arg1,agr2,arg3,arg4...)后台写法

介绍:在mybatis中根据多条件更新其他统一字段写法。

使用方法:

// 根据sn来修改online,status,mac
    int updateKtvEquipmentStatusOnline(@Param("code") String code,@Param("online")String online,@Param("status")String status,@Param("updateTime")Date updateTime);
<update id="updateKtvEquipmentStatusOnline">
        UPDATE ktv_equipment
        SET
        online=#{online,jdbcType=VARCHAR},
        status=#{status,jdbcType=VARCHAR},
        `updateTime`=#{updateTime, jdbcType=TIMESTAMP}
        WHERE code IN
        <foreach item="item" index="index" collection="code.split(',')"  open="(" separator="," close=")">
            '${item}'
        </foreach>
</update>

五、在使用到时间的时候不要多次使用(new Date())来进行创建,一是生成对象占用系统资源,二是多次生成的时间可能不一致。

介绍:可以通过该类的getCreate()来获取之前设定的时间进行赋值到其他的字段上。

使用:

ktvEquipment.setCreateTime(new Date());
ktvEquipment.setUpdateTime(ktvEquipment.getCreateTime());

六、在mybatis中select语句查询信息时,写出全部需要查询的字段,不要使用select *来代替,如果该表的字段特别多的话可以使用*来代替。

介绍:在mybatisselect语句中查询多少字段就写多少,不要使用*

使用:

<select id="selectKtvEquipmentByName" resultMap="BaseKtvEquipmentResultMap" parameterType="java.lang.String">
        SELECT
        e.id as id,
        e.code as `code`,
        e.name as `name`,
        e.area_id as area_id,
        e.describe as `describe`,
        e.space as `space`,
        e.use_space as use_space,
        e.status as `status`,
        e.online as `online`,
        e.is_use as is_use,
        e.creater as creater,
        e.updateTime as updateTime,
        a.areaName as areaName,
        a.areaFullName as areaFullName
        FROM ktv_equipment as e, sysconfmgmt.t_sysconfmgmt_area as a
        where e.area_id = a.areaId
        <if test="name != null and name != ''">
            and  name  LIKE CONCAT('%',#{name,jdbcType=VARCHAR},'%')
        </if>
        <if test="areaId != null and areaId != ''">
            and  area_id  =  #{areaId,jdbcType=VARCHAR}
        </if>
        ORDER BY e.createTime desc
</select>

七、idea中插件lombok安装,功能实体类get、set、tostring。

介绍:lombok插件安装,可以在实体类上使用@Getter@Setter@ToString注解,不需要再生成该方法。

使用:本地没有的话需要点击下图中的Broswer respositories选择框进行搜索下载安装。

 

八、未完成代码注释(//TODO,//FIXME),IDEA可以方便查找

介绍:在一些暂时没有能力完成的代码块,只能写成死数据的地方使用//TODO和//FIXME进行注释,通过idea工具可以快速的进行定位注释代码块。

使用:

headers.add("sign",sgin);//TODO
headers.add("timestamp",timestamp);//FIXME

猜你喜欢

转载自blog.csdn.net/CoderYin/article/details/82379320