按照网上教程搭建mybatis开发环境,我的本地目录如下:
?其中测试的映射文件位置在com.yd.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指定Dao接口的完整类名
mybatis会依据这个接口动态创建一个实现类去实现这个接口, 而这个实现类是一个Mapper对象-->
<mapper namespace="com.yd.mapper.UserMapper">
<!-- 根据id查找一个用户信息-->
<!-- “#{}”表示一个占位符,相当于“?”,而“#{id}”表示该占位符待接收参数的名称为id-->
<select id="selectUserByID" parameterType="Integer"
resultType="com.yd.pojo.User">
select *
from user
where id = #{id}
</select>
<!--查询所有用户信息 -->
<select id="selectAllUser" resultType="com.yd.pojo.User">
select *
from user
</select>
<!-- 添加一个用户-->
<insert id="addUser" parameterType="com.yd.pojo.User">
insert into user (name, sex)
values (#{name}, #{sex})
</insert>
<update id="updateUser" parameterType="com.yd.pojo.User">
update user
set name=#{name},
sex=#{sex}
where id = #{id}
</update>
<!-- 删除一个用户-->
<delete id="deleteUser" parameterType="Integer">
delete
from user
where id = #{id}
</delete>
</mapper>
MyBatis的配置文件如下
<?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>
<!-- 环境配置 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务管理-->
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容;
UNPOOLED:不使用连接池的数据源
POOLED :使用连接池的数据源
JNDI :使用JNDI实现的数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<mapper resource="com/yd/mapper/UserMapper.xml"/>
</mappers>
</configuration>
报错信息如下:
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/yd/mapper/UserMapper.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 2 more
Caused by: java.io.IOException: Could not find resource com/yd/mapper/UserMapper.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:377)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
... 4 more
原因是视频教程是用Eclipse,而我本地用的是idea,idea不会编译src的java目录的xml文件
解决:在pom.xml中添加build
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>com/yd/mapper/*.xml</include>
</includes>
</resource>
</resources>
</build>
再次执行,已不报错
|