ibator生成的代码确实很完美,也给我们省了很多的开发成本。
但这里面我们又碰到了一个如何启用的一个问题。因为毕竟是自动生成的代码,要灵活使用起来还是一个问题。
一个是ibatis独立的使用 ,一个是与Spring的整合使用。
一、 ibatis独立的使用
1、添加ibatis的jar包
ibatis2-sqlmap-2.3.0.677.jar
ibatis-2.3.4.726.jar
<dependency>
<groupId>com.ibatis</groupId>
<artifactId>ibatis2-sqlmap</artifactId>
<version>2.3.0.677</version>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis</artifactId>
<version>2.3.4.726</version>
</dependency>
这里面提供的是maven的pom文件配置,自己可以网上查下,我这有可能是局域网的仓库。
2、添加ibatis的配置文件 SqlMapCommonConfig.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 cacheModelsEnabled="true" enhancementEnabled="false" lazyLoadingEnabled="false" errorTracingEnabled="true" maxRequests="400" maxSessions="40" maxTransactions="32" useStatementNamespaces="true"/> <!-- 由transactionManager元素的type属性值指定使用哪个事务管理器. --> <transactionManager type="JDBC" commitRequired="false"> <!-- dataSource元素定义DataSourceFactory的实现类,用这个实现类来创建实际的DataSourse. --> <dataSource type="SIMPLE"> <!-- 指定数据源的各种属性值 --> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql:///ertu" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="1234" /> </dataSource> </transactionManager> <sqlMap resource="com/dbms/mapper/ammeterperiod_SqlMap.xml"/> </sqlMapConfig>
最后可以写一下测试方法来测试一下。
/ * SqlMapClient是ibatis的核心组件,提供数据操作的基础平台,可以通过 sqlMapClientBuilder创建
*/
SqlMapClient sqlMap = null;
String resource = "SqlMapCommonConfig.xml";
try {
reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
//这里面的dao就可以用来增删改查了
AmmeterperiodDAO dao = new AmmeterperiodDAOImpl(sqlMap);
dao.insert(am);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
二、 与Spring的整合使用
1、这里面需要在ibator配置文件中修改type的值为Spring以便与Spring的整合使用,修改好后,重新生成代码
<daoGenerator targetPackage="com.dbms.dao" targetProject="ERTUv1.0.4" type="SPRING" />
2、加jar包
spring-2.5.6.jar
mysql-connector-java-5.1.9.jar
3、创建Spring的配置文件
applicationContext.xml
写好相关的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="sqlSessionFactory" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///ertu"></property> <property name="username" value="root"></property> <property name="password" value="68972891230"></property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:SqlMapCommonConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="ammeterperiodDAO" class="com.dbms.dao.AmmeterperiodDAOImpl"> <property name="dataSource" ref="dataSource" /> <property name="sqlMapClient" ref="sqlMapClient"></property> </bean> </beans>
测试
DataSource ds = null; // 暂时测试用
Connection con = null; // 暂时测试用
AmmeterperiodDAO dao = null;
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
dao = (AmmeterperiodDAOImpl)
context.getBean("ammeterperiodDAO");
am = dao.selectByPrimaryKey(2);
System.out.println(am.getStartTime());
p.s:
AmmeterperiodDAO 与AmmeterperiodDAOImp 是我这边自己生成的。