springboot总结

  1. spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置spring boot会自动选择最合适的版本进行添加。
     

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <!--java.version 指定jdk版本号-->
    <java.version>1.8</java.version>
    
    <!--添加spring-boot-starter-web依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    
    
  2. 引入fastjson。
    注意:这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。
     

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>
    
  3. 配置fastjson-两种方法。
    3.1第一种方法
            1)启动类继承extends WebMvcConfigurerAdapter。
            2)覆盖方法
    configureMessageConverters。
     

    @SpringBootApplication
    public class ApiCoreApp  extends WebMvcConfigurerAdapter {
    	
    	@Override
    	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        	super.configureMessageConverters(converters);
    		
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
     
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.PrettyFormat
            );
            fastConverter.setFastJsonConfig(fastJsonConfig);
    		
        	converters.add(fastConverter);
    	}
    }
    

    3.2第二种方法
            1)注入Bean : HttpMessageConverters
             

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
    	FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    	FastJsonConfig fastJsonConfig = new FastJsonConfig();
    	fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    	fastConverter.setFastJsonConfig(fastJsonConfig);
    	HttpMessageConverter<?> converter = fastConverter;
    	return new HttpMessageConverters(converter);
    }
    

     

  4. 使用devtools热部署。
    4.1添加依赖包。
    4.2添加spring-boot-maven-plugin插件。
    说明:1)devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),因为其采用的虚拟机机制,该项重启是很快的。
               2)devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties
    文件中配置spring.thymeleaf.cache=false实现(这里注意不同的模板配置不一样)
               3)测试:修改类
    -->保存:应用会重启;修改配置文件-->保存:应用会重启;修改页面-->保存:应用会重启,页面会刷新(原理是将spring.thymeleaf.cache设为false
    注意:1)工具不能使用分析--对应的spring-boot版本是否正确,这里使用的是1.4.1版本;
               2)
    是否加入plugin以及属性<fork>true</fork>
               3)Eclipse Project 是否开启了Build Automatically(我自己就在这里栽了坑,不知道为什么我的工具什么时候关闭了自动编译的功能)。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
        <scope>true</scope>
    </dependency>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                <!--fork :  如果没有该项配置,devtools不会起作用,即应用不会restart -->
                <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  5. 添加spring data jpa-hibernate
    步骤:
            1)在pom.xml添加mysql,spring-data-jpa依赖;
            2)在application.properties文件中配置mysql连接配置文件;
            3)在application.properties文件中配置jpa配置信息;
     

    <!--添加依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    #在application.properties文件中配置mysql连接配置文件#
    ########################################################
    ###datasource
    ########################################################
    spring.datasource.url = jdbc:mysql://localhost:3306/test
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    spring.datasource.max-active=20
    spring.datasource.max-idle=8
    spring.datasource.min-idle=8
    spring.datasource.initial-size=10
    
    #在application.properties文件中配置JPA信息#
    ########################################################
    ### Java Persistence Api
    ########################################################
    # Specify the DBMS
    spring.jpa.database = MYSQL
    # Show or not log for each sql query
    spring.jpa.show-sql = true
    # Hibernate ddl auto (create, create-drop, update)
    spring.jpa.hibernate.ddl-auto = update
    # Naming strategy
    #[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    # stripped before adding them to the entity manager)
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    
    

     

  6. Spring Boot Spring Data JPA介绍

    6.1Repository接口。
            
    Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 :  public interface Repository<T, ID extends Serializable> { }。
            注意:
    1) Repository是一个空接口,即是一个标记接口;

                       2)若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean纳入到IOC容器                 中,进而可以在该接口中定义满足一定规范的方法。

                       3) 实际上也可以通过@RepositoryDefinition,注解来替代继承Repository接口。

                       4) 查询方法以find | read | get开头;

                       5) 涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。

                       6)使用@Query注解可以自定义JPQL语句实现更灵活的查询。

    6.2CrudRepository接口。
            
    CrudRepository 接口提供了最基本的对实体类的添删改查操作 :
             1)T save(T entity);//保存单个实体;
             2)Iterable
    <T> save(Iterable<? extends T> entities);//保存集合
             3)T
    findOne(ID id);//根据id查找实体 
             4)boolean exists(ID id);//根据id判断实体是否存在
             5)Iterable
    <T> findAll();//查询所有实体,不用或慎用!
             6)long count();//查询实体数量
             7)void delete(ID id);//
    根据Id删除实体
             8)void delete(T entity);//删除一个实体
             9)void delete(
    Iterable<? extends T> entities);//删除一个实体的集合 
             10) void deleteAll();//删除所有实体,不用或慎用!
    6.3PagingAndSortingRepository
    接口。
        
         该接口提供了分页与排序功能  :
             
     1)Iterable<T> findAll(Sort sort); //排序 
              2)Page<T>
    findAll(Pageable pageable); //分页查询(含排序功能)

  7.  

    全局异常捕捉。
    新建一个类GlobalDefaultExceptionHandler,在class注解上@ControllerAdvice,在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下:
     

    @ControllerAdvice
    public class GlobalDefaultExceptionHandler{
    	
    	@ExceptionHandler(value = Exception.class)
    	public void defaultErrorHandler(HttpServletRequest req, Exception e)  {
    }
    

     

  8.  

    其他配置。

    Spring boot 默认端口是8080,如果想要进行更改的话,只需要修改applicatoin.properties文件,在配置文件中加入

    #访问地址就是http://ip:port/spring-boot

    server.context-path=/spring-boot

    #server.port=8080

    #server.address= # bind to a specific NIC

    #server.session-timeout= # session timeout in seconds

    #the context path, defaults to '/'

    #server.context-path=/spring-boot

    #server.servlet-path= # the servlet path, defaults to '/'

    #server.tomcat.access-log-pattern= # log pattern of the access log

    #server.tomcat.access-log-enabled=false # is access logging enabled

    #server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers

    #server.tomcat.remote-ip-header=x-forwarded-for

    #server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)

    #server.tomcat.background-processor-delay=30; # in seconds

    #server.tomcat.max-threads = 0 # number of threads in protocol handler

    #server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding

     

     

     

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值