集成Mybatis
(1)引入start(JDBC,MySQl,MyBatis)
<dependency> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
(2)配置数据库
spring.datasource.username=root
spring.datasource.password=159263487qwe
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合MyBatis
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.my.pojo
(3)实体类、mapper
@Mapper
@Repository
public interface UserMapper {
List<User> queryAll ();
User queryById(int id);
int add (User user);
int delete (int id);
int update (User user);
}
(4)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">
<mapper namespace="com.my.mapper.UserMapper">
<select id="queryAll" resultType="User">
select * from user
</select>
<select id="queryById" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
在项目中不知道按到哪里了,出现识别不了指定文件名的mapper.xml(其他名称的mapper.xml可以识别)
解决方案:
一:
看哪个文件类型的File name patterns中有刚才识别不了的mapper.xml,如果有则删除(“选中,点 - ”),然后apply
二、
点开那个文件
设置文件类型
(5)Controller
调用Servce层方法````