经过我十次以上的实验,abator(新版本叫做ibator)还是1.0.0版本好用,自动生成vo,dao,impl,xml文件,而且能够自动生成Example类,并且该类自动继承vo里面的对应类.
用1.1.0发现Example不能自动继承vo里面的类,
1.2.1版本变化太大,有些生成的东西并不方便
废话少说,先建库,采用mysql
create database abator_test; CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) )
下面建立一个java project,名字就叫做abator吧,然后在项目上点右键,new-->other-->Abator for IBATIS Configuration File,文件就放在/abator/src下面,文件名不变
下面是配置文件内容<?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 generatorSet="Java5"> <abatorContext> <!-- TODO: Add Database Connection Information --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/abator_test" userId="root" password="root"> <classPathEntry location="D:/mysql-connector-java-5.0.8-bin.jar" /> </jdbcConnection> <javaModelGenerator targetPackage="vo" targetProject="abator" /> <sqlMapGenerator targetPackage="vo" targetProject="abator" /> <daoGenerator type="IBATIS" targetPackage="dao" targetProject="abator" /> <table schema="" tableName="person"/> </abatorContext> </abatorConfiguration>
然后在配置文件上面点击右键,选择"Generate IBATIS Artifacts",接着abator就会根据数据库自动生成对应的vo.dao等文件了
然后继续写test/ibatis_config.xml文件,配置ibatis的信息<?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> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="256" maxSessions="100" maxTransactions="100" useStatementNamespaces="true"/> <!-- 配置数据源 --> <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/abator_test"/> <!-- 登录名 --> <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="vo/person_SqlMap.xml"/> </sqlMapConfig>
再加一个test/dao.xml文件,用于配置ibatis dao框架的dao
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE daoConfig PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN" "http://ibatis.apache.org/dtd/dao-2.dtd"> <daoConfig> <context> <transactionManager type="SQLMAP"> <property name="SqlMapConfigResource" value="test/ibatis_config.xml" /> </transactionManager> <dao interface="dao.PersonDAO" implementation="dao.PersonDAOImpl" /> </context> </daoConfig>
截下来是一个读取配置文件的类,然后返回DaoManager,文件路径为test/DAOConfig.java
package test; import java.io.Reader; import java.util.Properties; import com.ibatis.common.resources.Resources; import com.ibatis.dao.client.DaoManager; import com.ibatis.dao.client.DaoManagerBuilder; public class DAOConfig { private static final String resource="test/dao.xml"; private static final DaoManager daoManager; static{ daoManager = newDaoManager(null); } public static DaoManager getDaoManager(){ return daoManager; } public static DaoManager newDaoManager(Properties props){ try { Reader reader = Resources.getResourceAsReader(resource); return DaoManagerBuilder.buildDaoManager(reader,props); } catch (Exception e) { // TODO Auto-generated catch block new RuntimeException("Could not initialize DaoConfig. Cause: " + e, e); } return null; } }
最后来一个测试类test/Test.java
package test; import vo.Person; import com.ibatis.dao.client.DaoManager; import dao.PersonDAO; public class Test { public static void main(String[] args) { DaoManager daoManager = DAOConfig.getDaoManager(); PersonDAO personDao = (PersonDAO) daoManager.getDao(PersonDAO.class); Person p = personDao.selectByPrimaryKey(1); System.out.println(p.getName()); } }
OK了,这样就用ibatis dao做了一个最简单的例子,数据库以及各种文件都在附件里面,所需的四个包全在里面