在使用mybatis进行mapper.xml测试的时候发生"必须为元素类型 “mapper” 声明属性 “namespace” "的错误
项目目录结构
UserMapper和UserMapper.xml统一放到mapper下了,SqlMapConfig.xml为mybatis的系统配置文件
SqlMapConfig.xml已经下引入了UserMapper啊
<configuration><environments default&#61;"development"><environment id&#61;"development"><transactionManager type&#61;"JDBC"/><dataSource type&#61;"POOLED"><property name&#61;"driver" value&#61;"com.mysql.jdbc.Driver"/><property name&#61;"url" value&#61;"jdbc:mysql://localhost:3306/mybatis?characterEncoding&#61;utf-8"/><property name&#61;"username" value&#61;"root"/><property name&#61;"password" value&#61;"123456"/>dataSource>environment>environments><mappers><mapper resource&#61;"cn/itheima/pojo/User.xml"/><mapper class&#61;"cn.itheima.mapper.UserMapper"/>mappers>
configuration>
UserMapper.xml
<mapper namespace&#61;"cn.itheima.mapper.UserMapper"><select id&#61;"findUserById" parameterType&#61;"int" resultType&#61;"cn.itheima.pojo.User">select * from user where id &#61; #{id}select><select id&#61;"findUserByUserName" parameterType&#61;"String" resultType&#61;"cn.itheima.pojo.User">select * from user where username like &#39;%${value}%&#39;select><insert id&#61;"insertUser" parameterType&#61;"cn.itheima.pojo.User">/*执行select LAST_INSERT_ID()数据函数,返回自增的主键keyProperty:将返回的主键放入传入参数的id中保存order:当前函数相对于insert语句的执行顺序,在insert之前执行是before,在insert后执行是afterresultType:id的类型,也是keyProperty中属性的类型*/<selectKey keyProperty&#61;"id" order&#61;"AFTER" resultType&#61;"int">select LAST_INSERT_ID()selectKey>insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})insert>
mapper>
网上找了一下发现我的UserMapper.xml下应该引错了
应该将Config改成mapper,改好从新测试,问题解决