`
snake_hand
  • 浏览: 573856 次
社区版块
存档分类
最新评论

使用abator自动生成代码时要注意的问题

 
阅读更多

1.我的习惯是先做数据库,数据库的设计如下图

2.进入eclipse,点击"Help>Install New Software"  输入name和location,其中location的URL地址为: URL: http://ibatis.apache.org/tools/abator

3.安装成功后,建立一个名为:ibatis的java工程,建立完成后,记得添加ibatis和数据的包

4.选中resources包,然后点击eclipse菜单栏的:File > New > Abator  for iBATIS Configuration File。然后自动生成一个叫abatorConfig.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/user" userId="root" password="123456" >
<classPathEntry location="G:/mysql/mysql-connector-java-5.1.6-bin.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.ibatis.model" targetProject="ibatis" />
<sqlMapGenerator targetPackage="com.ibatis.sqlmap" targetProject="ibatis" />
<daoGenerator targetPackage="com.ibatis.dao" targetProject="ibatis" type="GENERIC-CI" />
<table tableName="goods" >
</table>
</abatorContext>
</abatorConfiguration>

修改为以上配置后,选中该文件,右键选中Generate ibatis artifacts,即可生成相应的代码。但是其生成的代码有些需要修改的地方。

5.最重要一个是加上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>

<!--一定要加上下面这句,否则回报很多头痛的错误-->
<settings useStatementNamespaces="true" />
<transactionManager type="JDBC">
<!-- 数据源 -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/user" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="123456" />
</dataSource>
</transactionManager>
<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="com/ibatis/sqlmap/goods_SqlMap.xml" />
</sqlMapConfig>

6.修改一下DAO和DAOImpl里面的selectByExample和selectByPrimaryKey的返回类型,把它设置为你需要的泛型,这里我设置为List<Goods>,DAO的如下:

List<Goods> selectByPrimaryKey(Integer id) throws SQLException;

List<Goods> selectByExample(GoodsExample example) throws SQLException;

DAOImpl的如下:

//原来为List但我需要List集合所以改成List<Goods>
public List<Goods> selectByExample(GoodsExample example) throws SQLException {
List<Goods> list = (List<Goods>)sqlMapClient.queryForList("goods.abatorgenerated_selectByExample", example);
return list;
}

/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table goods
*
* @abatorgenerated Sat Mar 16 17:24:18 CST 2013
*/
//原来为Goods但我需要List集合所以改成List<Goods>
public List<Goods> selectByPrimaryKey(Integer id) throws SQLException {
Goods key = new Goods();
key.setId(id);
List<Goods> record = (List<Goods>) sqlMapClient.queryForObject("goods.abatorgenerated_selectByPrimaryKey", key);
return record;
}

7.在里面我们可以在Goods的模型里面重写以上toString()方法

public String toString(){
return "name="+this.name+" price="+this.price+" remark="+this.remark;
}

8.做完以上工作之后,我们建立一个main工程测试一下:

public class Test {

private static SqlMapClient sqlMapClient = null;
public static void main(String[] args) throws Exception {
Reader reader = Resources.getResourceAsReader("com/ibatis/sqlmap/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
GoodsExample example=new GoodsExample();
//多个条件同时查询:
Criteria criteria=example.createCriteria();
criteria.andNameLike("%o%");
criteria.andPriceEqualTo(1000.0);
// example.createCriteria().andNameLike("%o%");
// example.createCriteria().andPriceEqualTo(1000.0);
GoodsDAOImpl dao=new GoodsDAOImpl(sqlMapClient);
List<Goods> goods=dao.selectByExample(example);
for(Goods good:goods){
System.out.println(good);
}
}

}

9.控制台显示结果:

其中该项目的整体结构图如下:

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics