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. iOS_Swift初识之使用三种回调方式自定义Button

    最近在学习Swift ,发现青玉伏案大神早期用OC写的一篇博客--IOS开发之自定义Button(集成三种回调模式)  很适合用来熟悉Swift的回调方式,于是我就用Swift翻版了一下,具体实现原理 ...

  2. Bridge.NET

    块作用域闭包问题 结果正确:1 容易引入JSB:1 public class Program { static List<Action> createActions() { List< ...

  3. js 常用方法

    1: 五秒后跳出提示框 setTimeout("alert('已经过了五秒了')",5000) 2:  十秒后自动关闭窗口 <script language="ja ...

  4. [转]js来弹出窗口的详细说明

    1.警告对话框 <script> alert("警告文字") </script> 2.确认对话框 <script> confirm(" ...

  5. MapWinGIS.ocx 注册

    (1)不管对版本4.8还是4.9.3,运行环境都必须为32位的.Net Frame 3.5,低了高了都不行,会导致注册OCX失败. (2)对于MapWinGIS.ocx 4.8 版本,需要32位的 M ...

  6. git diff提示filemode发生改变(old mode 100644、new mode 10075)

    今天clone代码,git status显示修改了大量文件,git diff提示filemode变化,如下: diff --git a/Android.mk b/Android.mkold mode ...

  7. UISegmentedControl和UIStepper的使用

    UISegmentedControl:分栏控件,常用的属性和方法是 1.tintColor:控制分栏控件的颜色风格 2.insertSegmentWithTitle(Image):插入分栏标题(图片) ...

  8. 将JSON格式的时间/Date(2367828670431)/格式 转为正常的年-月-日 格式

    function formatDate(NewDtime) var dt = new Date(parseInt(NewDtime.slice(6, 19))); var year = dt.getF ...

  9. Mybatis原理分析之一:从JDBC到Mybatis

    1.引言 本文主要讲解JDBC怎么演变到Mybatis的渐变过程,重点讲解了为什么要将JDBC封装成Mybaits这样一个持久层框架.再而论述Mybatis作为一个数据持久层框架本身有待改进之处. 2 ...

  10. 结算凭证中委托付款部分sql

    select a.makevdate,a.summary,a.totalcredit,a.cent_typeid,c.accidname from fts_voucher a,fts_voucher_ ...