Could not find resource /com/example/demoMyBatis/XML/Student



我得到了一个简单的mybatis实现和两个xml文件:1个用于mapper,1个用于sqlmapconfig。不知怎的,当我运行它时,它一直显示错误。找不到resource.com/example/demoMyBatis/XML/Student.XML。一直在尝试找到类似资源筛选的解决方案,但没有成功。pls帮助

SqlMapConfig.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Student" type="com.example.demoMyBatis.XML.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="12345678" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="/com/example/demoMyBatis/XML/Student.xml" />
</mappers>
</configuration>

Student.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="Student">
<insert id="insertStudent" parameterType="Student" >
INSERT INTO STUDENT (ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{id}, #{name}, #{branch}, #{percentage}, #{phone}, #{email});
</insert>
<update id="updateStudent" parameterType="Student">
UPDATE STUDENT SET EMAIL = #{email}, NAME = #{name}, BRANCH = #{branch}, PERCENTAGE = #{percentage}, PHONE = #{phone} WHERE ID = #{id};
</update>
<delete id="deleteStudentById" parameterType="int">
DELETE from STUDENT WHERE ID = #{id};
</delete>
<select id="selectAllStudent" resultType="Student">
SELECT * FROM STUDENT;
</select>
<select id="selectStudentById" parameterType="int" resultType="Student">
SELECT * FROM STUDENT WHERE ID = #{id};
</select>
<resultMap id = "result" type = "Student">
<result property = "id" column = "ID"/>
<result property = "name" column = "NAME"/>
<result property = "branch" column = "BRANCH"/>
<result property = "percentage" column = "PERCENTAGE"/>
<result property = "phone" column = "PHONE"/>
<result property = "email" column = "EMAIL"/>
</resultMap>
</mapper>

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demoMyBatis-XML</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoMyBatis-XML</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</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>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

以下是根据本教程修改的代码。

  1. 已将映射器命名空间更改为:com.example.mybatisdemo.mapper.StudentMapper
  2. 已将映射程序名称更改为student-mapper.xml
  3. 将student-mapper.xml放入src/main/resources/mapper文件夹
<?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.example.mybatisdemo.mapper.StudentMapper">
<insert id="insertStudent" parameterType="Student" >
INSERT INTO STUDENT (ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{id}, #{name}, #{branch}, #{percentage}, #{phone}, #{email});
</insert>
<update id="updateStudent" parameterType="Student">
UPDATE STUDENT SET EMAIL = #{email}, NAME = #{name}, BRANCH = #{branch}, PERCENTAGE = #{percentage}, PHONE = #{phone} WHERE ID = #{id};
</update>
<delete id="deleteStudentById" parameterType="int">
DELETE from STUDENT WHERE ID = #{id};
</delete>
<select id="selectAllStudents" resultType="Student">
SELECT * FROM STUDENT;
</select>
<select id="selectStudentById" parameterType="int" resultType="Student">
SELECT * FROM STUDENT WHERE ID = #{id};
</select>
<resultMap id = "result" type = "Student">
<result property = "id" column = "ID"/>
<result property = "name" column = "NAME"/>
<result property = "branch" column = "BRANCH"/>
<result property = "percentage" column = "PERCENTAGE"/>
<result property = "phone" column = "PHONE"/>
<result property = "email" column = "EMAIL"/>
</resultMap>
</mapper>
  1. 创建com.example.mybatisdemo.mapper.StudentMapper接口:
@Mapper
public interface StudentMapper {
void insertStudent(Student student);
void updateStudent(Student student);
void deleteStudentById(int id);
List<Student> selectAllStudents();
Student selectStudentById(int id);
}
  1. 创建com.example.mybatisdemo.dao.用lombok研究pojo:
@Data
public class Student {
Integer id;
String name;
String branch;
Integer percentage;
Integer phone;
String email;
}
  1. 使用属性填充配置:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=12345678
mybatis.mapper-locations=classpath:mapper/**/*-mapper.xml
mybatis.type-aliases-package=com.example.mybatisdemo.dao
logging.level.com.example.mybatisdemo.mapper=DEBUG

pom.xml不包含build/resources部分,因为xml映射器是从resources(而不是src(文件夹中使用的。我还没有检查将xml映射器放入src文件夹的可能性。

确保文件Student.xml位于IDE Eclipse/intelliJ/whatever中项目中的文件夹src/main/resources/com/example/demoMyBatis/XML/中。

最新更新