Maven Plugin管理

通常,让你的Maven POM文件继承 spring-boot-starter-parent,并声明一个或多个 Starter POMs依赖即可。

spring-boot-starter-parent
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.6.RELEASE</version>
  5. </parent>
其他 Starter POMs依赖
  1. <!-- spring-boot的web启动的jar包 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-devtools</artifactId>
  13. <optional>true</optional>
  14. <scope>true</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-data-jpa</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-redis</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-aop</artifactId>
  27. </dependency>
  28. <!-- Spring Boot 集成MyBatis -->
  29. <dependency>
  30. <groupId>org.mybatis.spring.boot</groupId>
  31. <artifactId>mybatis-spring-boot-starter</artifactId>
  32. <version>1.3.1</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.github.pagehelper</groupId>
  36. <artifactId>pagehelper-spring-boot-starter</artifactId>
  37. <version>1.0.0</version>
  38. </dependency>

application.properties编写

  1. # 驱动配置信息
  2. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  3. spring.datasource.url = jdbc:mysql://127.0.0.1:3306/goku_db
  4. spring.datasource.username = root
  5. spring.datasource.password = root
  6. spring.datasource.driverClassName = com.mysql.jdbc.Driver
  7.  
  8. #连接池的配置信息
  9. spring.datasource.initialSize=5
  10. spring.datasource.minIdle=5
  11. spring.datasource.maxActive=20
  12. spring.datasource.maxWait=60000
  13. spring.datasource.timeBetweenEvictionRunsMillis=60000
  14. spring.datasource.minEvictableIdleTimeMillis=300000
  15. spring.datasource.validationQuery=SELECT 1
  16. spring.datasource.testWhileIdle=true
  17. spring.datasource.testOnBorrow=false
  18. spring.datasource.testOnReturn=false
  19. spring.datasource.poolPreparedStatements=true
  20. spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
  21. spring.datasource.filters=stat,wall,log4j
  22. spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  23.  
  24. # mybatis
  25. mybatis.type-aliases-package=com.goku.webapi.model
  26. mybatis.mapper-locations=classpath:mapping/**/*.xml
  27.  
  28. # Mapper
  29. mapper.mappers=com.goku.webapi.mapper
  30. mapper.not-empty=false
  31. mapper.identity=MYSQL
  32.  
  33. #pagehelper
  34. pagehelper.helperDialect=mysql
  35. pagehelper.reasonable=true
  36. pagehelper.supportMethodsArguments=true
  37. pagehelper.params=count=countSql
  38.  
  39. # Redis
  40. spring.redis.database=0
  41. spring.redis.host=127.0.0.1
  42. spring.redis.port=6379
  43. spring.redis.password=
  44. spring.redis.pool.max-active=8
  45. spring.redis.pool.max-wait=-1
  46. spring.redis.pool.max-idle=8
  47. spring.redis.pool.min-idle=0
  48. spring.redis.timeout=0

config配置类编写

数据库配置

  1. ackage com.goku.webapi.config;
  2.  
  3. import com.alibaba.druid.pool.DruidDataSource;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.mybatis.spring.SqlSessionFactoryBean;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Primary;
  13. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  14. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  15.  
  16. import java.sql.SQLException;
  17. import javax.sql.DataSource;
  18.  
  19. /**
  20. * Created by nbfujx on 2017/10/19.
  21. */
  22. @Configuration
  23. public class DruidDataBaseConfig {
  24.  
  25. private Logger logger = LoggerFactory.getLogger(DruidDataBaseConfig.class);
  26.  
  27. @Value("${spring.datasource.url}")
  28. private String dbUrl;
  29.  
  30. @Value("${spring.datasource.username}")
  31. private String username;
  32.  
  33. @Value("${spring.datasource.password}")
  34. private String password;
  35.  
  36. @Value("${spring.datasource.driverClassName}")
  37. private String driverClassName;
  38.  
  39. @Value("${spring.datasource.initialSize}")
  40. private int initialSize;
  41.  
  42. @Value("${spring.datasource.minIdle}")
  43. private int minIdle;
  44.  
  45. @Value("${spring.datasource.maxActive}")
  46. private int maxActive;
  47.  
  48. @Value("${spring.datasource.maxWait}")
  49. private int maxWait;
  50.  
  51. @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
  52. private int timeBetweenEvictionRunsMillis;
  53.  
  54. @Value("${spring.datasource.minEvictableIdleTimeMillis}")
  55. private int minEvictableIdleTimeMillis;
  56.  
  57. @Value("${spring.datasource.validationQuery}")
  58. private String validationQuery;
  59.  
  60. @Value("${spring.datasource.testWhileIdle}")
  61. private boolean testWhileIdle;
  62.  
  63. @Value("${spring.datasource.testOnBorrow}")
  64. private boolean testOnBorrow;
  65.  
  66. @Value("${spring.datasource.testOnReturn}")
  67. private boolean testOnReturn;
  68.  
  69. @Value("${spring.datasource.poolPreparedStatements}")
  70. private boolean poolPreparedStatements;
  71.  
  72. @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
  73. private int maxPoolPreparedStatementPerConnectionSize;
  74.  
  75. @Value("${spring.datasource.filters}")
  76. private String filters;
  77.  
  78. @Value("{spring.datasource.connectionProperties}")
  79. private String connectionProperties;
  80.  
  81. @Bean //声明其为Bean实例
  82. @Primary //在同样的DataSource中,首先使用被标注的DataSource
  83. public DataSource dataSource(){
  84. DruidDataSource datasource = new DruidDataSource();
  85.  
  86. datasource.setUrl(this.dbUrl);
  87. datasource.setUsername(username);
  88. datasource.setPassword(password);
  89. datasource.setDriverClassName(driverClassName);
  90.  
  91. //configuration
  92. datasource.setInitialSize(initialSize);
  93. datasource.setMinIdle(minIdle);
  94. datasource.setMaxActive(maxActive);
  95. datasource.setMaxWait(maxWait);
  96. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  97. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  98. datasource.setValidationQuery(validationQuery);
  99. datasource.setTestWhileIdle(testWhileIdle);
  100. datasource.setTestOnBorrow(testOnBorrow);
  101. datasource.setTestOnReturn(testOnReturn);
  102. datasource.setPoolPreparedStatements(poolPreparedStatements);
  103. datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  104. try {
  105. datasource.setFilters(filters);
  106. } catch (SQLException e) {
  107. logger.error("druid configuration initialization filter", e);
  108. }
  109. datasource.setConnectionProperties(connectionProperties);
  110.  
  111. return datasource;
  112. }
  113.  
  114. @Bean
  115. @Primary
  116. //配置事物管理
  117. public DataSourceTransactionManager masterTransactionManager(){
  118. return new DataSourceTransactionManager(dataSource());
  119. }
  120. }

redis配置

  1. package com.goku.webapi.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.annotation.CachingConfigurerSupport;
  6. import org.springframework.cache.annotation.EnableCaching;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.redis.cache.RedisCacheManager;
  10. import org.springframework.data.redis.connection.RedisConnectionFactory;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.core.StringRedisTemplate;
  13. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  14.  
  15. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  16. import com.fasterxml.jackson.annotation.PropertyAccessor;
  17. import com.fasterxml.jackson.databind.ObjectMapper;
  18.  
  19. /**
  20. * Created by nbfujx on 2017/10/19.
  21. */
  22. @Configuration
  23. @EnableCaching
  24. public class RedisCacheConfig extends CachingConfigurerSupport {
  25.  
  26. @Value("${spring.redis.host}")
  27. private String host;
  28. @Value("${spring.redis.port}")
  29. private int port;
  30. @Value("${spring.redis.timeout}")
  31. private int timeout;
  32.  
  33. //缓存管理器
  34. @Bean
  35. public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
  36. RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
  37. //设置缓存过期时间
  38. cacheManager.setDefaultExpiration(10000);
  39. return cacheManager;
  40. }
  41.  
  42. @Bean
  43. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
  44. StringRedisTemplate template = new StringRedisTemplate(factory);
  45. setSerializer(template);//设置序列化工具
  46. template.afterPropertiesSet();
  47. return template;
  48. }
  49.  
  50. private void setSerializer(StringRedisTemplate template){
  51. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  52. ObjectMapper om = new ObjectMapper();
  53. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  54. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  55. jackson2JsonRedisSerializer.setObjectMapper(om);
  56. template.setValueSerializer(jackson2JsonRedisSerializer);
  57. }
  58.  
  59. }

SpringApplication启动类编写

  1. package com.goku.webapi;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.web.servlet.ServletComponentScan;
  7. import org.springframework.context.annotation.ComponentScan;
  8.  
  9. /**
  10. * Created by nbfujx on 2017/10/19.
  11. */
  12. // Spring Boot 应用的标识
  13. @SpringBootApplication
  14. @ServletComponentScan
  15. @MapperScan("com.goku.webapi.mapper")
  16. public class WebapiApplication {
  17.  
  18. public static void main(String[] args) {
  19. // 程序启动入口
  20. // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
  21. SpringApplication.run(WebapiApplication.class,args);
  22. }
  23. }

编写其他相关业务类

model,mapper,service,controller

运行启动程序

查看运行效果

查看druid数据源监控

GITHUB

github :https://github.com/nbfujx/learn-java-demo/tree/master/Goku.WebService.Simple.Single

SpringBoot搭建基于Spring+SpringMvc+Mybatis的REST服务的更多相关文章

  1. Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构

    2018-08-16 09:27 更新 强烈推荐使用Springboot来搭建MVC框架! 强烈推荐使用Springboot来搭建MVC框架! 强烈推荐使用Springboot来搭建MVC框架! 后文 ...

  2. 基于Spring+SpringMVC+Mybatis的Web系统搭建

    系统搭建的配置大同小异,本文在前人的基础上做了些许的改动,重写数据库,增加依据权限的动态菜单的实现,也增加了后台返回json格式数据的配置,详细参见完整源码. 主要的后端架构:Spring+Sprin ...

  3. Maven+SSM框架搭建【spring+springmvc+mybatis】

    本案例用到:ssm[spring+springmvc+mybatis]框架 数据库:mysql (推荐使用mysql 或者 sqlserver  .oracle太大,一般大型项目才会用到) 开发工具: ...

  4. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  5. 搭建springboot的ssm(spring + springmvc + mybatis)的maven项目

    最终项目目录结构 创建过程 1.创建开关SpringBootApplication 为了创建快速.我们使用idea自带的创建springboot来创建结构,当然创建普通的web项目也是可以的.(使用e ...

  6. javaweb项目-医者天下 (Spring+SpringMVC+MyBatis)

    项目下载地址:http://download.csdn.net/detail/qq_33599520/9826683 项目完整结构图: 项目简介: 医者天下项目是一个基于Spring+SpringMV ...

  7. Spring+SpringMVC+MyBatis+easyUI整合

    进阶篇 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API 优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化 ...

  8. Spring+SpringMVC+MyBatis+easyUI整合基础篇

    基础篇 Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简介 Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试 Spring+S ...

  9. Spring+SpringMVC+MyBatis整合(easyUI、AdminLte3)

    实战篇(付费教程) 花了几天的时间,做了一个网站小 Demo,最终效果也与此网站类似.以下是这次实战项目的 Demo 演示. 登录页: 富文本编辑页: 图片上传: 退出登录: SSM 搭建精美实用的管 ...

随机推荐

  1. Java 项目管理工具 - Maven

    类似于 PHP 中的 Composer,NodeJS 中的 npm,Java 用 Maven 来管理依赖关系. 实际上,Maven 负责管理 Java 项目开发过程中的几乎所有的东西: 版本控制:Ma ...

  2. 【HANA系列】SAP HANA 2.0简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA 2.0简介   ...

  3. 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...

  4. ansible-playbook -l 选项

    -l <SUBSET>, --limit <SUBSET> further limit selected hosts to an additional pattern 限制脚本 ...

  5. python下对文件的操作(非目录)

    总文件夹 子文件夹01 文档01.txt-------------------------------------------------------------------------------- ...

  6. 由于;引发的Oracle的BadSqlExecption

    一.在使用单引号进行各种转换合并的时候发生的异常: 1:首先单引号的表示符为 ' 在Oracle中如果SQL语句带上了分号是会报错的, 在SQL传入进行执行的时候会报BadSQLExecption,这 ...

  7. 列表、元组和range

    小知识点 s = " 5 " print(int(s)) print(s.replace(" ","")) 结果: 5 5 id()#获取对 ...

  8. ajax后台请求两种方法(js和jQuery)

    (1)js的ajax var xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ...

  9. 声明对象的方式/构造函数/原型/this指向

      函数的发展历程(声明函数的方式):     1.通过Object构造函数或字面量的方式创建单个对象 var obj = new Object; obj.name="新华"; o ...

  10. java创建对象的5种方法

    java是面向对象的,所以在使用中经常会去创建对象,而我们一般创建对象只会使用new关键字去创建,这里给大家总结一下在java中创建对象的5中方法: 使用new关键字 } → 调用了构造函数 使用Cl ...