SpringBoot总结(一)

一、定时任务

方式1:fixedDelay当前任务执行完后一分钟再执行

	public final static long PROC_TIME =  30 * 1000;//30秒

    public final static long PROC_INIT_TIME =  17 * 1000;//17秒


    /**
     * 向消息接收人推送待办提醒(流程中消息提醒)
     * 每分钟执行一次(fixedDelay当前任务执行完后一分钟再执行)
     * @return
     */
    @Scheduled(fixedDelay=PROC_TIME)
    @GetMapping("sendmsg")
    @ResponseBody
    public List<TaskWeiXin> sendTaskMsg() {
        return taskWeiXinService.sendTaskMsg();
    }

方式2: 自定义cron表达式实现指定时间端执行

 /**
     * 更新申请状态 每天7-23点 每30分钟执行一次(整点执行如:8:30、9:00、9:30)
     * @Scheduled(cron = "0 0/30 7-23 * * ?")
     */
    @Scheduled(fixedDelay = PROC_TIME)
    @GetMapping("updateStatus")
    public void updateApplyStauts()  {
    }

二、Repository自定义接口
(1)如果存在更新、插入操作需要添加两个注解:@Transactional、@Modifying

@Repository
@Transactional
public interface SuggestRepository extends JpaRepository<Suggest, String>,JpaSpecificationExecutor<Suggest> {
	//根据命名规则定义查询接口
    Suggest findByUuid(String id);

	//自定义sql实现查询
    @Modifying
    @Query(value = "update T_WSBSFWZJ set VIEW_NUM=VIEW_NUM+1 where UUID=?1", nativeQuery = true)
    void updateViewNumByUuid(String uuid);
}

三、配置文件application.yml
(1) 复制粘贴配置文件格式会乱,建议按照格式手动输入
(2)如果使用thymeleaf模版引擎,则HTML代码校验严格开关标签需对应,使用mode: LEGACYHTML5页面不会强制校验代码规则
(3)SpringBoot版本1.5.*的可以使用以下log配置方式

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://IP:3306/databaseName?useUnicode=true&characterEncoding=UTF-8&useSSL=true
    username: 
    password: 

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none

  thymeleaf:
    cache: false
    check-template-location: true
    prefix: classpath:/templates/
    suffix: .html
    enabled: true
    encoding: utf-8
#    取消thymeleaf校验标签闭合
    mode: LEGACYHTML5
    content-type: text/html

   server:
     port: 8080

#日志相关配置
logging:
  level: info
  file: ./logs/rbjc.log

(4)如果使用mode: LEGACYHTML5取消Thymeleaf对HTML的校验,需要在pom.xml文件中引用依赖,版本1.9.22的尝试下载不成功,遂使用1.9.21

<!--取消thymleaf校验-->
<!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.21</version>
</dependency>

四、oracle数据库的链接
(1)需要在项目中添加lib包,并且在pom.xml中引入
(2)需要在添加以下代码,否则打包时引入的jar不存在

<!--Oracle驱动包-->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>14</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
		</dependency>

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!--打包允许加载本地的jar-->
				<configuration>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
	</build>

(3)数据表对应的实体类定义,oracle数据库表字段是大写的需使用@Column(name = “UUID”)标明
(4)需要指定数据表的主键,否则在Repository执行根据ID查询时报错

@Entity(name="T_WSBSFWZJ")
public class Suggest {
    @Id
    @Column(name = "UUID")
    private String uuid;//主键

    @Column(name = "AUTHID")
    private String authid;
}

五、服务启动时执行函数

(1)需要执行的函数

@Order(1)
public class StartupRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(StartupRunner.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... args){
        HttpClientUtil.setStringRedisTemplate(stringRedisTemplate);
        logger.info("启动StringRedisTemplate并实例化");
    }
}

(2)服务启动入口

@SpringBootApplication
public class WsbsfwzjApplication {

	public static void main(String[] args) {
		SpringApplication.run(WsbsfwzjApplication.class, args);
	}

	@Bean
	public StringRedisTemplate template(RedisConnectionFactory connectionFactory){
		return new StringRedisTemplate(connectionFactory);
	}

	//将定义的实体类在服务启动入口主函数添加
	@Bean
	public StartupRunner startupRunner(){
		return new StartupRunner();
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值