作为开源的Orm对象映射框架ibatis是一个线程安全学习容易但是开发相对于hibernate来说的话就要繁锁些没有很好的工具支持ibatis所有的配置几乎是通过手写这样增加了开发者的难度好啦言归正转下面编写实现
一引入springibatis jar包
二编写logjproperties日志文件
logjrootLogger=DEBUGstdout
logjappenderstdout=orgapachelogjConsoleAppender logjappenderstdoutlayout=orgapachelogjPatternLayout logjappenderstdoutlayoutConversionPattern=%c{}% %m%n
logjloggerjavasqlPreparedStatement=DEBUG
三建立Studentjava类映象属性
package orgterryibatispojo;
public class Student { private Long id;
private String name;
private String subject;
private Long score;
public Long getId() { return id; }
public void setId(Long id) { thisid = id; }
public String getName() { return name; }
public void setName(String name) { thisname = name; }
public Long getScore() { return score; }
public void setScore(Long score) { thisscore = score; }
public String getSubject() { return subject; }
public void setSubject(String subject) { thissubject = subject; } }
四编写 studentxml 映象文件
<?xml version= encoding=utf ?>
<!DOCTYPE sqlMap
PUBLIC ////DTD sql Map //EN
mapdtd>
<sqlMap namespace=student>
<typeAlias alias=student type=orgterryibatispojoStudent/>
<resultMap class=student id=studentResult>
<result property=id column=id jdbcType=number javaType=javalangLong/>
<result property=name column=name/>
<result property=subject column=subject/>
<result property=score column=score/> </resultMap>
<select id=selectAll resultMap=studentResult>
select * from student </select>
<select id=findbyId parameterClass=javalangLong resultClass=student>
select * from student where id=#id# </select> <insert id=insert parameterClass=student>
insert into student(idnamesubjectscore) values(#id##name##subject##score#)
</insert>
<update id=update parameterClass=student>
update student set name=#name#subject=#subject#score=#score# where id=#id#
</update>
<delete id=delete parameterClass=javalangLong>
delete from student where id=#id#
</delete>
</sqlMap>
五编写 SqlMapConfigxml文件
<?xml version= encoding=utf ?>
<!DOCTYPE sqlMapConfig
PUBLIC ////DTD sql Map Config //EN
mapconfigdtd>
<sqlMapConfig>
<sqlMap resource=org/terry/ibatis/pojo/studentxml/> </sqlMapConfig>
六编写 StudentDaojava(实现类)
package orgterryibatisdao;
import javaioIOException; import javasqlSQLException; import javautilList;
import orgspringframeworkbeansfactoryxmlXmlBeanFactory; import orgreioClassPathResource; import orgreioResource; import orgspringframeworkormibatisSqlMapClientCallback; import orgspringframeworkormibatissupportSqlMapClientDaoSupport; import orgterryibatispojoStudent;
import comibatissqlmapclientSqlMapExecutor;
public class StudentDao extends SqlMapClientDaoSupport implements Idao{
public void delete(Long id) {
thisgetSqlMapClientTemplate()delete(delete id); }
public Object findbyId(Long id) {
return thisgetSqlMapClientTemplate()queryForObject(findbyId id); }
public List getAll() {
return (List)thisgetSqlMapClientTemplate()execute(new SqlMapClientCallback(){
public Object doInSqlMapClient(SqlMapExecutor sqlMapper) throws SQLException { return sqlMapperqueryForList(selectAll);
}
});
}
public void save(Object o) {
thisgetSqlMapClientTemplate()insert(insert o); }
public void update(Object o) {
thisgetSqlMapClientTemplate()update(update o);
}
public static void main(String[] args) throws IOException {
Resource re=new ClassPathResource(IbatisContextxml);
XmlBeanFactory xml=new XmlBeanFactory(re);
StudentDao student=(StudentDao)xmlgetBean(studentDao);
Student stu=new Student();
stusetId(LongvalueOf());
stusetName(terry);
stusetScore(LongvalueOf());
stusetSubject(数学);
studentdelete(LongvalueOf());
}
}
七配置 ApplicationContextxml文件
<?xml version= encoding=UTF?> <!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
<bean id=dataSource
class=monsdbcpBasicDataSource>
<property name=driverClassName>
<value>oraclejdbcdriverOracleDriver</value>
</property> <property name=url>
<value>jdbc:oracle:thin:@localhost::orcl</value>
</property>
<property name=username>
<value>terry</value>
</property>
<property name=password>
<value>terry</value>
</property>
</bean>
<bean id=sqlMapClient class=orgspringframeworkormibatisSqlMapClientFactoryBean>
<property name=configLocation value=SqlMapConfigxml/>
<property name=dataSource ref=dataSource></property>
</bean>
<bean id=transactionManager class=orgspringframeworkjdbcdatasourceDataSourceTransactionManager>
<property name=dataSource ref=dataSource></property>
</bean>
<! 配置事务拦截器 >
<bean id=transactionIterceptor class=orgspringframeworktransactioninterceptorTransactionInterceptor>
<! 事务拦截器需要注入一个事务管理器 >
<property name=transactionManager ref=transactionManager></property>
<property name=transactionAttributes>
<props>
<prop key=insert*>PROPAGATION_REQUIRED</prop>
<prop key=find*get*>PROPAGATION_REQUIREDreadOnly</prop>
<prop key=*>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class=orgspringframeworkaopframeworkautoproxyBeanNameAutoProxyCreator>
<property name=beanNames>
<list>
<value>*Dao</value>
</list>
</property>
<property name=interceptorNames>
<list>
<value>transactionIterceptor</value>
</list>
</property>
</bean>
<bean id=studentDao class=orgterryibatisdaoStudentDao>
<property name=sqlMapClient ref=sqlMapClient></property>
</bean>
</beans>