问题如下:
我使用 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 ;