i got a simple mybatis implement and 2 xml file 1 for mapper and 1 for sqlmapconfig.Somehow when i run it keep showing error Could not find resource /com/example/demoMyBatis/XML/Student.xml. been try to find solution like resource filterring but nothing work.pls help
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>
CodePudding user response:
Make sure the file Student.xml
lies in folder src/main/resources/com/example/demoMyBatis/XML/
within your project in IDE Eclipse/ intelliJ/ whatever.
CodePudding user response:
Here is modified code according to this tutorial.
- Changed mapper namespace to: com.example.mybatisdemo.mapper.StudentMapper
- Changed mapper name to student-mapper.xml
- put student-mapper.xml into src/main/resources/mapper folder
<?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>
- Created com.example.mybatisdemo.mapper.StudentMapper interface:
@Mapper
public interface StudentMapper {
void insertStudent(Student student);
void updateStudent(Student student);
void deleteStudentById(int id);
List<Student> selectAllStudents();
Student selectStudentById(int id);
}
- Created com.example.mybatisdemo.dao.Student pojo with lombok:
@Data
public class Student {
Integer id;
String name;
String branch;
Integer percentage;
Integer phone;
String email;
}
- Populated config with properties:
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 does not contain build/resources section because xml mappers are used from resources (not src) folder. I haven't checked the possibility to put xml mappers to src folder.