博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于spring boot 及mybatis的web开发环境搭建
阅读量:7072 次
发布时间:2019-06-28

本文共 4930 字,大约阅读时间需要 16 分钟。

###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层(当然你可以手写,只是有点麻烦)。

然后在项目下建立如下目录。

这里由于偷懒所以没有写service层代码,在controller层中直接调用了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.测试一下:

ok,大功告成,在各层中添加你的代码吧!

ChangeLog

2018-04-08 完成

以上皆为个人所思所得,如有错误欢迎评论区指正。

欢迎转载,烦请署名并保留原文链接。

联系邮箱:huyanshi2580@gmail.com

更多学习笔记见个人博客------>

你可能感兴趣的文章
LVM 磁盘分区扩容
查看>>
mysql5.6之key_buffer_size优化设置
查看>>
查看Linux服务器网卡流量小脚本shell和Python各一例
查看>>
Linux TC的ifb原理以及ingress流控
查看>>
AgileEAS.NET之敏捷并行开发方法
查看>>
Java源码分析系列之ArrayList读后感
查看>>
性能测试之手机号码python生成方式
查看>>
统计数据库大小的方法
查看>>
PHP递归遍历文件夹
查看>>
用户系列之五:用户SID查看之终结版
查看>>
ubuntu 11.10下载和编译Android源码
查看>>
千兆级LTE的一小步,5G之路的一大步
查看>>
跟我一起写 Makefile(一)
查看>>
管理日志-原创理论工具--技能方格图
查看>>
MPLS TE第一步:创建基本TE隧道
查看>>
windows中禁止U盘写入
查看>>
Bash技巧总结
查看>>
在窗体中添加标签Label、Icon图标
查看>>
Perl脚本学习笔记(一)
查看>>
基于BIND实现DNS的解析、主从、子域、请求转发、访问控制
查看>>