最近项目需要了解一下springboot的使用所以就搭建了一个springboot+mybatis的框架,现在来简单的介绍一下。
前期准备: 我使用的IDE是eclipse,由于springboot的要求1.8JDK+Tomcat9 eclipse的老版本只能使用Tomcat8,所以需要换上最新版本的eclipse。
先建一个maven项目
然后把springboot官网上的pom.xml上的内容拷贝过来。我的是整合版所以有点不一样
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后配置一下springboot的配置文件如下:
application.properties:
#静态文件配置
spring.webflux.static-path-pattern =classpath:/static/**
#数据库配置
spring.datasource.url =jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
spring.datasource.username =root
spring.datasource.password =123456
spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver
#模板配置
spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.jsp
spring.thymeleaf.cache=false
#mybatis 配置
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=entity
#spring.profiles=development
#server.address=127.0.0.1
#端口设置
server.port=8000
然后写一个测试controller:JumpController.java
package com.springBoot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JumpController {
@RequestMapping("/")
String home() {
return "index";
}
}
index 跳转所在位置 (配置文件: spring.thymeleaf.prefix=classpath:/templates/)
然后写一下启动入口:Application.java
package com.springBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* springboot 启动入口
* @author hxl
*
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
然后右击启动入口 run as ===> java application
然后访问:http://localhost:8000/ 就可以看到index的内容了
然后整合mybatis配置已经在上面了 然后其他就和SSM是一样的我就不写了,下面是我这次搭建框架的源码有需要的
可以下载一下 :demo源码