前文

1、为什么使用springBoot

众所周知,spring是Java在搭建后台时非常实用的框架,其整合了市场上几乎所有的主流框架于一体,使后端编程更加高效、快速;

而SpringBoot更是把spring的优势发挥到了极致,声称零配置,简化版spring,只需要使用几行代码,是一个微服务框架。

它确实做到了,这也是他的牛逼之处,成功不是没有道理。

搭载环境

1、IDE工具:Eclipse

2、JDK版本:1.8+

3、持久层:Mybaits

4、数据库:Mysql

步骤

【01】创建一个简单的maven项目(创建普通项目就可以,可以打包成jar,毕竟SpringBoot是一个微服务框架)

如果项目没有 src/main/resources文件夹,请按照链接所示创建文件夹:https://www.cnblogs.com/zhangyuanqiang/p/9183908.html

【02】加入Springboot、Mybaits、mysql依赖和打包设置,JDK版本

<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>1026</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springBoot</name> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Spingboot相关jar包版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Springboot核心jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- web开发包:包含Tomcat和Springmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- mysql jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- mybatis-spring-boot jar包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> </dependencies>
<build>
<plugins>
<!-- 打包mainClass设置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.springBoot.App</mainClass>
</configuration>
</plugin>
<!--JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>

  

【03】设置SpringBoot启动类(这里使用创建初始的类作用启动类),启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("XXXX") 

注意:下文App.java为启动类,在com.springBoot这个包下面,之后新建的controller、entity、service都要在这个包下面,不然会报异常:Class not found

【03】创建配置文件

不使用application.properties文件 而使用更加简洁的application.yml文件。创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容(-dev是开发环境配置,后续可以新建-prod生产环境配置,通过active节点切换,便于项目管理):

application.yml:

spring:
profiles:
active: dev

application-dev.yml(已经设置mysql连接、log日志、mybatis的mapper位置和实体类别名):

server:
port: 8078
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8 # springboot-mybatis dateSource options
spring:
datasource:
username: root
password: HZBhzb1237763522-1
url: jdbc:mysql://localhost:3306/TestDB?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver # mybatis mapping propreties
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.springBoot.domain # showSql
logging:
level:
com:
springBoot:
mapper : debug

【04】完整的目录结构

完整代码:

App.java

package com.springBoot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
*项目启动类
*/ @MapperScan("com.springBoot.mapper") //扫描的mapper
@SpringBootApplication
public class App extends SpringBootServletInitializer
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args); //关键代码
} @Override//为了打包springboot项目
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}

UserController.java

package com.springBoot.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.springBoot.domain.UserBean;
import com.springBoot.service.UserService;
@SuppressWarnings("all")
@Controller
@RequestMapping(value="/springBoot")
public class UserController { @Autowired
UserService UserServiceImpl; @RequestMapping(value="/User")
@ResponseBody
public void hello(HttpServletRequest request,HttpServletResponse response) throws Exception{
String Name=request.getParameter("UserName");
UserBean user=UserServiceImpl.checkUser(Name,"");
if(null==user)
response.getWriter().write("Login was failed!");
else
response.getWriter().write("Login was successed!");
} }

UserBean.java

package com.springBoot.domain;

public class UserBean {
private String rowid;
private String col_userName;
private String col_password;
private String col_email;
private String col_createdOn;
private String col_createdBy;
private String col_modifiedOn;
private String col_modifiedBy; public String getRowid() {
return rowid;
}
public void setRowid(String rowid) {
this.rowid = rowid;
}
public String getCol_userName() {
return col_userName;
}
public void setCol_userName(String col_userName) {
this.col_userName = col_userName;
}
public String getCol_password() {
return col_password;
}
public void setCol_password(String col_password) {
this.col_password = col_password;
}
public String getCol_email() {
return col_email;
}
public void setCol_email(String col_email) {
this.col_email = col_email;
}
public String getCol_createdOn() {
return col_createdOn;
}
public void setCol_createdOn(String col_createdOn) {
this.col_createdOn = col_createdOn;
}
public String getCol_createdBy() {
return col_createdBy;
}
public void setCol_createdBy(String col_createdBy) {
this.col_createdBy = col_createdBy;
}
public String getCol_modifiedOn() {
return col_modifiedOn;
}
public void setCol_modifiedOn(String col_modifiedOn) {
this.col_modifiedOn = col_modifiedOn;
}
public String getCol_modifiedBy() {
return col_modifiedBy;
}
public void setCol_modifiedBy(String col_modifiedBy) {
this.col_modifiedBy = col_modifiedBy;
}
@Override
public String toString() {
return "UserBean [rowid=" + rowid + ", col_userName=" + col_userName + ", col_password=" + col_password
+ ", col_email=" + col_email + ", col_createdOn=" + col_createdOn + ", col_createdBy=" + col_createdBy
+ ", col_modifiedOn=" + col_modifiedOn + ", col_modifiedBy=" + col_modifiedBy + "]";
} }

UserMapper.java

package com.springBoot.mapper;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service; import com.springBoot.domain.UserBean;
@Service
public interface UserMapper {
/**
* check user
* @param userName
* @param password
* @param UserBean object
* */
UserBean checkUser(@Param(value="userName") String userName,@Param(value="password") String password);
}

UserService.java

package com.springBoot.service;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; import com.springBoot.domain.UserBean;
public interface UserService {
/**
* check user
* @param userName
* @param password
* @param UserBean object
* */
UserBean checkUser( String userName,String password);
}

UserServiceImpl.java

package com.springBoot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.springBoot.domain.UserBean;
import com.springBoot.mapper.UserMapper;
import com.springBoot.service.UserService;
@Service
public class UserServiceImpl implements UserService { @Autowired
UserMapper userMapper; @Override
public UserBean checkUser(String userName, String password) {
// TODO Auto-generated method stub
return userMapper.checkUser(userName, password);
} }

ClientMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.springBoot.mapper.UserMapper">
<resultMap type="UserBean" id="ClientList">
<id property="rowid" column="rowid" />
<result property="col_userName" column="col_userName" />
<result property="col_password" column="col_password" />
<result property="col_email" column="col_email" />
<result property="col_createdOn" column="col_createdOn" />
<result property="col_createdBy" column="col_createdBy" />
<result property="col_modifiedOn" column="col_modifiedOn" />
<result property="col_modifiedBy" column="col_modifiedBy" /> </resultMap> <!-- 查询 -->
<select id="checkUser" resultMap="ClientList" >
select *
from tb_user
WHERE
col_userName=#{userName}
</select> </mapper>

application.yml

spring:
profiles:
active: dev # use properties by application-dev.yml

application-dev.yml

server:
port: 8078
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8 # springboot-mybatis dateSource options
spring:
datasource:
username: root
password: HZBhzb1237763522-1
url: jdbc:mysql://39.108.217.125:3306/TestDB?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver # mybatis mapping propreties
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.springBoot.domain # showSql
logging:
level:
com:
springBoot:
mapper : debug

测试:

http://localhost:8078/springBoot/User?UserName=root

springBoot整合spring、springMVC、mybatis的更多相关文章

  1. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

  2. maven项目快速搭建SSM框架(一)创建maven项目,SSM框架整合,Spring+Springmvc+Mybatis

    首先了解服务器开发的三层架构,分配相应的任务,这样就能明确目标,根据相应的需求去编写相应的操作. 服务器开发,大致分为三层,分别是: 表现层 业务层 持久层 我们用到的框架分别是Spring+Spri ...

  3. eclipse整合spring+springMVC+Mybatis

    一.新建Maven项目 点击菜单栏File项,选择New->Project,选中Maven Project,如下图: 二.配置pom.xml <?xml version="1.0 ...

  4. SSM框架整合(Spring+SpringMVC+Mybatis)

    第一步:创建maven项目并完善项目结构  第二步:相关配置 pom.xml 引入相关jar包 1 <properties> 2 <project.build.sourceEncod ...

  5. 使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置

    1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发. 上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html ...

  6. 整合spring+springmvc+mybatis

    开发环境: jdk 1.8 eclipse 4.7.0 (Oxygen) tomcat 8.5.29 mysql 5.7 开发前准备: spring 框架的jar包,在这里使用的是spring-5.0 ...

  7. shiro与Web项目整合-Spring+SpringMVC+Mybatis+Shiro(八)

    Jar包

  8. Spring+SpringMVC+MyBatis+easyUI整合

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

  9. Spring+SpringMVC+MyBatis+easyUI整合优化篇

    优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log Spring+SpringMVC+MyBatis+easyUI整合优化篇 ...

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

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

随机推荐

  1. WiFi 6是否适合个人用户

    5G已经被热炒得家喻户晓,但与其对标的WiFi新技术标准——Wi-Fi 6却可能有着更好的经济和技术前景,WiFi 6也称为802.11ax,比过去的Wi-Fi技术更好,速度更快能跟5G对标,连接性更 ...

  2. vim复制到剪切板

    作者:whinc链接:https://www.zhihu.com/question/19863631/answer/89354508来源:知乎 转载文章 Vim 中的复制.删除的内容都会被存放到默认( ...

  3. Jmeter (二) 参数化

    一.数据 用户 参数化 1.添加 用户参数 添加——>前置处理器 ——>用户参数 2.设置 目标参数 3.变量代替 ${name} 4.线程组 设置循环次数,查看结果数中查看结果 thre ...

  4. Selenium(5)

    一.WebDriver结合Junit的使用 1.Junit中常用的断言 (1)assertEquals:断言实际结果与预期结果是否相等 Equals:相等 格式:assertEquals(预期值,实际 ...

  5. 题解 POJ1149 Pigs

    先翻译一下吧(题面可以在原OJ上找) Mirko在一个由M个锁着的猪舍组成的养猪场工作,Mirko无法解锁任何猪舍,因为他没有钥匙.客户纷纷来到农场.他们每个人都有一些猪舍的钥匙,并想购买一定数量的猪 ...

  6. hdu 6088 Rikka with Rock-paper-scissors (2017 多校第五场 1004) 【组合数学 + 数论 + 模意义下的FFT】

    题目链接 首先利用组合数学知识,枚举两人的总胜场数容易得到 这还不是卷积的形式,直接搞的话复杂度大概是O(n^2)的,肯定会TLE.但似乎和卷积有点像?想半天没想出来..多谢Q巨提醒,才知道可以用下面 ...

  7. HTTP的options方法作用

    1.HTTP的options方法作用 检测服务器所支持的请求方法.(比如:‘/user'路由支持哪些方法:get.post.delete...) CORS中的预检请求(检测某个接口是否支持跨域) 2. ...

  8. [pwnable.kr]--alloca

    0x00: 好久没玩了...去年十月以后就没玩过了TAT 这几天把peach的坑,winafl的坑填了下,就来搞下pwn. 0x01: 这个程序是给了源码的 #include <stdio.h& ...

  9. JVM(八),垃圾回收标记算法

    八.垃圾回收标记算法 1.对象被判定成垃圾的标准 没有被其他对象引用 2.判断对象是否为垃圾的算法 (1)引用计数法 优点and缺点 (2)可达性分析算法

  10. CUDA-F-2-0-CUDA编程模型概述1

    Abstract: 本文介绍CUDA编程模型的简要结构,包括写一个简单的可执行的CUDA程序,一个正确的CUDA核函数,以及相应的调整设置内存,线程来正确的运行程序. Keywords: CUDA编程 ...