美文网首页
springBoot入门总结

springBoot入门总结

作者: 林ze宏 | 来源:发表于2018-05-10 21:45 被阅读0次

要点

  • Springboot 使用FastJson解析JSON数据
App.class:入口文件
package com.zehong;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
@MapperScan("com.zehong.*") //扫描:该包下相应的class,主要是MyBatis的持久化类.
public class App extends WebMvcConfigurerAdapter {

    /**
     * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
     * @return
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1、需要先定义一个 convert 转换消息的对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        
        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        
        //3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);
        
        
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
    
    /**
     * 启动springBoot的主入口
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*
         * 在main方法进行启动我们的应用程序.
         */
        SpringApplication.run(App.class, args);
    }
}

pom.xml:
<!-- 添加fastjson 依赖包. -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

  • Springboot + devtools(热部署)
<!-- spring boot devtools 依赖包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
           <scope>true</scope>
        </dependency>

<!-- 这是spring boot devtool plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- fork :  如果没有该项配置,则这个devtools不会起作用,即应用不会restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>
  • 配置server信息、数据源、JPA等信息
application.properties:

########################################################
###server配置信息
########################################################
#spring boot 默认的端口是8080
#server.port = 8081
#spring boot 默认路径是 /
#server.context-path = /springboot

#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
# character encoding to use for URL decoding
server.tomcat.uriEncoding=UTF-8




########################################################
###datasource -- 数据库连接的配置信息
########################################################
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.testWhileIdle=true
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.validationQuery=SELECT 1
spring.datasource.test-on-borrow=true
spring.datasource.initialSize=5
spring.datasource.minIdle=50
spring.datasource.maxActive=10
spring.datasource.maxIdle=20


########################################################
### Java Persistence Api --  Spring jpa 配置信息
########################################################
# 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

  • SpringBoot结合Mybatis,pom.xml中需要引入的包
         <!-- 
            spring-boot-starter-web: MVC,AOP的依赖包....
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 
                <version></version>
                由于我们在上面指定了 parent(spring boot)
             -->
        </dependency>

        <!--
            spring-boot mybatis依赖: 
            https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
        -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- 添加MySQL数据库驱动依赖包. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

  • mybatis引入pageHelper分页插件
pom.xml:
        <!--
            添加mybatis分页插件pagehelper
            MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
            将其作为一个plugin装入到SqlSessionFactory中。 
            Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。 
            Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
            这里注意:pagehelper版本5.x 和 4.x是不同的
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.0</version>
        </dependency>
         -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
  • 其他知识点
1:JdbcTemplate:
package com.zehong.demo.dao;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zehong.demo.bean.Cat;

@Repository
public class CatDao {
    
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    public Cat selectByCatName(String catName){
        /**
         * 1、定义一个Sql语句;
         * 2、定义一个RowMapper.
         * 3、执行查询方法.
         */
        String sql = "select * from cat where cat_name=?";
        RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
        Cat cat = jdbcTemplate.queryForObject(sql, new Object[]{catName}, rowMapper);
        
        return cat;
    }
}

2:spring boot使用thymeleaf、freemarker等模板引擎

项目地址:https://gitee.com/hongzelin/springBoot

相关文章

网友评论

      本文标题:springBoot入门总结

      本文链接:https://www.haomeiwen.com/subject/xygtdftx.html