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. Live2D WebGL实现

    demo预览:http://www.kakinuma.date/l2d.html 官方:http://www.live2d.com/en/ sdk下载:https://link.zhihu.com/? ...

  2. C#序列化与反序列化方式简单总结

    序列化和反序列化 相关类: System.SerializableAttribute特性(或称为属性), System.Runtime.Serialization.ISerializable(自定义序 ...

  3. Qt开发中的实用笔记一--xml,Qpainter,Delegate:

    因为开发环境不能联网,开发中用到有用的知识就记在word稳定中,不知不觉就记载了几十页,为避免笔记丢失,现在就一点点忘博客上搬,方便日后回顾! ---------------------------- ...

  4. undefined reference to `dlopen'

    g++ -O0 -g3 -I. -Ithird/json -Ithird/core/include -Ithird/vite/include -Ithird/openfst-1.2.10/src/in ...

  5. [java] jsoup 解析网页获取省市区域信息

    到国家统计局抓取数据, 到该class下解析数据 /** * jsoup解析网页 * @author xwolf * @date 2016-12-13 18:11 * @since V1.0.0 */ ...

  6. kali 初始化

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

  7. Spring Security 3整合CAS 实现SSO

    spring security 3整合cas client用于实现各Application之间的单点登录. 1. 需要准备的jar spring-security-core-3.0.8.RELEASE ...

  8. FLASH结构

    mergedir/code.tmp.bin 压缩未加密文件的FLASH文件 mergedir/sfs/nvm.bin   shareData数据文件,未加密.未压缩 数据克隆:DATA/ALL APP ...

  9. 在UIView上添加tableView设置代理属性

  10. mybatis的动态sql及模糊查询

    1.动态sql 使用类似于jstl表达式来实现 2.模糊查找 用一个对象来封装条件 步骤: 1)新建一个条件实体 package com.hy.mybatis.entity; public class ...