zoukankan      html  css  js  c++  java
  • spring boot 1

    1、创建项目。

    meven可以配置阿里云meven镜像

    <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>

    删除不需要的文件

    pom文件

    <dependencies>
            <dependency>
                <!-- springboot启动web项目依赖-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <!-- springboot单元测试用-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <!-- 使用meven构建项目插件-->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    View Code

    meven库地址:http://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core/2.10.0

    报错:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

    解决:在pom文件添加依赖包:http://blog.csdn.net/liuxiangke0210/article/details/77892564

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.25</version>
            </dependency>

    写一个类:

    package com.milan.quick;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String say(){
            return "Hello Milan";
        }
    }
    View Code

    启动:

    1、右键,run ,访问http://127.0.0.1:8080/hello  OK

    2、进入pom所在目录,运行 mvn srping-boot:run

    3、mvn install 编译程序 进入taget目录 java -jar xxx.jar启动

      自定义配置文件  java -jar xxx.jar --spring.profiles.active=

    设置编码格式:http://blog.csdn.net/frankcheng5143/article/details/50779149

    配置文件支持 properties yml 2种格式:

    application.properties
    -------------------------------
    server.port=8081
    # url前缀 这样访问需要127.0.0.1:8081/quick/hello 访问
    server.context-path=/quick
    
    --------------------------------
    application.yml
    
    server:
      port: 8081
      context-path: /quick

    自定义配置 三个配置文件  application.yml,application-dev.yml,application-test.yml 可以用做环境切换

    #application.yml
    spring:
      profiles:
        active: dev
    
    #application-test.yml
    server:
      port: 8082
      context-path: /quick
    myname: milan
    age: 24
    content: "${myname},age:${age}"
    girl:
      name: ceshi_test
      age:  19
    
    
    #application-dev.yml
    server:
      port: 8080
      context-path: /quick
    myname: milan
    age: 24
    content: "${myname},age:${age}"
    girl:
      name: kaifa_name
      age:  25

    对应代码

    // GirProperties.java
    package com.milan.quick;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    @Component
    @ConfigurationProperties(prefix = "girl")
    public class GirProperties {
        public void setName(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        private String name;
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        private int age;
    }
    
    #HelloController .java
    
    package com.milan.quick;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String say(){
            return myname+age+"content:"+content;
        }
        @RequestMapping(value = "/girl",method = RequestMethod.GET)
        public String mygril(){
            return mygirl.getName()+","+mygirl.getAge();
        }
        @Value("${myname}")
        private String myname;
        @Value("${age}")
        private int age;
        @Value("${content}")
        private String content;
        @Autowired
        private GirProperties mygirl;
    
    }

    注解汇总:

    @Value("${myname}")
    
    @Autowired 使用注解类
    
    @Component
    @ConfigurationProperties(prefix = "girl") //将注解注入到类中

    自定义配置文件启动: java -jar xxx.jar --spring.profiles.active=test

    Controller的使用

    @Controller 处理http请求
    @RestController 返回json。以前返回json需要@ResonseBosy+@Controller组合
    @RequestMapping 配置url映射
    @RequestMapping(value = {"/str","/getstr"}) //2个路径都会被映射。且不限制请求方法

    获取参数中的值

    @PathVariable //获取url中的数据
    @RequestParam //获取请求参数的值
    @GetMapping //组合注解
    @RestController
    @RequestMapping("/test")
    public class Test1Controller {
        //http://127.0.0.1:8080/test/s1/11
        @RequestMapping(value = "/s1/{id}",method = RequestMethod.GET)
        public String s1(@PathVariable("id") int id){
            return  "s1 id is "+id;
        }
        //http://127.0.0.1:8080/test/s2?id=12 获取 id
        @RequestMapping(value = "/s2",method = RequestMethod.GET)
        public String s2(@RequestParam("id") int id){
            return  "s2 id is "+id;
        }
        //http://127.0.0.1:8080/test/s3?id= 设置默认值
        @GetMapping(value = "/s3")
        public String s3(@RequestParam(value = "id",required = true,defaultValue="1") int id){
            return  "s3 id is "+id;
        }
    }

    Spring-Data-Jpa

    JPA:定义了一系列对象持久化的标准。是spring对hibernate的整合。

    依赖增加:

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>RELEASE</version>
            </dependency>

    配置文件增加

    spring:
      profiles:
        active: dev
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://xxx:336/test
        username: --
        password: --
      jpa:
        hibernate:
          ddl-auto: update
        show-sql: true
    package com.milan.quick;
    import org.springframework.data.jpa.repository.JpaRepository;
    import java.util.List;
    public interface  GirlRepository extends JpaRepository<Girl2,Integer>{
        public List<Girl2> findByAge(Integer age);
    }
    // 对单表的增删改查
    package com.milan.quick;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.transaction.Transactional;
    import java.util.List;
    
    @RestController
    public class GirlController {
        @Autowired
        private GirlRepository girlRepository;
        @GetMapping(value = "/girls")
        public List<Girl2> getList(){
            return girlRepository.findAll();
        }
        @PostMapping(value = "/girls")
        public Girl2 Add(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age){
            Girl2 g = new Girl2();
            g.setName(name);
            g.setAge(age);
            return girlRepository.save(g);
        }
    
        @PostMapping(value = "/updategirl")
        public Girl2 Update(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age,@RequestParam(value = "id") Integer id){
            Girl2 g = new Girl2();
            g.setId(id);
            g.setName(name);
            g.setAge(age);
            return girlRepository.save(g);
        }
        @DeleteMapping(value = "/girls")
        public String Delete(@RequestParam(value = "id") Integer id){
             girlRepository.delete(id);
             return  "true";
        }
        @GetMapping(value = "/girls/{id}")
        public Girl2 Search(@PathVariable("id") Integer id){
            return girlRepository.findOne(id);
        }
        @GetMapping(value = "/searchbyage/{age}")
        public List<Girl2> SearchByAge(@PathVariable("age") Integer age){
            return girlRepository.findByAge(age);
        }
        @Autowired
        private GirlService girlService;
        @GetMapping(value = "/tran")
        public  void service(){
            girlService.insertTwo();
        }
    }
    //事务
    package com.milan.quick;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.GetMapping;
    
    import javax.transaction.Transactional;
    
    @Service
    public class GirlService {
        @Autowired
        private GirlRepository girlRepository;
        @Transactional
        public void insertTwo(){
            Girl2 g = new Girl2();
            g.setName("a");
            g.setAge(1);
            girlRepository.save(g);
            Girl2 a = new Girl2();
            a.setName("ba");
            a.setAge(2);
            girlRepository.save(a);
        }
    }
    package com.milan.quick;
    
    import javax.persistence.*;
    
    @Entity
    public class Girl2 {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        private Integer id;
        private String name;
        private int age;
    
        public Girl2() {
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
  • 相关阅读:
    【jQuery Case Study 1】Site:cssninjas.com
    PostgreSQL HA双机热备份配置
    PogreSQL物理备份与逻辑备份
    转:truncate,delete和drop删除表的异同
    pgsequence序列的用例
    Linux下端口占用的解决方法
    Linux下脚本的编写和执行
    schema模式的选用
    pg数据库基础备份的注意事项
    PostgreSQL VMCC多版本并发控制
  • 原文地址:https://www.cnblogs.com/milanmi/p/8376126.html
Copyright © 2011-2022 走看看