`
zjnbshifox
  • 浏览: 314919 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

iBatis使用(1)之照猫画虎

    博客分类:
  • Java
阅读更多
1,在web.xml加入Spring的Config配置
xml 代码
 
  1. <context-param>  
  2.   <param-name>contextConfigLocation</param-name>  
  3.   <param-value>  
  4.    /WEB-INF/dataAccess.xml  /WEB-INF/applicationContext.xml  
  5.   </param-value>  
  6.  </context-param>  
2,用spring的MVC,还要加入Spring的Dispatcher,(这里的servlet-name指定了MVC的配置文件ibatis-servlet.xml,也是一个spring的配置文件)
xml 代码
 
  1. <servlet>  
  2.   <servlet-name>ibatis</servlet-name>  
  3.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.   <load-on-startup>2</load-on-startup>  
  5.  </servlet>  
  6.  <servlet-mapping>  
  7.   <servlet-name>ibatis</servlet-name>  
  8.   <url-pattern>*.go</url-pattern>  
  9.  </servlet-mapping>  
3,dataAccess.xml中加入iBatis的配置
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  5.   <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>  
  6.   <property name="url" value="jdbc:mysql://localhost:3306/pulan?useUnicode=true&amp;&amp;characterEncoding=GBK"/>  
  7.   <property name="username" value="root"/>  
  8.   <property name="password" value=""/>  
  9.  </bean>  
  10.  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  11.   <property name="dataSource" ref="dataSource"/>  
  12.  </bean>  
  13.  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  14.   <property name="configLocation" value="WEB-INF/sql-map-config.xml"/>  
  15.   <property name="dataSource" ref="dataSource"/>  
  16.  </bean>  
  17.    
  18.  <bean id="lineDAO" class="dao.ibatis.dao.LineDAOImpl">  
  19.   <property name="sqlMapClient" ref="sqlMapClient"/>  
  20.  </bean>  
  21.    
  22.  <bean id="stationDAO" class="dao.ibatis.dao.StationDAOImpl">  
  23.   <property name="sqlMapClient" ref="sqlMapClient"/>  
  24.  </bean>  
  25.    
  26.  <bean id="meterDAO" class="dao.ibatis.dao.MeterDAOImpl">  
  27.   <property name="sqlMapClient" ref="sqlMapClient"/>  
  28.  </bean>  
  29.  <bean id="dataDAO" class="dao.ibatis.dao.DataDAOImpl">  
  30.   <property name="sqlMapClient" ref="sqlMapClient"/>  
  31.  </bean>  
  32. </beans>  
sql-map-client.xml配置
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  
  3.     "http://www.ibatis.com/dtd/sql-map-config-2.dtd">  
  4. <sqlMapConfig>  
  5.  <sqlMap resource="dao/ibatis/maps/Line.xml"/>  
  6.  <sqlMap resource="dao/ibatis/maps/Station.xml"/>  
  7.  <sqlMap resource="dao/ibatis/maps/Meter.xml"/>  
  8.  <sqlMap resource="dao/ibatis/maps/Data.xml"/>  
  9.    
  10. </sqlMapConfig>  
5,Station.xml
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">  
  3. <sqlMap namespace="Station">  
  4.   <resultMap id="result" class="dao.ibatis.db.Station">  
  5.    <result property="stationID" column="StationID" columnIndex="1"/>  
  6.    <result property="stationNumber" column="StationNumber" columnIndex="2"/>  
  7.    <result property="stationName" column="StationName" columnIndex="3"/>  
  8.    <result property="stationAddress" column="StationAddress" columnIndex="4"/>  
  9.    <result property="stationType" column="StationType" columnIndex="5"/>  
  10.    <result property="assessType" column="assessType" columnIndex="6"/>  
  11.    <result property="transformerCapacity" column="TransformerCapacity" columnIndex="7"/>  
  12.    <result property="lineID" column="LineID" columnIndex="8"/>   
  13.   </resultMap>  
  14.   
  15.   <select id="getStationList" resultMap="result">  
  16.     select *  from station  
  17.   </select>  
  18.   <select id="getStationListByLineID"   
  19.    parameterClass="java.lang.String"  
  20.    resultMap="result">  
  21.     select *  from station  where `LineID` = #value#     
  22.   </select>  
  23.     
  24. </sqlMap>  
6,Station.java ----POJO
java 代码
 
  1. package dao.ibatis.db;  
  2. import java.io.Serializable;  
  3. public class Station implements Serializable {  
  4.     
  5.    private int stationID;  
  6.    private String stationNumber;  
  7.    private String stationName;  
  8.    private String stationAddress;  
  9.    private int stationType;  
  10.    private int assessType;  
  11.    private int transformerCapacity;  
  12.    private int lineID;  
  13.  public int getAssessType() {  
  14.   return assessType;  
  15.  }  
  16.  public void setAssessType(int assessType) {  
  17.   this.assessType = assessType;  
  18.  }  
  19.  public int getLineID() {  
  20.   return lineID;  
  21.  }  
  22.  public void setLineID(int lineID) {  
  23.   this.lineID = lineID;  
  24.  }  
  25.  public String getStationAddress() {  
  26.   return stationAddress;  
  27.  }  
  28.  public void setStationAddress(String stationAddress) {  
  29.   this.stationAddress = stationAddress;  
  30.  }  
  31.  public int getStationID() {  
  32.   return stationID;  
  33.  }  
  34.  public void setStationID(int stationID) {  
  35.   this.stationID = stationID;  
  36.  }  
  37.  public String getStationName() {  
  38.   return stationName;  
  39.  }  
  40.  public void setStationName(String stationName) {  
  41.   this.stationName = stationName;  
  42.  }  
  43.  public String getStationNumber() {  
  44.   return stationNumber;  
  45.  }  
  46.  public void setStationNumber(String stationNumber) {  
  47.   this.stationNumber = stationNumber;  
  48.  }  
  49.  public int getStationType() {  
  50.   return stationType;  
  51.  }  
  52.  public void setStationType(int stationType) {  
  53.   this.stationType = stationType;  
  54.  }  
  55.  public int getTransformerCapacity() {  
  56.   return transformerCapacity;  
  57.  }  
  58.  public void setTransformerCapacity(int transformerCapacity) {  
  59.   this.transformerCapacity = transformerCapacity;  
  60.  }  
  61.      
  62.      
  63. }  
7,StationDAO,StationDAOImpl数据访问
java 代码
 
  1. package dao.ibatis.dao;  
  2. import java.util.List;  
  3. public interface StationDAO {  
  4.  public List getStationList(int LineID);  
  5. }  
  6.    
  7. package dao.ibatis.dao;  
  8. import java.util.List;  
  9. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  
  10. public class StationDAOImpl extends SqlMapClientDaoSupport implements  
  11.   StationDAO {  
  12.  public List getStationList(int LineID) {  
  13.   // TODO Auto-generated method stub  
  14.   if(LineID <= 0){  
  15.    return getSqlMapClientTemplate().queryForList("getStationList"null);  
  16.   }else{  
  17.    return getSqlMapClientTemplate().queryForList("getStationListByLineID", String.valueOf(LineID));  
  18.   }  
  19.  }  
  20. }  
ibatis-serlet.xml定义动作
xml 代码
 
  1. <bean name="/listStation.go" class="net.fox.web.ListStationController">  
  2.   <property name="ibatis" ref="ibatis"/> //这里的ibatis不是servlet名字,而是一个Spring的bean  
  3.    //定义在applicationContext.xml里  
  4.  </bean>  
9。ibatis(property)
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.  <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  
  5.   <property name="transactionManager"><ref bean="transactionManager"/></property>  
  6.   <property name="transactionAttributes">  
  7.    <props>  
  8.     <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  9.     <prop key="update*">PROPAGATION_REQUIRED</prop>  
  10.     <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  11.    </props>  
  12.   </property>  
  13.  </bean>  
  14.  <bean id="ibatis" parent="baseTransactionProxy">  
  15.   <property name="target">  
  16.    <bean class="net.fox.IBatisImpl">  
  17.     <property name="lineDAO" ref="lineDAO"/>  
  18.     <property name="stationDAO" ref="stationDAO"/>  
  19.     <property name="meterDAO" ref="meterDAO"/>  
  20.     <property name="dataDAO" ref="dataDAO"/>  
  21.    </bean>  
  22.   </property>  
  23.  </bean>  
  24.    
  25. </beans>  
10.ListStationController.java
java 代码
 
  1. package net.fox.web;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import net.fox.IBatisImpl;  
  9. import org.springframework.web.servlet.ModelAndView;  
  10. import org.springframework.web.servlet.mvc.Controller;  
  11. import dao.ibatis.db.Station;  
  12. public class ListStationController implements Controller {  
  13.   private IBatisImpl ibatis;  
  14.     
  15.     
  16.     
  17.   public void setIbatis(IBatisImpl ibatis) {  
  18.    this.ibatis = ibatis;  
  19.   }  
  20.  public ModelAndView handleRequest(HttpServletRequest arg0,  
  21.    HttpServletResponse arg1) throws Exception {  
  22.   int LineID = Integer.parseInt(arg0.getParameter("line"));  
  23.   Map model = new HashMap();  
  24.   List l1 = this.ibatis.getStationDAO().getStationList(LineID);  
  25.   model.put("stationList", l1);  
  26.   return new ModelAndView("ListStation",model);  
  27.  }  
  28. }  
  29.    
ListStation.jsp (MVC 中的V)
xml 代码
 
  1. <%@page contentType="text/html;charset=GBK"%>  
  2. <%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld" %>  
  3. <%@ taglib prefix="fmt" uri="/WEB-INF/tld/fmt.tld" %>  
  4. <html><head><title>数据查看</title>  
  5. <meta content="text/html; charset=GBK" http-equiv="Content-Type" />  
  6. <META HTTP-EQUIV="Cache-Control" CONTENT="max-age=0">  
  7. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">  
  8. <meta http-equiv="expires" content="0">  
  9. <META HTTP-EQUIV="Pragma" CONTENT="no-cache">  
  10. </head>  
  11. <body bgcolor="white">  
  12. <center>  
  13.   <font size="4"><b>台区列表</b></font>  
  14. </center>  
  15. <form action="<c:url value="/listMeter.go"/>method="post">  
  16. <table align="center" bgcolor="#008800" border="0" cellspacing="2" cellpadding="3">  
  17.   <tr bgcolor="#CCCCCC">  <td><b>选择台区</b></td>  </tr>  
  18.   <tr bgcolor="#FFFF88"><td>  
  19.    <select name="station">  
  20.   <c:forEach var="station" items="${stationList}">    
  21.    <option value="<c:out value="${station.stationID}"/>"><c:out value="${station.stationName}"/></option>  
  22.   </c:forEach>  
  23.  </select>  
  24. </td>  
  25. </tr>  
  26. <tr><td><input type="submit" value="OK"/></td></tr>     
  27. </form>  
  28. </table>  
  29. <%@ include file="footer.jsp" %>  
总结:基本上是依靠Spring自带的jpetStore例子来做的,有几个问题,使用的TAGLIb是需要到apache的网站上下载,还有要用到的有cglib,commons,iBatis的jar,cglib是一定要用的
分享到:
评论

相关推荐

    ibatis 使用手册

    1. **配置文件**: iBatis的配置文件定义了数据库连接信息、SqlMap的位置等,是系统运行的基础。 2. **SqlMap**: SqlMap是iBatis的核心,它包含了SQL语句和结果映射。每个SqlMap元素代表一个数据库表,而SqlMap中的...

    ibatis的使用教程

    然而,Ibatis 也有其不足之处: 1. 开发者需要手动编写 SQL 语句,工作量较大。 2. 数据库移植可能较为困难,因为映射关系依赖于具体的 SQL 语句。 3. 缺乏对大数据查询的支持和对象缓存功能,可能影响性能。 4. SQL...

    ibatis 相关使用文档及安装包ibatis 相关使用文档及安装包

    这个压缩包包含了iBATIS的相关使用文档和安装包,是学习和使用iBATIS的重要参考资料。 《iBATIS-SqlMaps-2.pdf》可能是关于iBATIS SQL映射器的详细指南,它会介绍如何编写SQL Map配置文件,这些文件定义了数据库...

    ibatis使用手册

    【Ibatis 使用手册】 Ibatis 是一款优秀的对象关系映射(ORM)框架,它使得开发者能够在不脱离 SQL 的强大功能的同时,享受面向对象编程的便利。Ibatis.Net 是其针对 .NET 平台的移植版本,它简化了数据访问层(DAL...

    IBatis使用

    使用Ibatis对数据库的访问,实现增删改查的操作 ;

    ibatis使用及环境搭建文档

    1. **SqlMapConfig.xml**: 这是Ibatis的全局配置文件,包含了数据源、事务管理器、环境等信息。 2. **Mapper XML文件**: 每个Mapper XML文件代表一个DAO接口,包含SQL语句和映射结果集的定义。 3. **DAO接口**: 在...

    ibatis资料ibatis资料

    1. **《iBATIS in Action》**: 这通常是一本关于iBATIS的书籍,可能深入介绍了iBATIS的原理、配置、使用方法以及最佳实践,对于学习和理解iBATIS非常有帮助。 2. **《ibatis Guide》**: 这可能是iBATIS的官方指南...

    ibatis总结 ibatis ibatis ibatis ibatis

    1. Ibatis SQL映射: Ibatis的主要特点是通过XML或注解定义SQL映射文件,将SQL语句与Java对象绑定。例如,`&lt;select&gt;`标签用于定义查询语句,`&lt;insert&gt;`、`&lt;update&gt;`和`&lt;delete&gt;`分别对应增删改操作。在SQL映射文件...

    ibatis 之分页

    1. **SQL映射分页**:在Ibatis的XML配置文件中,我们可以直接编写包含分页条件的SQL语句。例如,对于MySQL,可以写成: ```xml SELECT * FROM your_table WHERE some_condition LIMIT #{start}, #{limit} ``` ...

    ibatis简易使用 ibatis简易使用 ibatis简易使用

    以下是对iBATIS简易使用的详细解释: 1. **环境准备**: - **jar文件**:iBATIS框架运行需要依赖一些库文件,包括`log4j`用于日志记录,`ibatis`核心库,以及`jdbc`驱动,这些都需要添加到项目的类路径中。 - **...

    ibatis使用备注

    标题 "ibatis使用备注" 暗示了我们即将探讨的是如何在Java应用程序中使用iBatis这个持久层框架。iBatis是一个轻量级的框架,它允许开发者将SQL语句与Java代码分离,提高了代码的可读性和可维护性。 首先,我们需要...

    struts1+ibatis框架整合

    "struts1+ibatis框架整合"意味着将这两个框架结合使用,以实现更高效、更灵活的Web应用开发。整合过程中,通常会将iBatis作为Struts1的DAO层,通过Action调用iBatis的SqlSession执行SQL,获取或更新数据库数据。...

    ibatis demo,ibatis例子,ibatis示例

    1. **配置文件**:Ibatis的配置文件(mybatis-config.xml)是整个框架的入口,它包含了数据源、事务管理器、SqlSessionFactory等重要设置。数据源定义了连接数据库的信息,如驱动、URL、用户名和密码;...

    IBatis简单使用

    1. **SQL映射**: IBatis的核心是SQL Map配置文件,其中包含了SQL语句及其执行方式。它可以将Java对象与数据库表中的记录进行映射,方便地实现增删改查操作。 2. **动态SQL**: IBatis支持动态SQL,这意味着开发者...

    struts1+ibatis+Spring demo

    Struts1、iBatis和Spring是Java Web开发中三个重要的框架,它们分别负责MVC模式中的表现层、数据访问层以及业务层的管理。这个"struts1+ibatis+Spring demo"是一个示例项目,展示了如何将这三个框架集成到一起,实现...

    iBatis详细使用手册(.net版)[收集].pdf

    1. iBatis概述 iBatis是一种灵活的持久层框架,提供了一种可控的方式来实现类ORM解决方案。它不同于NHibernate那样具备全自动的数据操作,而是提供了一种灵活的方式来控制SQL语句。用户需要自己编写SQL语句,但这也...

    IBatis多数据库,可通过配置指定不同的实体使用不同的数据库

    1.每个实体类需要配置各自的“SqlMap.config”在“config/SysConfig.xml”中,SqlMap,db1.Db1SqlMap就是“ibatis/db1/Db1SqlMap.config”,不同的db将分配不同的mapper类。 2.数据库链接统一放在“config/ibatis/...

    .net中使用iBATIS的小例子

    下面我们将详细探讨iBATIS在.NET中的使用方法,以及如何利用提供的文件进行实践。 首先,了解iBATIS的基本概念。iBATIS是一个轻量级框架,它的核心功能是动态SQL映射,允许开发者编写SQL语句并将其封装到XML配置...

    iBATIS 级联iBATIS 级联

    iBATIS 级联iBATIS 级联iBATIS 级联

Global site tag (gtag.js) - Google Analytics