Spring Boot,微框架,确实不错,很方便。

相关网站链接:

http://www.tuicool.com/articles/veUjQba

https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details

http://tianmaying.com/tutorial/spring-boot-overview

1.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.zhengmo</groupId>
<artifactId>SpringBootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>SpringBootTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 这里一定要配置上java的版本,如果是1.7版本的可不用配置 -->
<java.version>1.7</java.version>
<!-- 配置你的tomcat版本 -->
<tomcat.version>7.0.55</tomcat.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<!--如果是通过parent方式继承spring-boot-starter-parent则不用此插件 <plugin> -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 父依赖 -->
<!-- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version> </parent> -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies> <dependency>
<!-- 导入jar包 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- 导入jar包 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2.SimpleController.java

package com.zhengmo.springboot;

import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@ComponentScan
@EnableAutoConfiguration
@EnableTransactionManagement
//@RestController
public class SimpleController {
@Autowired
private SimpleDao dao;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
//@ResponseBody
public String hello() {
return "totalCount:" + dao.getUserCount();
}
@RequestMapping(value = "/hellojson/{id}", method = RequestMethod.GET)
@ResponseBody
public List<Map<String, Object>> hellojson(HttpServletRequest req, @PathVariable("id") Long id) {
List<Map<String, Object>> list = dao.getJdbcTemplate().queryForList("select * from users where keyid=?", req.getParameter("keyid") == null ? id : req.getParameter("keyid"));
return list;
}
public static void main(String[] args) {
SpringApplication.run(SimpleController.class, args);
}
}

3.SimpleDao.java

package com.zhengmo.springboot;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; /**
* TODO 新建类.
* @author zhengmo
*/
@Component
public class SimpleDao {
@Bean
@ConfigurationProperties(prefix = "spring.dbcp.datasource")
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setTestOnBorrow(true);
ds.setTestOnReturn(true);
ds.setTestWhileIdle(true);
ds.setValidationQuery("select 1");
return ds;
}
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 设置jdbcTemplate.
* @return 返回jdbcTemplate
*/
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
/**
* 获取jdbcTemplate.
* @param jdbcTemplate 要设置的jdbcTemplate
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public int getUserCount() {
return this.getJdbcTemplate().queryForObject("select count(1) from users", Integer.class);
}
}

4.application.properties

# SPRING CONFIG (ConfigFileApplicationListener)
spring.config.name=
spring.config.location=
spring.main.sources=
spring.main.web-environment=
spring.main.show-banner=true # LOGGING
logging.level.=INFO
logging.path=
logging.file=
logging.config= # IDENTITY (ContextIdApplicationContextInitializer)
spring.application.name=SpringBootTest@2016
spring.application.index=1 #datasource
spring.dbcp.datasource.url=jdbc:mysql://localhost/test
spring.dbcp.datasource.username=root
spring.dbcp.datasource.password=123456
spring.dbcp.datasource.driver-class-name=com.mysql.jdbc.Driver
#
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Server settings (ServerProperties)
server.port=8111
server.address=127.0.0.1
server.sessionTimeout=30
server.contextPath=
# Tomcat specifics
tomcat.accessLogEnabled=false
tomcat.protocolHeader=x-forwarded-proto
tomcat.remoteIpHeader=x-forwarded-for
tomcat.basedir=
# secs
tomcat.backgroundProcessorDelay=30

目录结构:

├─.settings
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─zhengmo
│ │ │ └─springboot
│ │ └─resources
│ └─test
│ └─java
│ └─com
│ └─zhengmo
│ └─springboot
└─target
├─classes
│ ├─com
│ │ └─zhengmo
│ │ └─springboot
│ └─META-INF
│ └─maven
│ └─com.zhengmo
│ └─SpringBootTest
├─generated-sources
│ └─annotations
├─generated-test-sources
│ └─test-annotations
├─maven-archiver
├─maven-status
│ └─maven-compiler-plugin
│ ├─compile
│ │ └─default-compile
│ └─testCompile
│ └─default-testCompile
├─surefire-reports
└─test-classes
└─com
└─zhengmo
└─springboot

SpringBoot Demo的更多相关文章

  1. spring-boot - demo

    当我发现把最初的一个demo整的面目全非的时候,突然想要找一个简单的demo做测试,发现与其在原来的上面该,还不如新建一个demo. 官方入门:http://projects.spring.io/sp ...

  2. 初识gradle, idea+springboot Demo

    写在前面; 使用maven管理写过几个springboot的系统, 此篇博客纯属记录整理学习的过程. 另外, 源码分享地址在最后. Java: 1.8.0_281 tomcat: 1.8 IDE: I ...

  3. (一)SpringBoot Demo之 Hello World

    文章目录 最终效果 pom文件编写 编写资源类 编写控制器 运行项目 原文 : https://spring.io/guides/gs/rest-service/ 类型:官网入门指南 要求:JDK1. ...

  4. springboot demo(二)web开发demo

    如入门般建立项目,引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</g ...

  5. springboot demo(一)快速开始

    快速入门 maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.26以及一些工程基本信息,点击" ...

  6. Spring Boot整合Dubbo框架demo

    Dubbo框架原理见之前的博文:http://www.cnblogs.com/umgsai/p/5836925.html 首先启动zookeeper Server端 Pom配置如下 <?xml ...

  7. springboot 学习笔记(一)

    引子 最近在搞一个项目,走在科技前沿的师兄, 摒弃了公司老套的框架模式, 采用了springboot搭建新应用.看到如此简洁的代码 , 深受诱惑.趁周末闲余之时, 背晒阳光, 学起了springboo ...

  8. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  9. SpringBoot核心注解应用

    1.今日大纲 了解Spring的发展 掌握Spring的java配置方式 学习Spring Boot 使用Spring Boot来改造购物车系统 2.Spring的发展 Spring1.x 时代 在S ...

随机推荐

  1. 关于VS2010无法编译问题

    cvtres.exe 近来遇到无法编译问题,编译后debug之类文件全部为空,纠结好久才发现还是一个以前遇到解决过的问题...= = C:\Windows\Microsoft.NET\Framewor ...

  2. java1234教程系列笔记 S1 Java SE chapter 02 写乘法口诀表

    一.水仙花数 1.方式一:这是我的思路,取各个位数的方式.我个人习惯于使用取模运算. public static List<Integer> dealNarcissiticNumberMe ...

  3. [Nginx] - PHP+FPM相关的配置

    CodeIgniter的配置: worker_processes ; events { worker_connections ; } http { include mime.types; defaul ...

  4. http请求相关

    1.POST方式向服务器发送AJAX请求时   设置请求头 application/x-www-form-urlencoded 2.表单上传文件时     设置请求头 multipart/form-d ...

  5. python之 Redis

    Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...

  6. ruby中tes-unitt数据初始化方法整理

    在用ruby做测试时,很多时候需要一些数据初始化以及事后的数据恢复还原之类的操作,下面整理了这些方法.require "test/unit" class TestAnion < ...

  7. 【学】React的学习之旅4-添加事件(onChange)

    实现联动绑定,在文本框中输入内容的同时,后面的span里内容跟着一起变化: onChange(),一旦触发一次变动,就执行某个函数: 既然已经在getInitialState属性里申明了一个变量inp ...

  8. 远程桌面不能连接,提示awgina.dll取代错误的解决办法

    远程桌面不能连接,错误提示:您不能初始化一个远程桌面连接,因为在远程计算机上的windows登录软件被不兼容的软件c:\windows\system32\awgina.dll取代,如下图所示: 原因: ...

  9. nginx响应时间监控脚本

    最近我们服务的使用方总是反应说我们接口超时,于是做了一个监控脚本,统计最近五分钟的响应情况,并对异常情况发送邮件报警. #!/bin/bash function define(){ ori_log_p ...

  10. 异步|同步&阻塞|非阻塞

    异步|同步:区别在于发出一个功能调用时,是否马上得到返回结果 阻塞|非阻塞:区别在于调用结果返回之前,当前线程是否挂起 node.js:单线程.异步非阻塞模型 单线程与异步不矛盾,与并发是矛盾的 ht ...