spring-boot2代码_json

App.java

package com.kfit;

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 com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.
 * @author Angel -- 守护天使
 * @version v.0.1
 * @date 2016年12月10日
 */
//extends WebMvcConfigurerAdapter
@SpringBootApplication
public class App{
    
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        super.configureMessageConverters(converters);
//        
//        /*
//         * 1、需要先定义一个 convert 转换消息的对象;
//         * 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
//         * 3、在convert中添加配置信息.
//         * 4、将convert添加到converters当中.
//         * 
//         */
//        
//        // 1、需要先定义一个 convert 转换消息的对象;
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        
//        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.PrettyFormat
//        );
//        
//        //3、在convert中添加配置信息.
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        
//        //4、将convert添加到converters当中.
//        converters.add(fastConverter);
//        
//    }
    
    
    /**
     * 在这里我们使用 @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);
    }
    
    
    /**
     * 这是springloader的配置方式:-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
     * @param args
     */
    public static void main(String[] args) {
        /*
         * 在main方法进行启动我们的应用程序.
         */
        SpringApplication.run(App.class, args);
    }
}

总结.txt

Spring Boot JPA 总结
---------------------
1、需要添加相应的依赖包;
2、需要在application.properties文件添加配置信息;
3、需要创建一个实体类,比如Cat;
4、需要创建一个接口继承CrudRepository;
5、需要创建一个Service;
6、需要创建一个Controller;
7、代码测试;

Cat.java

package com.kfit.demo.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * 创建了一个实体类。
 * 
 * 如何持久化呢?
 * 
 * 1、使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中有
 * 
 * @Entity 注解的时候,会在数据库中生成对应的表结构信息。
 * 
 * 
 * 如何指定主键以及主键的生成策略?
 * 
 * 2、使用@Id指定主键.
 * 
 * 
 * 
 * @author Angel -- 守护天使
 * @version v.0.1
 * @date 2016年12月17日
 */
@Entity  //会创建一张Cat表
public class Cat {
    
    /**
     * 使用@Id指定主键.
     * 
     * 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
     * 指定主键的生成策略,mysql默认的是自增长。
     * 
     */
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;//主键.
    
    private String catName;//姓名. cat_name
    
    private int catAge;//年龄. cat_age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public int getCatAge() {
        return catAge;
    }

    public void setCatAge(int catAge) {
        this.catAge = catAge;
    }
    
    
}

CatController.java

package com.kfit.demo.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kfit.demo.bean.Cat;
import com.kfit.demo.service.CatService;

@RestController
@RequestMapping("/cat")
public class CatController {
    
    @Resource
    private CatService catService;
    
    @RequestMapping("/save")
    public String save(){
        Cat cat = new Cat();
        cat.setCatName("jack");
        cat.setCatAge(3);
        catService.save(cat);
        return "save ok.";
    }
    
    @RequestMapping("/delete")
    public String delete(){
        catService.delete(2);
        return "delete ok";
    }
    
    @RequestMapping("/getAll")
    public Iterable<Cat> getAll(){
        return catService.getAll();
    }
    
}

CatRepository.java

package com.kfit.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.kfit.demo.bean.Cat;

public interface CatRepository extends CrudRepository<Cat, Integer>{

}

CatService.java

package com.kfit.demo.service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.kfit.demo.bean.Cat;
import com.kfit.demo.repository.CatRepository;

@Service
public class CatService {
    
    @Resource
    private CatRepository catRepository;
    
    /**
     * save,update ,delete 方法需要绑定事务.
     * 
     * 使用@Transactional进行事务的绑定.
     * 
     * @param cat
     */
    
    //保存数据.
    @Transactional
    public void save(Cat cat){
        catRepository.save(cat);
    }
    
    //删除数据》
    @Transactional
    public void delete(int id){
        catRepository.delete(id);
    }
    
    //查询数据.
    public Iterable<Cat> getAll(){
        return catRepository.findAll();
    }
    
}

application.properties

########################################################
###datasource -- \u6307\u5b9amysql\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f.
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/spboot1
spring.datasource.username = root
spring.datasource.password = 
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


########################################################
### Java Persistence Api --  Spring jpa\u7684\u914d\u7f6e\u4fe1\u606f.
########################################################
# 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

pom.xml

<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.0</modelVersion>

    <groupId>com.kfit</groupId>
    <artifactId>spring-boot-hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-hello</name>
    <url>http://maven.apache.org</url>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- spring-boot-starter-web: MVC,AOP的依赖包.... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- <version></version> 由于我们在上面指定了 parent(spring boot) -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>


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

        <!-- spring boot devtools 依赖包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <!-- <optional>true</optional> -->
            <!-- <scope>true</scope> -->
        </dependency>

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

        <!-- 添加Spring-data-jpa依赖,数据库操作有用。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        

    </dependencies>

    <!-- 构建节点. -->
    <build>
        <plugins>


            <!-- 在这里添加springloader plugin <plugin> <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <dependency> 
                <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> 
                <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> 
                <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> 
                </configuration> </execution> </executions> </plugin> -->

            <!-- 这是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>


            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

        </plugins>

        <defaultGoal>compile</defaultGoal>
    </build>


</project>