qq_39330298
2017-07-26 03:08 阅读 2.4k

Mybatis使用接口添加数据,报错

5

这是单元测试

 public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void test03() throws IOException{
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try{
        EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
        Employee employee=new Employee(null, "leihong", "159159");

        //System.out.println(employee);

        mapper.insertuser(employee);
        openSession.commit();
        System.out.println("成功");
        }finally{
            openSession.close();

        }
    }

这是接口:

 package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.bean.Employee;

public interface EmployeeMapper {

    public Employee getEmpById(Integer id);
    public Employee insertuser(Employee employee);

}

这是sql映射文件:

<?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.atguigu.mybatis.dao.EmployeeMapper">
                    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
        select * from user where id = #{id}
    </select>
    <insert id="insertuser">
    insert into user(username,password) values(#{username},#{password})
    </insert>
</mapper>**

这是封装类:
package com.atguigu.mybatis.bean;

import org.apache.ibatis.type.Alias;

@Alias("emp")
public class Employee {

private Integer id;
private String username;
private String password;


public Employee() {
    super();
    // TODO Auto-generated constructor stub
}


public Employee(Integer id, String username, String password) {
    super();
    this.id = id;
    this.username = username;
    this.password = password;
}


public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
@Override
public String toString() {
    return "Employee [id=" + id + ", username=" + username + ", password=" + password + "]";
}

}

这是全局配置文件:

<?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">

<properties resource="dbconfig.properties"></properties>
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>

    <package name="com.atguigu.mybatis.bean"/>
</typeAliases>

<environments default="dev_mysql">
    <environment id="dev_mysql">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </dataSource>
    </environment>

    <environment id="dev_oracle">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="${orcl.driver}" />
            <property name="url" value="${orcl.url}" />
            <property name="username" value="${orcl.username}" />
            <property name="password" value="${orcl.password}" />
        </dataSource>
    </environment>
</environments>

<databaseIdProvider type="DB_VENDOR">
    <!-- 为不同的数据库厂商起别名 -->
    <property name="MySQL" value="mysql"/>
    <property name="Oracle" value="oracle"/>
    <property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>

<mappers>
    <package name="com.atguigu.mybatis.dao"/>
</mappers>

这是报错信息:

org.apache.ibatis.binding.BindingException: Mapper method 'com.atguigu.mybatis.dao.EmployeeMapper.insertuser' has an unsupported return type: class com.atguigu.mybatis.bean.Employee
        at org.apache.ibatis.binding.MapperMethod.rowCountResult(MapperMethod.java:109)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
        at com.sun.proxy.$Proxy7.insertuser(Unknown Source)
        at com.atguigu.mybatis.test.MyBatisTest.test03(MyBatisTest.java:117)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)al.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • u010001477 Nothing_katu 2017-07-26 03:12

    你把报错信息贴出来。好针对问题看。

    点赞 评论 复制链接分享
  • qq_27718453 庄粟 2017-07-26 03:26
    <?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.atguigu.mybatis.dao.EmployeeMapper" >
    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
        select * from user where id = #{id}
    </select>
    <insert id="insertuser">
    insert into user(username,password) values(#{username},#{password})
    </insert>
    </mapper>
    

    加上mapper

    点赞 评论 复制链接分享
  • strutce 丵鹰 2017-07-26 03:38

    你的参数对象EmployeeMapper与你的mapper对应的对象Employee不是同一个对象

     Mapper method 'com.atguigu.mybatis.dao.EmployeeMapper.insertuser' has an unsupported return type: class com.atguigu.mybatis.bean.Employee
    
    点赞 评论 复制链接分享
  • qq_31138523 huwenyong123 2017-07-26 03:50

    public interface EmployeeMapper {

    public Employee getEmpById(Integer id);
    public Employee insertuser(Employee employee);
    

    }
    insertUser返回的不是 Employee 这个对象,是影响表行数的值 ,应该是 int 类型

    点赞 评论 复制链接分享

相关推荐