###Spring Boot 是什么 官网对他的介绍是:
Spring Boot 使您能轻松地创建独立的、生产级的、基于 Spring 且能直接运行的应用程序。我们对 Spring 平台和第三方库有自己的看法,所以您从一开始只会遇到极少的麻烦。
我对Spring Boot的理解是:
大家都喜欢使用Spring进行开发,但是配置一个基于Spring 开发的项目是在有些复杂,而Spring Boot所做的工作就是将Spring开发时常用的,主流的框架集合进行二次封装,使得配置工作变得简单。
Spring Boot拥有合理(主流)的默认配置,例如:默认情况下,Spring Boot Web应用程序内嵌了一个Tomcat容器。
当然你也可以禁用其默认配置并自己添加新的配置,这个过程也十分简单。
###环境搭建 前提: 本文使用:Idea + maven.
正式开始!1.首先打开idea,点击File->new->project. 在出现的页面中点击Spring initializr。点击next。
2.在接下来的页面配置你的项目名称信息。点击next。
3.在dependenice页面中,选中web栏下的web,SQL栏下的JPA,Mybatis,MySQL。之后一路点击next完成项目创建。
4.配置pom.xml文件。 我的完整pom.xml为:
复制代码 4.0.0 com.huyan configcenter 0.0.1-SNAPSHOT jar configcenter Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
5.配置application.yml文件。
这是spring boot 的配置文件,在resources目录下新建文件,文件名为:application.yml
.然后在其中配置数据库:
spring: # 数据库配置 datasource: url: jdbc:mysql://localhost:3306/config_center?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false username: {your.username} password: {your.password} driver-class-name: org.gjt.mm.mysql.Driver复制代码
将其中的username和password换成你自己的数据库信息。
6.在你的mysql数据库中新建数据库config_center。
7.此时,在包主目录下找到Application类(名字和你的包名有关系),run,会发现tomcat已经启动成功。
8.在pox.xml中已经加入了对mybatis-spring-boot-starter的依赖,所以我们只需要在yml文件中添加以下内容。
# mybatis配置mybatis: # 配置映射类所在包名 type-aliases-package: panfeng.configcenter.model # 配置mapper xml文件所在路径 mapper-locations: classpath:mapper/**.xml复制代码
9.好了,现在基于spring boot 及mybatis的web环境已经搭建完成。让我们来测试以下。 首先,在数据库中添加一张表,我添加了一张名为ldap_account的表。然后使用myvatis generator自动生成与之对应的model,mapper层(当然你可以手写,只是有点麻烦)。
然后在项目下建立如下目录。
StatusController代码:
package com.huyan.configcenter.controller;import com.alibaba.fastjson.JSONObject;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class StatusController { @RequestMapping(value = "/status", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8"}) @ResponseBody public JSONObject status() { JSONObject jsonObject = new JSONObject(); jsonObject.put("server_name", "config-center"); jsonObject.put("status", "ok"); return jsonObject; }}复制代码
AccountController代码:
package com.huyan.configcenter.controller;import com.alibaba.fastjson.JSONObject;import com.huyan.configcenter.mapper.LdapAccountMapper;import com.huyan.configcenter.model.LdapAccount;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class AccountController { @Resource private LdapAccountMapper ldapAccountMapper; @RequestMapping(value = "/account}", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8"}) @ResponseBody public JSONObject status() { JSONObject jsonObject = new JSONObject(); LdapAccount ldapAccount = ldapAccountMapper.selectByPrimaryKey(1); jsonObject.put("id", ldapAccount.getId()); jsonObject.put("name", ldapAccount.getLdapName()); jsonObject.put("phone",ldapAccount.getLdapPhine()); return jsonObject; }}复制代码
10.测试一下:
ChangeLog
2018-04-08 完成以上皆为个人所思所得,如有错误欢迎评论区指正。
欢迎转载,烦请署名并保留原文链接。
联系邮箱:huyanshi2580@gmail.com
更多学习笔记见个人博客------>