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>

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";
}
}

启动:

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;
}
}

spring boot 1的更多相关文章

  1. 玩转spring boot——快速开始

    开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...

  2. 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  3. 玩转spring boot——开篇

    很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...

  4. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  5. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  6. 玩转spring boot——结合JPA入门

    参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...

  7. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  8. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  9. 玩转spring boot——结合jQuery和AngularJs

    在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  10. 玩转spring boot——MVC应用

    如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...

随机推荐

  1. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 11

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  2. 5.3 将users表添加到xadmin后台

    在users模块中添加adminx.py文件,是xadmin后台管理默认的文件名,内容是: from .models import EmailVerifyRecord, Banner import x ...

  3. 《FDTD electromagnetic field using MATLAB》读书笔记之 Figure 1.14

    背景: 基于公式1.42(Ez分量).1.43(Hy分量)的1D FDTD实现. 计算电场和磁场分量,该分量由z方向的电流片Jz产生,Jz位于两个理想导体极板中间,两个极板平行且向y和z方向无限延伸. ...

  4. 对ashx请求用Gzip,Deflated压缩

    //GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...

  5. 解决一个java facets问题

    经常被一个问题困扰: JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfi ...

  6. C# zedgraph 怎么设置初始时坐标轴的比例??

    http://bbs.csdn.net/topics/390872329 已解决,,,我问是刷新图用的,,我以为mypane.YAxis.Scale.Min=0; mypane.YAxis.Scale ...

  7. bzoj 4823 [Cqoi2017]老C的方块——网络流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4823 一个不合法方案其实就是蓝线的两边格子一定选.剩下两部分四相邻格子里各选一个. 所以这个 ...

  8. linux 下 ifcfg-eth0 配置/CentOS_minimal安装和开发环境部署

    CentOS_minimal安装和开发环境部署:http://www.th7.cn/system/lin/201305/39002.shtml 网络接口配置文件 [root@localhost ~]# ...

  9. const T & 的适用范围

    我们往往在类中的函数体,重载操作中看到 const T & 的影子,以前还是比较纳闷. 对于非内部数据类型的参数而言,象void Func(A a) 这样声明的函数注定效率比较底.因为函数体内 ...

  10. Weex入门与进阶指南

    Weex入门与进阶指南 标签: WeexiOSNative 2016-07-08 18:22 59586人阅读 评论(8) 收藏 举报 本文章已收录于:  iOS知识库  分类: iOS(87)  职 ...