13 Spring Boot:
SpringBoot基础
五、SpringBoot与整合其他技术
5.1 SpringBoot整合Mybatis
MySQL数据库版本: 8.0.17
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
1、添加Mybatis的起步依赖
2、添加数据库驱动坐标
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpingBoot集成junit测试的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!-- MySQL连接驱动 --> <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>
3、添加数据库连接信息
在 application.properties 中添加数据量的连接信息
project Encoding
为了避免.properties文件中文乱码,请按参考资料配置编码格式为UTF-8
#数据库连接信息 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root
在itheima数据库中创建user表
使用Navicat图形化界面连接软件
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
插入测试数据
-- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
4、 在domain包:创建实体类
package com.itheima.domain; public class User { private Long id; private String username; private String password; private String name; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } //此处省略getter和setter方法
5、 编写DAO层的接口类
IUserDao
package com.itheima.mapper; import com.itheima.domain.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface IUserDao { public List<User> queryUserList(); }
6、配置Mapper映射文件
在src/main/resources/com/itheima/mapper路径下加入UserMapper.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" > <mapper namespace="com.itheima.mapper.IUserDao"> <select id="queryUserList" resultType="user"> select * from user </select> </mapper>
7、在application.properties中添加mybatis的信息
#配置mybatis的信息 #spring集成Mybatis环境 #pojo别名扫描包 mybatis.type-aliases-package=com.itheima.domain #加载Mybatis映射文件 mybatis.mapper-locations=classpath:com/itheima/mapper/*Mapper.xml
通过测试证明:spring boot 集成mybatis框架【DAO层接口+XML配置文件】 在接口的public的关键字上一行加注解 @Mapper
接口的文件名不一定必须和XML映射配置文件名一致,因为spring boot的 application*.properties里面会进行扫描包的配置。
===================
参考资料:
end