mybatis insert失败

J2EE 码拜 9年前 (2015-04-14) 1576次浏览 0个评论

问题如下:
我使用 mybatis插入一条数据,结果失败了 !

以下是我的做法,希望指点:
———————————————————-
数据库: mysql ;
程序: JavaEE + mybatis 
———————————-
mybatis-config.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>
	<properties  resource="jdbc.properties"></properties>
	<settings>
		<setting name="autoMappingBehavior" value="FULL"></setting>
	</settings>
	<typeAliases>
		<package name="com.mocoo.mvc.orm.mybatis.model"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<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>
	</environments>
	<mappers>
		<mapper resource="com/mocoo/mvc/orm/mybatis/mapping/TestMapper.xml" />
	</mappers>
</configuration>

TestMapper.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="com.mocoo.mvc.orm.mybatis.dao.TestMapper" >
  <resultMap id="BaseResultMap" type="com.mocoo.mvc.orm.mybatis.model.Test" >
    <id column="testId" property="testid" jdbcType="VARCHAR" />
    <result column="testName" property="testname" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    testId, testName
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select  <include refid="Base_Column_List" />  from testt  where testId = #{testid,jdbcType=VARCHAR}
  </select>
  <insert id="insert" parameterType="com.mocoo.mvc.orm.mybatis.model.Test" >
    insert into testt (testId, testName)
    values (#{testid,jdbcType=VARCHAR}, #{testname,jdbcType=VARCHAR})
  </insert>
</mapper>

TestMapper接口:

public interface TestMapper {
    int insert(Test record);
    Test selectByPrimaryKey(String testid);
}

Test 模型

public class Test {
    private String testid;

    private String testname;

    public String getTestid() {
        return testid;
    }

    public void setTestid(String testid) {
        this.testid = testid == null ? null : testid.trim();
    }

    public String getTestname() {
        return testname;
    }

    public void setTestname(String testname) {
        this.testname = testname == null ? null : testname.trim();
    }
}

TestMapperImpl 接口实现:

public class TestMapperImpl implements TestMapper{

	private static final String namespace = "com.mocoo.mvc.orm.mybatis.dao.TestMapper" ;

	private static SqlSession session = null ; 

	private static class TestMapperImplSington
	{
		public final static TestMapperImpl instance = new TestMapperImpl() ;
	}

	private TestMapperImpl()
	{
		session = MAMybatisSqlSessionFactory.instance.getSession() ;
	}

	public static TestMapper getInstance()
	{
		return TestMapperImplSington.instance ;
	}

	public int insert(Test record) {
		return session.insert(namespace+".insert", record) ;
	}

	public Test selectByPrimaryKey(String testid) {
		return session.selectOne(namespace+".selectByPrimaryKey", testid) ;
	}
}

测试:

	public static void main(String[] args) {
	    TestMapper testMapper = TestMapperImpl.getInstance() ;
	    Test t = new Test() ;
	    t.setTestid("3") ;
	    t.setTestname("vvsd");
	    int i = testMapper.insert(t);
	    System.out.println(i);
	}

————————–
问题如下:
我使用 mybatis插入一条数据,结果失败了 !

以下是我的做法,希望指点:

mybatis insert失败
错误呢? 贴出来啊
mybatis insert失败
引用 1 楼 shijing266 的回复:

错误呢? 贴出来啊

没有出错,程序是正确的,运行OK,就是数据库不变 !

mybatis insert失败
40分
  public int insert(Test record) {
        return session.insert(namespace+".insert", record) ;
    }
 

你这句的  namespace+”.insert”   这个跟你的TestMapper.xml的id=”insert”?   这个ID匹配么?

还有就是sql打出来了没,打出来了但是数据库不变的话 那是事务没提交   否则,你就设置断点,看看到哪一步了吧

mybatis insert失败
引用 3 楼 shijing266 的回复:
  public int insert(Test record) {
        return session.insert(namespace+".insert", record) ;
    }
 

你这句的  namespace+”.insert”   这个跟你的TestMapper.xml的id=”insert”?   这个ID匹配么?

还有就是sql打出来了没,打出来了但是数据库不变的话 那是事务没提交   否则,你就设置断点,看看到哪一步了吧

问题在mybatis官网找到了,因为事务没有提交 … 

官网说明,如下:

The default openSession() method that takes no parameters will create a SqlSession with the following characteristics:

A transaction scope will be started (i.e. NOT auto-commit).
A Connection object will be acquired from the DataSource instance configured by the active environment.
The transaction isolation level will be the default used by the driver or data source.
No PreparedStatements will be reused, and no updates will be batched.

——————————————————-

谢谢你 ! 40分请笑纳


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mybatis insert失败
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!