1、eclipase中生成自动属性:首先定义好私有字段,然后右键选择source,然后选择Generate Getters and Setters然后出现一个框,选择你需要生成的字段完工。

2、设置固定jdk版本

    <!-- 将运行时环境设置成1.8 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <finalName>spring</finalName>
        <plugins>
            <!-- jdk编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>

有时候文件出现乱码,右键这个文件->properties然后可以看到有个编码,选择utf-8即可

 

3、新建maven项目,没有web.xml问题,如果是创建网站,选择war,如果是执行程序则选择jar,web.xml所在的路径为src/main/webapp/WEB-INF/web.xml

 

4、eclipse的git相关教程:https://my.oschina.net/songxinqiang/blog/192567

 

5、lombok便捷工具使用

 

6、热启动模块,有时候项目变更过,需要重新编译启动起来,java有这么个模块,自动编译然后自动启动起来。

        <!-- 热启动模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
 

然后在build的标签下的configuration标签添加一个<fork>true</fork>即可,位置如下

java中一些入门级技巧

 

7、springboot读取配置文件

方法一

    import org.springframework.core.env.Environment;
    //@Autowired
    //private Environment env;

方法二

    import org.springframework.beans.factory.annotation.Value;
    @Value("${jdbc.user}")
    private String user; 

读取自定义配置文件,在类上添加如**解然后使用方法二来读取配置即可

@PropertySource("classpath:config/config.properties")

 

8、将异常抛到上层:throw new RuntimeException(e);或者方法后面加一个throws exception

9、try-with-resource来捕获异常,c#释放资源可以使用using关键字,但是java代码中经常看到catch中再来try,一直觉得比较搞笑的写法,现在发现是有语法糖的,来个伪代码吧

FileInputStream fis = null;
try {
    fis = new FileInputStream("aa");
} catch (Exception ex) {
    if (fis != null) {
        try {
            fis.close();
        } catch (Exception ex1) {
            throw ex1;
        }
    }
}

来个语法糖,其实就是try关键字多用,和c#中的using关键字功能一样了

try (FileInputStream fis = new FileInputStream("aa")) {
    System.out.println("xxxx");
} catch (Exception ex) {
    throw ex;
}

 

转载于:https://my.oschina.net/uwith/blog/1609501

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-07-12
  • 2021-07-04
猜你喜欢
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-04-23
  • 2021-04-06
  • 2021-10-10
  • 2021-08-06
相关资源
相似解决方案