请教一个Ibaties的问题?

 

 

--- The error occurred in data/Account.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertAccount-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values (       1, 'fenglong', 'aaa', 'jushi1988@126.com'     )' at line 1

 

Accout.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Account">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Account" type="domain.Account"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->
  <resultMap id="AccountResult" class="Account">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>

  <!-- A simpler select example without the result map.  Note the
       aliases to match the properties of the target result class. -->
  <select id="selectAccountById" parameterClass="int" resultClass="Account">
    select
      ACC_ID as id,
      ACC_FIRST_NAME as firstName,
      ACC_LAST_NAME as lastName,
      ACC_EMAIL as emailAddress
    from ACCOUNT
    where ACC_ID = #id#
  </select>
  
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    values (
      #id#, #firstName#, #lastName#, #emailAddress#
    )
  </insert>

  <!-- Update example, using the Account parameter class -->
  <update id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>

</sqlMap>

 

SqlMapConfig.xml:

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <!-- Configure a built-in transaction manager.  If you're using an
       app server, you probably want to use its transaction manager
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/storebook"/>
      <property name="JDBC.Username" value="root"/>
      <property name="JDBC.Password" value="jushi"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="data/Account.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>
 一个Account.java:

package domain;

public class Account {

  private int id;
  private String firstName;
  private String lastName;
  private String emailAddress;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmailAddress() {
    return emailAddress;
  }

  public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
  }

}

TestIbatis.java

 

package domain;

import java.sql.SQLException;

import data.SimpleExample;

public class TestIbatis {
  public static void main(String as[]) throws SQLException{
   Account a = new Account();
   a.setId(1);
   a.setFirstName("fenglong");
   a.setEmailAddress("jushi1988@126.com");
   a.setLastName("aaa");
   try{
   SimpleExample se = new SimpleExample();
   se.insertAccount(a);
   System.out.print("插入数据成功"+a.getFirstName()+a.getEmailAddress());
   }catch(Exception e){
    System.out.print(e.getMessage());
   }
  }
}

请教最上面的异常是什么原因造成的?恳求答案?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iBatis 中,可以使用注解方式执行 SQL 查询并返回一个实体类里包含另一个实体类。这可以通过使用 `@Result` 注解中的 `association` 属性来实现。 首先,需要在执行 SQL 查询的方法上使用 `@Select` 注解指定 SQL 语句,并通过 `resultMap` 属性指定返回结果的映射关系。 然后,在 `@Results` 注解中使用 `@Result` 注解来指定主实体类的属性映射关系,并使用 `@Association` 注解来指定从属实体类的属性映射关系。 以下是一个示例代码: ```java public interface MyMapper { @Select("SELECT a.id AS a_id, a.name AS a_name, b.id AS b_id, b.name AS b_name FROM table_a a JOIN table_b b ON a.id = b.a_id WHERE a.id = #{id}") @Results({ @Result(property = "id", column = "a_id"), @Result(property = "name", column = "a_name"), @Result(property = "subEntity", column = "b_id", javaType = SubEntity.class, one = @One(select = "getSubEntityById")) }) MyEntity getEntityById(@Param("id") int id); @Select("SELECT id AS id, name AS name FROM table_b WHERE id = #{id}") SubEntity getSubEntityById(@Param("id") int id); } public class MyEntity { private int id; private String name; private SubEntity subEntity; // Getters and setters } public class SubEntity { private int id; private String name; // Getters and setters } ``` 在这个例子中,`MyMapper` 接口定义了两个方法 `getEntityById` 和 `getSubEntityById`,分别执行两个不同的 SQL 查询语句。 `MyEntity` 类中包含了一个 `SubEntity` 类的属性 `subEntity`,通过使用 `@Result` 注解中的 `association` 属性,指定了从属实体类的映射关系。在 `@One` 注解中,通过 `select` 属性指定了调用的方法名。 当需要执行这个查询并返回一个实体类里包含另一个实体类时,只需要调用 `getEntityById` 方法并传入相应的参数即可。返回的结果将会是一个完整的 `MyEntity` 对象,其中的 `subEntity` 属性也会被填充为对应的 `SubEntity` 对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值