这里使用idea maven 自动配置
一.导入必要的jar仓库
导入jar
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二.导入配置(idea 默认不编译java 包下的 xml,如 mapper .xml)
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
三.整合mybaits .
在配置文件application.yml 下
#\u516C\u5171\u914D\u7F6E
server:
port: 8080
tomcat:
uri-encoding: UTF-8 #tmcat 配置
spring:
#\u89C6\u56FE\u914D\u7F6E
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/ #死都要加上classpath
thymeleaf:
prefix: classpath:/templates/ #thymeleaf 模板配置
suffix: .html
mode: HTML
encoding: UTF-8
cache: false
datasource:
url: jdbc:mysql://localhost:3306/cat-cloud?useUnicode=true&characterEncoding=utf-8 #数据源
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #指定mapper 路径
type-aliases-package: com.unlzh.catcloud.pojo #别名配置
四.文件格式
静态文件默认放在,resources/static 文件夹下
网页文件默认放在,resouces/templates文件夹下
五.注意点
1.mapper 接口必须加入
@mapper ,否则spring 找不到接口 完成不了映射
2.如果没有加入@mapper 注解,需要在启动类中接入@mapperScanner 注解
六.测试
Controller
启动类
package com.unlzh.catcloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.unlzh.catcloud.mapper")
public class CatCloudApplication {
public static void main(String[] args) {
SpringApplication.run(CatCloudApplication.class, args);
}
}
启动后
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20191012205146577.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21hMTIxMzg=,size_16,color_FFFFFF,t_70)