VueBlog

1、数据库建表时:

timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下:

1.CURRENT_TIMESTAMP 

当要向数据库执行insert操作时,如果有个timestamp字段属性设为 

CURRENT_TIMESTAMP,则无论这个字段有木有set值都插入当前系统时间 

2.ON UPDATE CURRENT_TIMESTAMP

当执行update操作是,并且字段有ON UPDATE CURRENT_TIMESTAMP属性。则字段无论值有没有变化,他的值也会跟着更新为当前UPDATE操作时的时间。

2、springboot的@Configuration

作用:替代以前的applicationContext.xml文件,完成spring容器的初始化。

例子1@Configuration+@ComponentScan

作用:功能类似于在applicationContext.xml文件中配置组件扫描器。在定义各级bean时,使用@Controoler,@Service,@Component等注释,就可以自动完成spring容器对Bean的装载。

复制代码
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/*
配置器
 */
@Configuration
@ComponentScan("com.yrc.test4")
public class MyConfig {
}

例子2::@Configuration+@Bean

作用::功能类似于在applicationContext.xml文件手动注册Bean。此时在各级Bean中需要添加setter方法,

复制代码
@Configuration
public class MyConfig {
    @Bean
    public FunctionService functionService() {
        return new FunctionService();
    }

    @Bean
    public UseFunctionService useFunctionService(FunctionService functionService) {
        UseFunctionService useFunctionService = new UseFunctionService();
        useFunctionService.setFunctionService(functionService);
        return useFunctionService;
    }
}
复制代码

 例子3:@Configuration+@ComponentScan+@EnableAspectJAutoProxy

作用:实现AOP配置,@EnableAspectJAutoProxy开启自动代理

@Configuration
@ComponentScan("com.yrc.test6")
@EnableAspectJAutoProxy
public class MyConfig {
}

springboot推荐使用用java代码的形式申明注册bean。 
@Configuration注解可以用java代码的形式实现spring中xml配置文件配置的效果。

@Configuration
public class TestMybaitsConf {
 
    @Bean
    public DataSource dataSource() {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://192.168.100.25:6660/TXSMS?useUnicode=true&characterEncoding=utf-8");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return dataSource;
    }
 
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactory factory = null;
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));
        try {
            factory = bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return factory;
    }
 
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
 
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

也可以使用xml注册bean

@Configuration
@ImportResource("classpath:spring-mybatis.xml")
public class TestMybaitsConf {
 

3、ComponentScan注解

做的事情就是告诉Spring从哪里找到bean

由你来定义哪些包需要被扫描。一旦你指定了,Spring将会将在被指定的包及其下级包(sub packages)中寻找bean

下面分别介绍在Spring Boot项目和非Spring Boot项目(如简单的JSP/Servlet或者Spring MVC应用)中如何定义Component Scan

Spring Boot项目
总结:

  1. 如果你的其他包都在使用了@SpringBootApplication注解的main
    app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
  2. 如果你有一些bean所在的包,不在main
    app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包

举个例子,看下面定义的类

package com.demo.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = 
                SpringApplication.run(SpringbootApplication .class, args);

        for (String name : applicationContext.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }
}

类 SpringbootApplication 在com.demo.springboot包下,这个类使用了@SpringBootApplication注解,该注解定义了Spring将自动扫描包com.demo.springboot及其子包下的bean

如果你项目中所有的类都定义在com.demo.springboot包及其子包下,那你不需要做任何事

但假如你一个类定义在包com.demo.somethingelse下,则你需要将这个新包也纳入扫描的范围,有两个方案可以达到这个目的。

方案1
定义@CoponentScan(“com.demo”)

这么做扫描的范围扩大到整个父包com.demo

@ComponentScan(“com.demo”)
@SpringBootApplication
public class SpringbootApplication {

方案2

定义分别扫描两个包
@ComponentScan({“com.demo.springboot”,”com.demo.somethingelse”})

@ComponentScan({"com.demo.springboot","com.demo.somethingelse"})
@SpringBootApplication
public class SpringbootApplication {

特别注意一下:如果使用了方案2,如果仅仅只写@ComponentScan({"com.demo.somethingelse"})将导致com.demo.springboot包下的类无法被扫描到(框架原始的默认扫描效果无效了)

4、@MapperScan()

在SpringBoot中集成MyBatis,可以在mapper接口上添加@Mapper注解,将mapper注入到Spring,但是如果每一个mapper都添加@mapper注解会很麻烦,这时可以使用@MapperScan注解来扫描包。

经测试发现,@MapperScan注解只会扫描包中的接口,不会扫描类,所以可以在包中写Provider类。

@MapperScan("com.demo.mapper"):扫描指定包中的接口

@MapperScan("com.demo..mapper"):一个代表任意字符串,但只代表一级包,比如可以扫到com.demo.aaa.mapper,不能扫到com.demo.aaa.bbb.mapper

@MapperScan("com.demo.*.mapper"):两个代表任意个包,比如可以扫到com.demo.aaa.mapper,也可以扫到com.demo.aaa.bbb.mapper

在SpringBoot中,除了使用@ConfigurationProperties注入属性的值,还可以使用Spring的底层注解@Value注入属性的值。

@Component

public class Person {

    @Value("${person.last-name}")

    private String lastName;

    private Integer age;

    private Boolean boss;

    private Date birth;

}

这个两个注解都能注入属性的值,那么这两个注解的区别是什么?

@ConfigurationProperties能够批量注入配置文件的属性。

@Value只能一个个指定。

@ConfigurationProperties支持松散绑定。

@ConfigurationProperties(prefix = "person"),只需要指定一个前缀,就能绑定有这个前缀的所有属性值。

@Value支持SpringEl的语法。@ConfigurationProperties不支持SpringEl的语法。

@ConfigurationProperties还支持JSR303进行配置文件值及校验。

@Slf4j的使用

https://www.cnblogs.com/sxdcgaq8080/p/11288213.html

@RestControllerAdvice

https://www.jianshu.com/p/47aeeba6414c

@ResponseStatus

自定义返回状态码

https://www.cnblogs.com/fnlingnzb-learner/p/10685651.html

跨域问题

https://blog.csdn.net/dreamcatcher1314/article/details/78652884

解决方案

https://blog.csdn.net/zhaokejin521/article/details/81007884

@DateTimeFormat与@JsonFormat 日期转换

 https://www.cnblogs.com/mracale/p/9828346.html

5、mybatis-plus入门:
6、lombok基本使用:
7、springboot异常关闭退出:
使用任务管理器停止进程。
8、shiro与jwt使用:
 
原文地址:https://www.cnblogs.com/baldprogrammer/p/13703150.html