Mybatis 3.4.6 - 数据未插入 HSQLDB
Mybatis 3.4.6 - Data is not being inserted into HSQLDB
我将 Mybatis 3.4.6 与 HSQLDB 2.4.1 结合使用,但似乎无法将任何数据插入到数据库中。程序没有错误地执行并且似乎已经完成,但是当我检查数据库时没有插入任何内容。
数据库中的 Retrieving/selecting 行似乎工作正常,这让我相信我的 syntax/structure 关于 'insert' 映射有问题。任何帮助将不胜感激。
configuration.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>
<!--suppress XmlPathReference -->
<properties resource="org/mybatis/hsqldb/db.properties"/>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="false"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="5"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<typeAlias type="org.mybatis.hsqldb.POJO.Contact" alias="contact" />
<typeAlias type="org.mybatis.hsqldb.POJO.EIList" alias = "EIList" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!--suppress MybatisConfigXml -->
<property name="driver" value="${jdbc.driver}" />
<!--suppress MybatisConfigXml -->
<property name="url" value="${jdbc.url}" />
<!--suppress MybatisConfigXml -->
<property name="username" value="${jdbc.username}" />
<!--suppress MybatisConfigXml -->
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--suppress XmlPathReference -->
<mapper resource="org/mybatis/hsqldb/mappers-xml/ContactMapper.xml" />
<!--suppress XmlPathReference -->
<mapper resource="org/mybatis/hsqldb/mappers-xml/EIListMapper.xml" />
</mappers>
</configuration>
db.properties
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:file:hsqldb/db/ipdb;shutdown=true
jdbc.username=admin
jdbc.password=password
Contact.java
package org.mybatis.hsqldb.POJO;
public class Contact {
Integer id;
String lastName;
String firstName;
String phone;
String email;
public Contact(Integer id, String lastName, String firstName, String phone, String email) {
this.id = id;
this.lastName = lastName;
this.firstName = firstName;
this.phone = phone;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
ContactMapper.java
package org.mybatis.hsqldb.mappers_interface;
import java.util.List;
import org.mybatis.hsqldb.POJO.Contact;
public interface ContactMapper {
Integer insert(Contact contact);
List<Contact> selectAll();
Contact select(Integer id);
Integer update(Contact contact);
Integer delete(Integer id);
}
ContactMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.mybatis.hsqldb.mappers_interface.ContactMapper">
<insert id="insert" parameterType="contact">
INSERT INTO CONTACT VALUES (${id}, ${lastName}, ${firstName}, ${phone}, ${email})
</insert>
<update id="update">
UPDATE CONTACT SET
"lastName" = #{lastName},
"firstName" = #{firstName},
"phone" = #{phone},
"email" = #{email}
WHERE "id" = #{id}
</update>
<delete id="delete">
DELETE FROM CONTACT WHERE "id" = #{value}
</delete>
<select id="selectAll" resultType="contact">
SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT
</select>
<select id="select" resultType="contact">
SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT where "id" = #{value}
</select>
</mapper>
Driver.java
package org.mybatis.hsqldb;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.hsqldb.POJO.Contact;
import org.mybatis.hsqldb.POJO.EIList;
import org.mybatis.hsqldb.mappers_interface.ContactMapper;
import org.mybatis.hsqldb.mappers_interface.EIListMapper;
public class Driver {
public static void main(String[] args)
{
SqlSessionFactory sqlMapper = null;
String resource = "org/mybatis/hsqldb/configuration.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Problem opening configuration.xml");
}
SqlSession session = sqlMapper.openSession();
try {
ContactMapper mapper = session.getMapper(ContactMapper.class);
Contact testContact = new Contact(3,"'last'","'first'","'phone'","'email'");
int result = mapper.insert(testContact);
session.commit();
} finally {
session.close();
}
}
}
如果这是一个愚蠢的 question/has 在我道歉之前被问到。在过去的几个小时里一直在敲键盘,但无法找出问题所在。
由于评论中没有太多空间,我将添加此评论作为答案,但这是评论。
你说没有错误。我只能想到两件事:
数据库在某种程度上是临时的,它不会持久保存在文件系统中,或者在连接关闭后被删除;或
执行中出现静默错误。
我建议启用 MyBatis DEBUG 模式。我使用 Log4j(但任何其他日志记录都有效),对于您的情况,您应该添加一行:
log4j.logger.org.mybatis.hsqldb.mappers_interface.ContactMapper=DEBUG
这将逐步向您展示每次执行。大多数时候 DEBUG
级别对我来说已经足够了,但您可以将其提高到 TRACE
以查看更多内容。
无论如何,请参阅 MyBatis Logging 了解详情。
解决了我自己的问题,所以我会在这里为遇到同样问题的其他人解答。
在 .script 文件中,我将 'File Write Delay' 设置为 0 毫秒,而不是原来的 500 毫秒延迟,后者似乎不会对数据库进行任何写入。
正常的数据库操作现已成功。
我将 Mybatis 3.4.6 与 HSQLDB 2.4.1 结合使用,但似乎无法将任何数据插入到数据库中。程序没有错误地执行并且似乎已经完成,但是当我检查数据库时没有插入任何内容。
数据库中的Retrieving/selecting 行似乎工作正常,这让我相信我的 syntax/structure 关于 'insert' 映射有问题。任何帮助将不胜感激。
configuration.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>
<!--suppress XmlPathReference -->
<properties resource="org/mybatis/hsqldb/db.properties"/>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="false"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="5"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<typeAlias type="org.mybatis.hsqldb.POJO.Contact" alias="contact" />
<typeAlias type="org.mybatis.hsqldb.POJO.EIList" alias = "EIList" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!--suppress MybatisConfigXml -->
<property name="driver" value="${jdbc.driver}" />
<!--suppress MybatisConfigXml -->
<property name="url" value="${jdbc.url}" />
<!--suppress MybatisConfigXml -->
<property name="username" value="${jdbc.username}" />
<!--suppress MybatisConfigXml -->
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--suppress XmlPathReference -->
<mapper resource="org/mybatis/hsqldb/mappers-xml/ContactMapper.xml" />
<!--suppress XmlPathReference -->
<mapper resource="org/mybatis/hsqldb/mappers-xml/EIListMapper.xml" />
</mappers>
</configuration>
db.properties
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:file:hsqldb/db/ipdb;shutdown=true
jdbc.username=admin
jdbc.password=password
Contact.java
package org.mybatis.hsqldb.POJO;
public class Contact {
Integer id;
String lastName;
String firstName;
String phone;
String email;
public Contact(Integer id, String lastName, String firstName, String phone, String email) {
this.id = id;
this.lastName = lastName;
this.firstName = firstName;
this.phone = phone;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
ContactMapper.java
package org.mybatis.hsqldb.mappers_interface;
import java.util.List;
import org.mybatis.hsqldb.POJO.Contact;
public interface ContactMapper {
Integer insert(Contact contact);
List<Contact> selectAll();
Contact select(Integer id);
Integer update(Contact contact);
Integer delete(Integer id);
}
ContactMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.mybatis.hsqldb.mappers_interface.ContactMapper">
<insert id="insert" parameterType="contact">
INSERT INTO CONTACT VALUES (${id}, ${lastName}, ${firstName}, ${phone}, ${email})
</insert>
<update id="update">
UPDATE CONTACT SET
"lastName" = #{lastName},
"firstName" = #{firstName},
"phone" = #{phone},
"email" = #{email}
WHERE "id" = #{id}
</update>
<delete id="delete">
DELETE FROM CONTACT WHERE "id" = #{value}
</delete>
<select id="selectAll" resultType="contact">
SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT
</select>
<select id="select" resultType="contact">
SELECT "id", "lastName", "firstName", "phone", "email" from CONTACT where "id" = #{value}
</select>
</mapper>
Driver.java
package org.mybatis.hsqldb;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.hsqldb.POJO.Contact;
import org.mybatis.hsqldb.POJO.EIList;
import org.mybatis.hsqldb.mappers_interface.ContactMapper;
import org.mybatis.hsqldb.mappers_interface.EIListMapper;
public class Driver {
public static void main(String[] args)
{
SqlSessionFactory sqlMapper = null;
String resource = "org/mybatis/hsqldb/configuration.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Problem opening configuration.xml");
}
SqlSession session = sqlMapper.openSession();
try {
ContactMapper mapper = session.getMapper(ContactMapper.class);
Contact testContact = new Contact(3,"'last'","'first'","'phone'","'email'");
int result = mapper.insert(testContact);
session.commit();
} finally {
session.close();
}
}
}
如果这是一个愚蠢的 question/has 在我道歉之前被问到。在过去的几个小时里一直在敲键盘,但无法找出问题所在。
由于评论中没有太多空间,我将添加此评论作为答案,但这是评论。
你说没有错误。我只能想到两件事:
数据库在某种程度上是临时的,它不会持久保存在文件系统中,或者在连接关闭后被删除;或
执行中出现静默错误。
我建议启用 MyBatis DEBUG 模式。我使用 Log4j(但任何其他日志记录都有效),对于您的情况,您应该添加一行:
log4j.logger.org.mybatis.hsqldb.mappers_interface.ContactMapper=DEBUG
这将逐步向您展示每次执行。大多数时候 DEBUG
级别对我来说已经足够了,但您可以将其提高到 TRACE
以查看更多内容。
无论如何,请参阅 MyBatis Logging 了解详情。
解决了我自己的问题,所以我会在这里为遇到同样问题的其他人解答。
在 .script 文件中,我将 'File Write Delay' 设置为 0 毫秒,而不是原来的 500 毫秒延迟,后者似乎不会对数据库进行任何写入。
正常的数据库操作现已成功。