AbatorForEclipse是IbatorForEclipse之前的一个老版插件。
插件装好后,我们来使用看看:
1,新建一个工程AbatorTest,点击右键,新建一个abatorConfig.xml,如图:
建好后如下图:
2,
我这里是用的mysql,修改abatisconfig.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC
"-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"
"http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/otm" userId="root" password="sa" >
<classPathEntry location="D:apache-tomcat-6.0.20webappsibatis_stuWEB-INFlibmysql-connector-java-5.1.7-bin.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.test.dto" targetProject="AbatorTest" >
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.test.sqlmap" targetProject="AbatorTest" />
<daoGenerator targetPackage="com.test.dao" targetProject="AbatorTest" type="GENERIC-CI" />
<table? tableName="customer" >
<generatedKey column="ID" sqlStatement="select LAST_INSERT_ID()" identity="true" />
</table>
<table? tableName="orders"/>
</abatorContext>
</abatorConfiguration>
建立数据库和表信息如下:
create database otm;
use otm
----测试onetomany
/*==============================================================*/
/* Table: customer????????????????????????????????????????????? */
/*==============================================================*/
create table customer
(
id?????????????????? bigint not null,
address????????????? varchar(120),
postcode???????????? varchar(6),
sex????????????????? varchar(2),
name???????????????? varchar(24),
primary key (id)
);
alter table customer comment '客户';
/*==============================================================*/
/* Table: orders??????????????????????????????????????????????? */
/*==============================================================*/
create table orders
(
id?????????????????? bigint not null,
code???????????????? varchar(24),
customerId?????????? bigint not null,
primary key (id)
);
alter table orders comment '订单';
alter table orders add constraint FK_rf1 foreign key (customerId)
references customer (id) on delete restrict on update restrict;
select * from orders;
select * from customer
select LAST_INSERT_ID()
3,选择abatorConfig.xml文件,工程结构图如下:
4,我们在使用之前先要配好SqlMapConfig.xml,这里没有整合其他框架,最原始的了:
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>
<properties resource="jdbc.properties" />
<settings cacheModelsEnabled="true" errorTracingEnabled="true"
enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<!--
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="${jdbc.driver}" />
<property name="JDBC.ConnectionURL" value="${jdbc.url}" />
<property name="JDBC.Username" value="${jdbc.username}" />
<property name="JDBC.Password" value="${jdbc.password}" />
</dataSource>
</transactionManager>
<!--
List the SQL Map XML files. They can be loaded from the classpath, as
they are here (com.domain.data...)
-->
<sqlMap resource="com/test/sqlmap/customer_SqlMap.xml" />
<sqlMap resource="com/test/sqlmap/orders_SqlMap.xml" />
<!-- List more here...-->
</sqlMapConfig>
jdbc.proteries文件中内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/otm
jdbc.username=root
jdbc.password=sa
为了让控制太能出现sql语句方便我们调试,log4j.properties文件中内容如下:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout
以Customer为例:
public static void main(String[] args) throws IOException {
// 读取SqlMapConfig的资源配置
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
CustomerDAOImpl cus=new CustomerDAOImpl(sqlMapClient);
Customer c=new Customer();
c.setAddress("haha");
c.setName("xixi");
c.setId(new Long(1));
try {
cus.updateByPrimaryKeySelective(c);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对应的一段配置文件如下:
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.test.dto.Customer" >
update customer
<dynamic prepend="set" >
<isNotNull prepend="," property="address"? >
address = #address:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="postcode" >
postcode = #postcode:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="sex" >
sex = #sex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="name" >
name = #name:VARCHAR#
</isNotNull>
</dynamic>
where id = #id:BIGINT#
</update>
下面是打印出的结果。
log4j:WARN No appenders could be found for logger (com.ibatis.common.jdbc.SimpleDataSource).
log4j:WARN Please initialize the log4j system properly.
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Executing Statement:????? update customer???? set??????????????? address = ???????????????????????????? ,???????? name = ????????????????? where id = ???
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Parameters: [haha, xixi, 1]
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Types: [java.lang.String, java.lang.String, java.lang.Long]
在自动生成pojo类中,对应就有一个xxxexample类,我们在用一下这个类。
public static void main(String[] args) throws IOException {
// 读取SqlMapConfig的资源配置
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
CustomerDAOImpl cus=new CustomerDAOImpl(sqlMapClient);
CustomerExample ce=new CustomerExample();
CustomerExample.Criteria cr=ce.createCriteria();
cr.andAddressIsNotNull();
cr.andAddressLike("h");
ce.setOrderByClause("id");
try {
cus.selectByExample(ce);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在XXXexample类中有个静态的内部内:部分内容如下:
public static class Criteria {
protected List criteriaWithoutValue;
protected List criteriaWithSingleValue;
protected List criteriaWithListValue;
protected List criteriaWithBetweenValue;
protected Criteria() {
super();
criteriaWithoutValue = new ArrayList();
criteriaWithSingleValue = new ArrayList();
criteriaWithListValue = new ArrayList();
criteriaWithBetweenValue = new ArrayList();
}
public boolean isValid() {
return criteriaWithoutValue.size() > 0
|| criteriaWithSingleValue.size() > 0
|| criteriaWithListValue.size() > 0
|| criteriaWithBetweenValue.size() > 0;
}
public List getCriteriaWithoutValue() {
return criteriaWithoutValue;
}
public List getCriteriaWithSingleValue() {
return criteriaWithSingleValue;
}
public List getCriteriaWithListValue() {
return criteriaWithListValue;
}
public List getCriteriaWithBetweenValue() {
return criteriaWithBetweenValue;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteriaWithoutValue.add(condition);
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
Map map = new HashMap();
map.put("condition", condition);
map.put("value", value);
criteriaWithSingleValue.add(map);
}
protected void addCriterion(String condition, List values, String property) {
if (values == null || values.size() == 0) {
throw new RuntimeException("Value list for " + property + " cannot be null or empty");
}
Map map = new HashMap();
map.put("condition", condition);
map.put("values", values);
criteriaWithListValue.add(map);
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
List list = new ArrayList();
list.add(value1);
list.add(value2);
Map map = new HashMap();
map.put("condition", condition);
map.put("values", list);
criteriaWithBetweenValue.add(map);
}
很关键的对应的配置文件部分内容如下:
<sql id="abatorgenerated_Example_Where_Clause"? >
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true"? >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and"? >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
下载:http://download.csdn.net/user/fatbear007
插件装好后,我们来使用看看:
1,新建一个工程AbatorTest,点击右键,新建一个abatorConfig.xml,如图:
建好后如下图:
2,
我这里是用的mysql,修改abatisconfig.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC
"-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"
"http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/otm" userId="root" password="sa" >
<classPathEntry location="D:apache-tomcat-6.0.20webappsibatis_stuWEB-INFlibmysql-connector-java-5.1.7-bin.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.test.dto" targetProject="AbatorTest" >
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.test.sqlmap" targetProject="AbatorTest" />
<daoGenerator targetPackage="com.test.dao" targetProject="AbatorTest" type="GENERIC-CI" />
<table? tableName="customer" >
<generatedKey column="ID" sqlStatement="select LAST_INSERT_ID()" identity="true" />
</table>
<table? tableName="orders"/>
</abatorContext>
</abatorConfiguration>
建立数据库和表信息如下:
create database otm;
use otm
----测试onetomany
/*==============================================================*/
/* Table: customer????????????????????????????????????????????? */
/*==============================================================*/
create table customer
(
id?????????????????? bigint not null,
address????????????? varchar(120),
postcode???????????? varchar(6),
sex????????????????? varchar(2),
name???????????????? varchar(24),
primary key (id)
);
alter table customer comment '客户';
/*==============================================================*/
/* Table: orders??????????????????????????????????????????????? */
/*==============================================================*/
create table orders
(
id?????????????????? bigint not null,
code???????????????? varchar(24),
customerId?????????? bigint not null,
primary key (id)
);
alter table orders comment '订单';
alter table orders add constraint FK_rf1 foreign key (customerId)
references customer (id) on delete restrict on update restrict;
select * from orders;
select * from customer
select LAST_INSERT_ID()
3,选择abatorConfig.xml文件,工程结构图如下:
4,我们在使用之前先要配好SqlMapConfig.xml,这里没有整合其他框架,最原始的了:
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>
<properties resource="jdbc.properties" />
<settings cacheModelsEnabled="true" errorTracingEnabled="true"
enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<!--
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="${jdbc.driver}" />
<property name="JDBC.ConnectionURL" value="${jdbc.url}" />
<property name="JDBC.Username" value="${jdbc.username}" />
<property name="JDBC.Password" value="${jdbc.password}" />
</dataSource>
</transactionManager>
<!--
List the SQL Map XML files. They can be loaded from the classpath, as
they are here (com.domain.data...)
-->
<sqlMap resource="com/test/sqlmap/customer_SqlMap.xml" />
<sqlMap resource="com/test/sqlmap/orders_SqlMap.xml" />
<!-- List more here...-->
</sqlMapConfig>
jdbc.proteries文件中内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/otm
jdbc.username=root
jdbc.password=sa
为了让控制太能出现sql语句方便我们调试,log4j.properties文件中内容如下:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout
以Customer为例:
public static void main(String[] args) throws IOException {
// 读取SqlMapConfig的资源配置
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
CustomerDAOImpl cus=new CustomerDAOImpl(sqlMapClient);
Customer c=new Customer();
c.setAddress("haha");
c.setName("xixi");
c.setId(new Long(1));
try {
cus.updateByPrimaryKeySelective(c);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对应的一段配置文件如下:
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.test.dto.Customer" >
update customer
<dynamic prepend="set" >
<isNotNull prepend="," property="address"? >
address = #address:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="postcode" >
postcode = #postcode:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="sex" >
sex = #sex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="name" >
name = #name:VARCHAR#
</isNotNull>
</dynamic>
where id = #id:BIGINT#
</update>
下面是打印出的结果。
log4j:WARN No appenders could be found for logger (com.ibatis.common.jdbc.SimpleDataSource).
log4j:WARN Please initialize the log4j system properly.
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Executing Statement:????? update customer???? set??????????????? address = ???????????????????????????? ,???????? name = ????????????????? where id = ???
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Parameters: [haha, xixi, 1]
2010-03-14 16:13:21,812 DEBUG [java.sql.PreparedStatement] - {pstm-100001} Types: [java.lang.String, java.lang.String, java.lang.Long]
在自动生成pojo类中,对应就有一个xxxexample类,我们在用一下这个类。
public static void main(String[] args) throws IOException {
// 读取SqlMapConfig的资源配置
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
CustomerDAOImpl cus=new CustomerDAOImpl(sqlMapClient);
CustomerExample ce=new CustomerExample();
CustomerExample.Criteria cr=ce.createCriteria();
cr.andAddressIsNotNull();
cr.andAddressLike("h");
ce.setOrderByClause("id");
try {
cus.selectByExample(ce);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在XXXexample类中有个静态的内部内:部分内容如下:
public static class Criteria {
protected List criteriaWithoutValue;
protected List criteriaWithSingleValue;
protected List criteriaWithListValue;
protected List criteriaWithBetweenValue;
protected Criteria() {
super();
criteriaWithoutValue = new ArrayList();
criteriaWithSingleValue = new ArrayList();
criteriaWithListValue = new ArrayList();
criteriaWithBetweenValue = new ArrayList();
}
public boolean isValid() {
return criteriaWithoutValue.size() > 0
|| criteriaWithSingleValue.size() > 0
|| criteriaWithListValue.size() > 0
|| criteriaWithBetweenValue.size() > 0;
}
public List getCriteriaWithoutValue() {
return criteriaWithoutValue;
}
public List getCriteriaWithSingleValue() {
return criteriaWithSingleValue;
}
public List getCriteriaWithListValue() {
return criteriaWithListValue;
}
public List getCriteriaWithBetweenValue() {
return criteriaWithBetweenValue;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteriaWithoutValue.add(condition);
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
Map map = new HashMap();
map.put("condition", condition);
map.put("value", value);
criteriaWithSingleValue.add(map);
}
protected void addCriterion(String condition, List values, String property) {
if (values == null || values.size() == 0) {
throw new RuntimeException("Value list for " + property + " cannot be null or empty");
}
Map map = new HashMap();
map.put("condition", condition);
map.put("values", values);
criteriaWithListValue.add(map);
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
List list = new ArrayList();
list.add(value1);
list.add(value2);
Map map = new HashMap();
map.put("condition", condition);
map.put("values", list);
criteriaWithBetweenValue.add(map);
}
很关键的对应的配置文件部分内容如下:
<sql id="abatorgenerated_Example_Where_Clause"? >
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true"? >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and"? >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
下载:http://download.csdn.net/user/fatbear007