在开发的sql-map-config.xml文件以及sqlMap。xml文件中,发现了一个问题。就是注释语句必须在<sqlMapConfig>................</sqlMapConfig>的第一个元素中进行,放到其他地方就会解析不成功。
我用的是myeclipse6.0来开发的,不知道大家有没有遇到过这种问题,调试了一天都没有成功,到处找都没有找到错误的地方,知道第二天将所有的注释删除,才无意中发现,原来是注释的问题。我这里是自己试验的一个小案例:
开发环境:myeclipse+mysql 相关的包都在附件中。
sql-map-config.xml文件:
sqlmap文件的名字person.xml:
以上两个文件放在包:com.Jdnis.ibatis.map下
数据库:/*
SQLyog Enterprise Trial - MySQL GUI v6.55 RC
MySQL - 5.0.51a-community-nt : Database - person
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
javabean类存放在:com.Jdnis.ibatis.model下
测试类:
我用的是myeclipse6.0来开发的,不知道大家有没有遇到过这种问题,调试了一天都没有成功,到处找都没有找到错误的地方,知道第二天将所有的注释删除,才无意中发现,原来是注释的问题。我这里是自己试验的一个小案例:
开发环境:myeclipse+mysql 相关的包都在附件中。
sql-map-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- [b]注意,注释只能写在这里[/b] -->
<settings cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/person" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="root" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery" value="select 1 from sample" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<sqlMap resource="com/Jdnis/ibatis/map/person.xml" />
</sqlMapConfig>
sqlmap文件的名字person.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<!-- 同理注释只能写在这里 -->
<typeAlias alias="person" type="com.Jdnis.ibatis.model.Person" />
<statement id="insertPerson" parameterClass="person">
INSERT INTO persons VALUES(#id#,#firstName#,#lastName#,#birthDate#,#weight#,#height#)
</statement>
</sqlMap>
以上两个文件放在包:com.Jdnis.ibatis.map下
数据库:/*
SQLyog Enterprise Trial - MySQL GUI v6.55 RC
MySQL - 5.0.51a-community-nt : Database - person
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`person` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `person`;
/*Table structure for table `persons` */
DROP TABLE IF EXISTS `persons`;
CREATE TABLE `persons` (
`per_id` int(4) NOT NULL,
`PER_FIRST_NAME` varchar(20) NOT NULL,
`PER_LAST_NAME` varchar(20) NOT NULL,
`PER_BIRTH_DATE` date default NULL,
`weightInKilograms` double NOT NULL,
`heightInMeters` double NOT NULL,
PRIMARY KEY (`per_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `persons` */
insert into `persons`(`per_id`,`PER_FIRST_NAME`,`PER_LAST_NAME`,`PER_BIRTH_DATE`,`weightInKilograms`,`heightInMeters`) values (9,'hello','Jdni',NULL,125,170),(10,'cao','gang','3883-08-05',125,171),(11,'cao','gang','3883-08-05',125,171);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
javabean类存放在:com.Jdnis.ibatis.model下
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id ;
private String firstName ;
private String lastName ;
private Date birthDate ;
private double weight ;
private double height ;
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 Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
测试类:
package com.Jdnis.ibatis.dao;
import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.Jdnis.ibatis.model.Person;
public class PersonDao {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
p.setId(9);
p.setFirstName("hello");
p.setLastName("Jdni");
//p.setBirthDate(new Date(12,12,12));
p.setWeight(125);
p.setHeight(170);
PersonDao dao = new PersonDao();
dao.insert(p);
}
public void insert(Person p){
String xml = "com/Jdnis/ibatis/map/sql-map-config.xml";
SqlMapClient sqlMap = null;
try {
Reader reader = Resources.getResourceAsReader(xml);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMap.insert("insertPerson", p);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("主键冲突!,无法插入");
}
}
}