文章目录
SpringBoot整合MyBatis
数据库环境配置
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'亚瑟','123456'),(2,'妲己','123456'),(3,'安琪拉','123456');
1、新建一个SpringBoot项目
具体操作见我的上篇博客
2、配置环境
数据访问层;MyBatis 所有的包都是自己的,所以要导入自己的依赖。
- 导入驱动和依赖
<!-- 这是自定义的包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- 编写配置文件
application.yaml
或者application.properties
这两种配置文件都可以,以前大家可能都是用.properties
文件做配置文件的,我这里就两个配置文件都写一个,大家看格式有什么不同,掌握一个也就够了!
application.yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
注意,我这里的空格和缩进都是严格按照规定来的!
application.properties
# 在这里也可以编写一些SpringBoot的配置
#更改tomcat端口号
server.port=9090
# 集成 MyBatis 也可以在这里配置
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
注意:这两个文件可以同时存在并且发挥作用,互补的~
但是同一种操作写入一个文件就行,所以我这里将.properties
里连接数据库的操作注掉了,但是在里面更改了tomcat的端口号,一样能发挥作用!
3、在pojo层编写实体类
/*
Description:实体类
Author:32259
Time:2020三月2020/3/314:01
*/
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
4、在dao层编写Mapper接口
使用注解和xml配置文件都可以
- 注解
/*
Description:接口
Author:32259
Time:2020三月2020/3/313:58
*/
import com.gang.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper//标注这是一个mapper接口
@Repository//代表持久层
public interface UserMapper {
@Select("select * from user")
List<User> getUserLists();
}
- xml文件
/*
Description:接口
Author:32259
Time:2020三月2020/3/313:58
*/
import com.gang.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper//标注这是一个mapper接口
@Repository//代表持久层
public interface UserMapper {
//将注解注掉,使用xml文件
//@Select("select * from user")
List<User> getUserLists();
}
编写Mapper.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">
<!-- 原来我们都是编写具体的执行sql -->
<mapper namespace="com.gang.springboot.mapper.UserMapper">
<select id="getUserLists" resultType="User">
select * from user;
</select>
</mapper>
使用xml文件后要在pom文件里设置资源过滤,因为springboot没有设置.xml文件的资源过滤,会读取不到xml文件,导致报错
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 设置资源过滤:springboot没有设置.xml文件的资源过滤-->
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
还得在application.yaml
中加入一些扫描包的操作
# 这个是配mapper.xml文件时要配底下这几行 扫描包,扫描xml文件
mybatis:
type-aliases-package: com.gang.springboot.pojo
mapper-locations: classpath:com/gang/springboot/mapper/*.xml
hhh,xml配置文件简直太麻烦了,个人建议大家使用注解哈~
注意:两者只能选其一,不然会报错!
5、在controller层编写控制视图跳转的类
/*
Description:控制视图跳转
Author:32259
Time:2020三月2020/3/314:15
*/
import com.gang.springboot.mapper.UserMapper;
import com.gang.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/list")
public List<User> getUserList(){
List<User> userList = userMapper.getUserLists();
return userList;
}
}
最终的一个架构:
5、启动主启动类,查看结果
注意端口号的变化
OK,这样我们数据库表中的数据就被查询到了!
又到了和大家说再见的时候~