ibates和Hibernate差不躲 都是一个ORM的实现 它主要是要程序员手动的取编写SQL语句,而不是和Hibernate一样,靠框架自动动态生成SQL语句。
需要下载3个ibates的jar包 分别是ibatis-2.3.0.677.jar, ibatis-common-2.jar, ibatis-sqlmap.jar 把这个包复制到项目下的LIB下面
写一个POJO 实体bean 实现序列化接口
public class Tmac implements Serializable {
public Integer id;
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
然后新建一个tmac.xml跟Tmac.java映射起来
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="Tmac">
<typeAlias alias="Tmac" type="enpty.Tmac" />
<resultMap id="Result" class="Tmac">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<insert id = "insertCust" parameterClass="Tmac">
INSERT INTO tmac VALUES(#id#,#name#)
</insert>
</sqlMap>
在新建一个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>
<sqlMap resource="enpty/tmac.xml" />
</sqlMapConfig>
新建一个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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
<property name="maxWait">
<value>5</value>
</property>
</bean>
<bean id="TransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/classes/sql-map-config.xml">
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- sqlMapClientTemplate是一个模板类 -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
<bean id="jsong" class="enpty.Jsong">
<property name="map">
<ref bean="sqlMapClientTemplate"/>
</property>
</bean>
</beans>
在建立一个Jsong.java
public class Jsong {
@SuppressWarnings("unused")
private SqlMapClientTemplate map;
public SqlMapClientTemplate getMap() {
return map;
}
public void setMap(SqlMapClientTemplate map) {
this.map = map;
}
public void insert(Tmac aa)
{
map.insert("insertCust",aa);
}
}
在新建一个测试类 Test.java
public class Test{
/**
* @param args
*/
public static void main(String[] args) {
try
{
ApplicationContext fa=new ClassPathXmlApplicationContext("applicationContext.xml");
Jsong jsong=(Jsong) fa.getBean("jsong");
Tmac aa=new Tmac();
aa.setId(10);
aa.setName("jay");
jsong.insert(aa);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的数据库是MySql,里面有张表是tmac 里面有2个字段 id和name