Mybatis 3.4.6-数据未插入HSQLDB

时间:2018-07-25 18:35:25

标签: java intellij-idea mybatis hsqldb

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

    }
}

如果这是一个愚蠢的问题/在我道歉之前已经有人问过。在过去的几个小时里,我一直在用键盘敲打我的头,无法解决问题。

2 个答案:

答案 0 :(得分:0)

由于评论中没有太多空间,因此我将其添加为答案。

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

  • 数据库是临时的,并且不保留在文件系统上,或者在关闭连接后被删除;或

  • 执行过程中出现无提示错误。

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

Failed to load the dll from [?1], HRESULT: 0x8007007F
Failed to bind to CoreCLR at 'C:\vstsagent\A1\bin\coreclr.dll'

这将逐步向您显示每次执行。在大多数情况下,log4j.logger.org.mybatis.hsqldb.mappers_interface.ContactMapper=DEBUG 级别对我来说足够了,但是您可以将其增强到DEBUG来查看更多内容。

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

答案 1 :(得分:0)

解决了我自己的问题,因此我将在这里为遇到相同问题的其他人回答。

在.script文件中,我将“文件写入延迟”设置为0 ms,而不是原来的500 ms延迟,该延迟似乎并没有持久地写入数据库。

现在正常的数据库操作成功了。