前文

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. js动态添加控件(输入框为例)

    写在前面 昨天得到一个需求,需要在账户登记页面中动态添加输入框,经过半天的捣鼓,最终完美成型,写下来跟大家分享下, 供大家参考 开始复制代码了 如果复制了我所有代码的话,注意看js最后面方法的备注,最 ...

  2. centos6下yum安装mariadb数据库的解决方法

    在centos6下Yum安装mariadb数据库时老是提示无法正常安装,看错误日志才发现,是没有mariadb release源文件在/etc/yum.repos.d/中,为此,我特意在新建文件: # ...

  3. javac & java

    # 没有 package, 没有 import 的情况 * 源文件 public class HelloWorld{ public static void main(String[] args){ S ...

  4. 接口数据转json格式

    接口数据转json格式 function tojson($result, $callback = null){ header('Content-Type:text/html; charset=utf- ...

  5. Python——import与reload模块的区别

     原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 语法不同 import sys reload('sys') 2. 导入特性不同 import 和reload都可以对同一个模块多次加载, ...

  6. 安装Angulr CLI

    参考文档 https://www.jianshu.com/p/327d88284abb http://blog.csdn.net/zhy13087344578/article/details/6074 ...

  7. BZOJ2144 跳跳棋[建模+LCA]

    思维题,思路比较神仙. 个人思路过程:个人只想到了只要中间棋子开始向外跳了,以后就不应该向内跳了,这样很蠢.所以应该要么先向内跳一会,要么直接开始中间的向外跳.不知道怎么处理,就卡住了. 20pts: ...

  8. AJAX增删查

    数据库 CREATE DATABASE crmweb; CREATE TABLE `customerstatus` ( `id` bigint(20) NOT NULL AUTO_INCREMENT ...

  9. HDU 6034 - Balala Power! | 2017 Multi-University Training Contest 1

    /* HDU 6034 - Balala Power! [ 大数进位,贪心 ] 题意: 给一组字符串(小写英文字母),将上面的字符串考虑成26进制数,每个字母分配一个权值,问这组数字加起来的和最大是多 ...

  10. html5文件夹上传下载组件

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...