前文
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
spring: profiles: active: dev # use properties by application-dev.yml
application-dev.yml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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