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. 创建私有的cocoapod库

    我是通过cocoachina 的一篇文章 跟着学习的 http://www.cocoachina.com/ios/20150228/11206.html 这里我简单描述下  主要还是给我自己记忆的   ...

  2. maven 的repository index构建

    1,windows -- >preferences里面选择maven,选中“Download repository index updates on startup" 2.window ...

  3. 5.5修改xadmin的头部底部和导航栏名称

    1.修改xadmin的头部标题和底部信息: 在users模块中的adminx.py中添加修改函数: from xadmin import views class GlobalSettings(obje ...

  4. SVN 定时 更新代码 Demo

    1. 涉及技术: Winservice: 用system身份后台跑:  Quartz:定时任务:  SVN 2. 思路: Quartz定时调用cmd 程序,执行SVN update 命令,整个程序寄宿 ...

  5. Ubuntu网络配置IP和DNS等,适用于14.04,16.04和17.10

    本文主要介绍Ubuntu系统的网络设置,包括IP,DNS和主机名等,适用于14.04,16.04和17.10等版本 ===============  完美的分割线 ================ = ...

  6. self-taught learning setting && semi-supervised learning

    参考文献: 摘于上文献: The more general and powerful setting is the self-taught learning setting, which does n ...

  7. 如何搭建Spring MVC 框架---Hello World

    传送门 现在的Web框架基本都采用了MVC(model-view-Controller)设计模式,其中,Servlet和Filter都可以充当控制器.Spring MVC采用一个Servlet作为控制 ...

  8. Session History 属性和方法

    History 接口允许操作浏览器的曾经在标签页或者框架里访问的会话历史记录. js通过window.history来访问和操作的,操作的范围是某个tab的会话历史记录. 这个tab打开后,tab内的 ...

  9. ARM汇编返回指令

    [ 588.756226] task: ffff000008a22f80 task.stack: ffff000008a10000 [ 588.762153] PC is at vb2_buffer_ ...

  10. Linux菜鸟入门级命令大全

    1. man 对你熟悉或不熟悉的命令提供帮助解释eg:man ls 就可以查看ls相关的用法注:按q键或者ctrl+c退出,在linux下可以使用ctrl+c终止当前程序运行.2. ls 查看目录或者 ...