springBoot入门

一、springBoot概念

        1)创建独立的spring应用程序

        2)直接内嵌tomcat、jetty和undertow(不需要打包成war包部署,直接jar包)

二、入门程序:

      1. 引入依赖

      2.创建引导类      

/**
*  @SpringBootApplication:组合注解: 
*                                   @SpringBootConfiguration:声明为springBoot的配置类
*                                   @EnableAutoConfiguration:开启自动配置
*                                   @ComponentScan:开启注解扫描
*/

@SpringBootApplication
public class UserApplication {

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

        3.编写controller         

@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("hello")
    public String test(){
        return "hello ssm";
    }
}

        4.1.整合springMvc

            1) 添加全局配置文件

                application.yml中配置端口:默认为8080             

#映射端口
server.port = 80

             2)访问静态资源

                默认静态资源路径:

                 classpath:/META-INF/resources/

                 classpath:/resources/

                 classpath:/static/

                 classpath:/public/

            只要静态资源放在这些目录中任何一个,SpringMVC都会帮我们处理

       4.2.  添加拦截器

              使用java配置的方式,配置拦截器,方法: 实现WebMvcConfigurer并添加@Configuration注解;

              1)定义一个拦截器                 

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle method is running!");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle method is running!");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion method is running!");
    }
}

             2).定义配置类,注册拦截器

             

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private MyInterceptor myInterceptor;

    /**
     * 重写接口中的addInterceptors方法,添加自定义拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

         4.3.整合连接池

            在pom.xml中引入启动器,springBoot会自动引入;       


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
dependency>

          配置参数在application.yml中:              

# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/heima
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10

         4.4.整合mybatis

             1) pom.xml中引入mybatis的启动器                 


<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>1.3.2version>
dependency>

               2)在application.yml中引入配置          

# mybatis 别名扫描
mybatis.type-aliases-package=cn.itcast.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml

                注意: 这里没有配置mapper接口扫描包,因此我们需要给每一个Mapper接口添加@Mapper注解,才能被识别。  也可以在启动类上加上@MapperScanner("mapper的全路径类名")            

@Mapper
public interface UserMapper {
}

           4.5.整合通用mapper

               1)  在pom.xml中引入通用mapper的启动器

                 


<dependency>
    <groupId>tk.mybatisgroupId>
    <artifactId>mapper-spring-boot-starterartifactId>
    <version>2.0.2version>
dependency>

              2)然后让mapper接口继承Mapper<实体类>

@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{
}

            4.6.整合事务

                 1)在引入jdbc或者web的启动器,就已经引入事务相关的依赖及默认配置了;

                  2)使用:在相应的方法上加上@Transactional

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User queryById(Long id){
        return this.userMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void deleteById(Long id){
        this.userMapper.deleteByPrimaryKey(id);
    }
}

          4.6.启动测试

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id")Long id){
        return this.userService.queryById(id);
    }

    @GetMapping("hello")
    public String test(){
        return "hello ssm";
    }
}

         pom.xml文件:

           

xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0modelVersion>

    <groupId>cn.itcast.usergroupId>
    <artifactId>itcast-userartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.6.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>

        
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.0.2version>
        dependency>
    dependencies>

project>

             application.yml文件中的配置

              

server.port=80

logging.level.org.springframework=debug

spring.datasource.url=jdbc:mysql://localhost:3306/heima
spring.datasource.username=root
spring.datasource.password=root

# mybatis 别名扫描
mybatis.type-aliases-package=cn.itcast.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
# mybatis.mapper-locations=classpath:mappers/*.xml

 

            

 

   

 

               

            

            

 

你可能感兴趣的:(springBoot入门)