gpt4 book ai didi

java - Mybatis 3.4.6 - 数据未插入 HSQLDB

转载 作者:行者123 更新时间:2023-12-05 07:31:58 28 4
gpt4 key购买 nike

我将 Mybatis 3.4.6 与 HSQLDB 2.4.1 结合使用,但似乎无法将任何数据插入到数据库中。程序没有错误地执行并且似乎已经完成,但是当我检查数据库时没有插入任何内容。

从数据库中检索/选择行似乎工作正常,这让我相信我的语法/结构在“插入”映射方面有问题。任何帮助将不胜感激。

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();
}

}
}

如果这是一个愚蠢的问题/在我道歉之前有人问过。在过去的几个小时里,我的头一直在键盘上敲打,但无法找出问题所在。

最佳答案

由于评论中没有太多空间,我将添加此评论作为答案,但这是评论。

你说没有错误。我只能想到两件事:

  • 数据库在某种程度上是临时的,它不会持久保存在文件系统中,或者在连接关闭后被删除;或者

  • 执行中出现静默错误。

我建议启用 MyBatis DEBUG 模式。我使用 Log4j(但任何其他日志记录都有效),对于您的情况,您应该添加一行:

log4j.logger.org.mybatis.hsqldb.mappers_interface.ContactMapper=DEBUG

这将逐步向您展示每次执行。大多数情况下,DEBUG 级别对我来说已经足够了,但您可以将其增强到 TRACE 以查看更多信息。

无论如何,请参阅 MyBatis Logging了解详情。

关于java - Mybatis 3.4.6 - 数据未插入 HSQLDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51525607/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com