必须为元素类型 "mapper" 声明属性 "namespace"解决

在使用mybatis进行mapper.xml测试的时候发生"必须为元素类型 “mapper” 声明属性 “namespace” "的错误
在这里插入图片描述

项目目录结构

UserMapper和UserMapper.xml统一放到mapper下了,SqlMapConfig.xml为mybatis的系统配置文件


SqlMapConfig.xml已经下引入了UserMapper啊
<?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>
    <!-- 和spring整合后 environments配置将废除-->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc事务管理-->
            <transactionManager type="JDBC"/>
            <!-- 数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <!--<mapper resource="User.xml"/>-->
        <mapper resource="cn/itheima/pojo/User.xml"/>
        <!--
        使用class属性引入接口的全路径名称:
        使用规则:
            1,接口名称和映射文件名除扩展名外要完全相同
            2,接口和映射文件要放在同一目录下
        -->
        <mapper class="cn.itheima.mapper.UserMapper"/>
   </mappers>
</configuration>

UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--
    mapper接口代理实现编写规则:
        1,映射文件中的namespace要等于接口的全路径名称
        2,映射文件中sql语句要等同于接口的方法名称
        3,映射文件中传入参数类型要等于接口方法的传入参数类型
        4,映射文件中返回结果集类型要等于接口方法的返回值类型
    -->
<mapper namespace="cn.itheima.mapper.UserMapper">

    <!--
    id:sql语句唯一标识
    parameterType:指定传入参数类型
    resultType:返回结果集类型
    #{}占位符:起到占位作用,如果传入的基本类型(String,long,double,boolean,float等),那么#{}中的变量名称可以随意写
    -->
    <select id="findUserById" parameterType="int" resultType="cn.itheima.pojo.User">
        select * from user where id = #{id}
    </select>


    <!--
        如果返回结果为集合,可以调用selectList方法,这个方法返回的结果就是一个集合,所以映射文件中应该配置为集合泛型的类型
        ${}拼接符:字符串原样拼接,如果传入的参数是类型如果传入的基本类型(String,long,double,boolean,float等),那么${}中的变量必须为value
        注意:拼接符有sql注入的风险,所以慎重使用
    -->
    <select id="findUserByUserName" parameterType="String" resultType="cn.itheima.pojo.User">
        select * from user where username like '%${value}%'
    </select>


    <!--
        #{}:如果传入的是pojo类型,那么#{}中的变量名称必须是pojo中对应的属性.属性,属性....
        如果要返回数据库自增主键:可以使用select LAST_INSERT_ID()
    -->
    <insert id="insertUser" parameterType="cn.itheima.pojo.User">
        /*
        执行select LAST_INSERT_ID()数据函数,返回自增的主键
        keyProperty:将返回的主键放入传入参数的id中保存
        order:当前函数相对于insert语句的执行顺序,在insert之前执行是before,在insert后执行是after
        resultType:id的类型,也是keyProperty中属性的类型
        */
        <selectKey keyProperty="id" order="AFTER" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
    </insert>
</mapper>

网上找了一下发现我的UserMapper.xml下应该引错了

应该将Config改成mapper,改好从新测试,问题解决

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值