SSM项目代码优化及技巧积累(一)

通过定义公共工具类和常量类,可以提升代码的复用性和维护性。公共工具类如DateUtils封装了日期格式化方法,方便统一管理和修改。常量类如Contents则用于集中管理全局常量,如登录状态返回码,避免直接使用硬编码,简化后期修改。这两种方式有助于减少代码冗余,提高代码质量。

1.定义公共工具类

对于一些经常需要使用到的方法,可以定义一个工具类。比如我们在项目中经常会使用到对于一个Date类型的数据进行格式化,方便对日期进行比较或得到我们想要的格式。那么在一个项目中可能多处使用到如下这种日期格式化的方法。

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH​:mm:​ss");

String nowTime= sdf.format(new Date());

在项目维护时,如果我们需要修改格式,那要找到每一段如下代码,修改格式参数,比较繁琐。所以我们可以借助公共的工具类将这个日期格式化方法进行封装,然后就可轻松调用和维护。

public class DateUtils{
    public static String formateDateTime(Date date){
        //对指定的Date对象进行格式化成yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate=sdf.format(date);
        return newDate;
        
    }
}
​//调用方法
DateUtils.formateDateTime(new Date)

2.定义公共常量类

在一个项目的实现过程中,前端会接收到一些后端控制器传回来的一些信息,这些信息往往表示具体含义,比如说我们在进行登录功能实现时,可以用1表示登录成功,0表示登录失败。

在这种情况下如果我们需要对代码进行修改,不用1或者0来表示,换成其他值,比如改成布尔值,改成000,111。那我们需要去代码块中找到所有使用到的代码。那么为了降低维护成本,可以定义一个公共的常量类。

比如

public class Contents(){
    public static final String RETURN_CODE_SUCCESS="1";
     public static final String RETURN_CODE_FAILURE="0";
}
​
​
//调用时
Message.setCode(Contents.RETURN_CODE_SUCCESS);
 return Message;   

SQL代码美化程序 SQL Pretty Printer 3.2.8 Copyright 2005-2011, Gudu Software. All Rights Reserved http://www.dpriver.com -------------------------------------------------------- Overview -------- SQL Pretty Printer is a tool that will help you beautify your SQL code. Using hotkey functionality, SQL Pretty Printer can reformat SQL statements for a wide variety of database tools such as Microsoft Query Analyzer, SQL Server Management Studio (SSMS), TOAD and PL/SQL Developer, development environments such as Visual Studio 2003/2005/2008 and Eclipse, and popular editors such as UltraEditor and EditPlus. In addition to beautifying SQL code, SQL Pretty Printer can translate SQL code into C#, Java, PHP, DELPHI and other program languages. SQL Pretty Printer also includes command line functionality, with the ability to format single files, single directories and multiple directories. SQL Pretty Printer is designed to deal with the syntax used by most popular database systems including Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). Output conforms to most of the entry level SQL99 Standard. Add-Ins for SSMS and Visual Studio 2003/2005/2008/2011 are available. APIs for dotnet and COM version are available. features: ** Beautifies SQL statements utilizing highly customizable format options. ** Formats SQL on-the-fly in popular tools and editors using hotkey functionality. ** Minimizes to the system tray for quick access. ** Includes a command line for batch conversion of single files, single directories or directory trees (use the command line API in your own program!) ** Verifies SQL syntax with detailed error information. ** Converts monochrome SQL code into colorful RTF document. ** Converts monochrome SQL code into colorful HTML for easy placement in blogs and forums. ** Converts SQL to various programming languages including C#, Java, DELPHI, PHP and others. ** Currently supports SQL syntax for Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). ** Add-In for SQL Server Management Studio available. ** Add-In for Visual Studio 2003/2005/2008 available. Requirements ------------ Pentium class CPU or higher Windows 95/98/NT/2000/XP/Vista/win7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ethan_Zhuo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值