熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

ibatis+spring 集成

2022-06-13   來源: Java開源技術 

  作為開源的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>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28705.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.