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. 结对编程--基于android平台的黄金点游戏

    游戏内容: 阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫“黄金点”的游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或1 ...

  2. cps变换

    网上看了很多内容,很少有给出一个准确的概念,它的英文全称是continuous passing style, 直译为连续传递样式,那么cps transform就是将一些原本不是continuous ...

  3. Python底层socket库

    Python底层socket库将Unix关于网络通信的系统调用对象化处理,是底层函数的高级封装,socket()函数返回一个套接字,它的方法实现了各种套接字系统调用.read与write与Python ...

  4. How to create/restore a slave using GTID replication in MySQL 5.6

    MySQL 5.6 is GA! Now we have new things to play with and in my personal opinion the most interesting ...

  5. statsd+graphite

    一些观点: Statsd:一个nodejs的客户端,用于向graphite的收集器发送数据,使用各类编程语言的客户端响起发送timer,counter等统计数据后,其通过udp定时向graphite发 ...

  6. 【转】libevent和基于libevent的网络编程

    转自: http://www.cnblogs.com/nearmeng/p/4043548.html 1 libevent介绍和安装 介绍 libevent是一个轻量级的基于事件驱动的高性能的开源网络 ...

  7. kali 初始化

    关于kali使用前的一些配置,网上有很多版本,但是几乎都很雷同,或者是不全,或者是根本就没有测试过,或者是有的方法是错的(换句话说是版本变化的差异),因此让很多人接触kali时百度无数,效果一般,浪费 ...

  8. RDLC An unexpected error occurred while compiling expressions. Native compiler return value: '-1073741511'

    One of my web project, which has a rdlc file using some expressions, was working fine while developi ...

  9. mybatis实战教程(mybatis in action)之十:mybatis SqlSessionSupport 的使用,构件DAO 层的应用

    前面的系列mybatis 文章,已经基本讲到了mybatis的操作,但都是基于mapper隐射操作的,在mybatis 3中这个mapper 接口貌似充当了以前在ibatis 2中的 DAO 层的作用 ...

  10. js预解析及特效

    预解析: // 作用域: // 域:空间.范围.区域…… // 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 {} 浏览器: “JS解析器” 1)“找一些东西” :var ...