1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.导出静态资源解决方案
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yaml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3.配置数据源,连接数据库,配置mybatis的登记xml路径
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true;characterEncoding=utf-8;useSSL=false;serverTimezone=GMT%2B8
username: root
password: 123456
mybatis:
mapper-locations: classpath:com/kxy/mapper/*.xml
config-location:
4.mapper接口
@Mapper
@Repository
public interface UserDao {
List<User> selectAllUser();
}
写mapper接口的时候,要@Mapper标注这是一个mapper类
5.@MapperScan
@SpringBootApplication
@ComponentScan("com.kxy")
@MapperScan("com.kxy.dao")
public class SpringbootMybatisApplication {
@MapperScan扫描mapper类所在的包
6.xml文件的sql编写
7.controller调用mapper接口的方法
将数据返回到视图
@RestController
public class UserController {
@Resource
UserDao userDao;
@GetMapping("/getAll")
public List<User> getAll(){
return userDao.selectAllUser();
}
}