Spring Boot 入门

595人浏览 / 0人评论

快速入门

新建Maven项目

项目文件结构

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>

编写main方法

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

启动项目

  1. 使用idea启动
  2. mvn spring-boot:run
  3. mvn install 打成jar 直接运行jar

属性配置

两种方式 1. application.properties 2. application.yml

修改application.properties文件

server.port=80
server.context-path=/
server
    port: 80
    context-path: /

注解

@RestController 等同于ControllerResponseBody联合

@Value

@RequestMapping

@ConfigurationProperties

@RequestParam 获取参数值


SpringBoot JPA的使用

  • 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  1. 编写DAO 接口继承JpaRepository<Girl, Integer> Girl 表示对应的实体类 Integer 表示对应实体类的 主键类型
  2. 在dao前面加入@Repository将dao注入到容器中
  3. controller 使用 @Autowired 对dao变量进行装配
  4. 自定义查询
    • dao声明方法 方法名必须按照指定格式
    public List <Girl> findByAge(Integer age);
    
  5. 数据库的事物操作 在方法前面加入注解@Transactional

AOP处理请求

添加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()