2020
10-09
10-09
java spring整合junit操作(有详细的分析过程)
此博客解决了什么问题:解决测试的时候代码冗余的问题,解决了测试工程师的编码能力可能没有开发工程师编码能力的问题,解决了junit单元测试和spring注解相结合!测试类代码:(只给大家展示测试类的代码)publicclassAccountServiceTest{@TestpublicvoidtestFindAll(){//1.获取容器ApplicationContextac=newClassPathXmlApplicationContext("bean.xml");//2.得到业务层对象IAccountServiceas=ac.getBean("accoun...
继续阅读 >
自定义欢迎页SpringBoot项目在启动后,首先会去静态资源路径下查找index.html作为首页文件,若查找不到,则会去查找动态的index文件作为首页文件。例如,如果想使用静态的index.html作为首页,那么只需在resources/static目录下创建index.html文件即可。若想使用动态页面作为项目首页,则需在resources/templates目录下创建index.html(使用Thymeleaf模板)或者index.ft!(使用FreeMarker模板),然后在Controller中返...
基本配置JdbcTemplate基本用法实际上很简单,开发者在创建一个SpringBoot项目时,除了选择基本的Web依赖,再记得选上Jdbc依赖,以及数据库驱动依赖即可,如下:项目创建成功之后,记得添加Druid数据库连接池依赖(注意这里可以添加专门为SpringBoot打造的druid-spring-boot-starter,而不是我们一般在SSM中添加的Druid),所有添加的依赖如下:<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-star...
导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>基本配置spring.redis.port=638...
一、创建项目并导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>二、相关配置和代码在创建完项目时,我们得springboot项目所有接口都被保护起来了,如果要想访问必须登陆,用户名默认是user,密码在项目启动时生成在...
主要是重写attemptAuthentication方法导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>相关配置和代码application.properties配置密码spring.security.user.name=adminspring.security.user.password=123创建自定义身...
在SecurityConfig中加入@BeanRoleHierarchyroleHierarchy(){RoleHierarchyImplroleHierarchy=newRoleHierarchyImpl();Stringhierarchy="ROLE_adb>ROLE_admin\nROLE_admin>ROLE_user";roleHierarchy.setHierarchy(hierarchy);returnroleHierarchy;}在SpringBoot2.08版本的时候是以下写法用空格把ROLE_dba>ROLE_admin和ROLE_admin>ROLE_user分开SpringBoot2.0.8(不含)之后得版本用\n来区分root角色...
前言:在订单业务中,有时候需要对订单设置有效期,有效期到了后如果还未支付,就需要修改订单状态。对于这种业务的实现,有多种不同的办法,比如:1、使用querytz,每次生成一个订单,就创建一个定时任务,到期后执行业务代码;2、rabbitMq中的延迟队列;3、对Redis的Key进行监控; 1、引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></...
在jdbc的模板中使用具名参数:1、就需要在之前的jdbc的例子中进行修改;需要在xml文件中重新配置一个bean。这是固定的格式。如下对于使用具名参数而言。配置NamedParameterJdbcTemplate,该对象可以说使用具名参数。其没有无参数的构造器。所以必须为其构造器指定参数。2、之后在test的稳健者哄。得到bean的对象。之后进行测试。具体的代码示例如下;主要的区别是在sql语句的后边value中不在是问号。而是一下名字。之后通过map进行...