一、MyBatis配置以及启动步骤
- 导入依赖
- 设置表,根据表创建实现类
- 写mapper接口文件,写对应的mapper.xml(映射文件)
- 在application.yml编写mybatis配置信息(驱动,url,用户名,密码,连接池等)
- 启动类上加上@MapperScan
- Controller调用service,service调用mapper
- 启动测试
---- 后续 再写 ----
只需改动第3,6步
二、resources文件夹下的mapper文件夹下的.xml配置信息:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.mapper.UserMapper">
<select id="selectUserById" parameterType="int" resultType="com.qf.entity.User">
select * from tb_user where id = #{id}
</select>
</mapper>
三、application.yml中配置信息
mybatis:
mapper-locations: classpath:mapper/*.xml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java2313数据库库名?serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
logging:
level:
com.qf.mapper: DEBUG