项目文件结构
src
-main
-java
-resources
-static
-templates
application.properties/application.yml
-test
pom.xml配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
两种方式 1. application.properties 2. application.yml
修改application.properties文件
server.port=80
server.context-path=/
server
port: 80
context-path: /
@RestController
等同于Controller
和ResponseBody
联合
@Value
@RequestMapping
@ConfigurationProperties
@RequestParam
获取参数值
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Girl
表示对应的实体类 Integer
表示对应实体类的 主键类型@Repository
将dao注入到容器中@Autowired
对dao变量进行装配public List <Girl> findByAge(Integer age);
@Transactional
添加aop依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
类的前面添加@Aspect
和@Component
注解
@Before("execution(public * com.admxj.controller.GirlController.finaAll())")
public void log(){
System.out.println("11111");
}
@Before("log()")
public void doBefore(JoinPoint joinPoint){
//获取请求的内容
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
logger.info("url={}",request.getRequestURL());
logger.info("method={}",request.getMethod());
logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
logger.info("arg={}",joinPoint.getArgs());
}
//获取返回的内容
@AfterReturning(returning = "object", pointcut = "log()")
public void doAfterReturnning(Object object){
logger.info("response={}",object.toString());
}
service
测试,测试类前面添加@RunWith(SpringRunner.class)
@SpringBootTest
controller
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@Autowired
private MockMvc mvc
mvc.perform()