MyBatis3.1.1+Spring3.1.2+Struts2.3.4.1

地址:http://takeme.iteye.com/blog/1736320

MyBatis3.1.1+Spring3.1.2+Struts2.3.4.1 

先來看目錄結構 
 

 

來看配置文件 
applicationContext.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:aop="http://www.springframework.org/schema/aop"    
  5.     xmlns:tx="http://www.springframework.org/schema/tx"    
  6.     xmlns:context="http://www.springframework.org/schema/context"     
  7.     xsi:schemaLocation="http://www.springframework.org/schema/context     
  8.      http://www.springframework.org/schema/context/spring-context-3.0.xsd      
  9.      http://www.springframework.org/schema/beans     
  10.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  11.      http://www.springframework.org/schema/tx     
  12.      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  13.      http://www.springframework.org/schema/aop  
  14.      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">   
  15.   
  16. <!-- 採用c3p0數據源 這個是在企業中用的比較多的一個數據源 -->    
  17. <!-- destroy-method="close"的作用是當數據庫連接不使用的時候,就把該連接重新放到數據池中,方便下次使用調用 -->    
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">    
  19.    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>    
  20.    <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>    
  21.    <property name="user" value="luob"/>    
  22.    <property name="password" value="luob"/>    
  23.    <!-- 連接池中的最大連接數 -->    
  24.    <property name="maxPoolSize" value="150"/>    
  25.        
  26.    <!-- 連接池中的最小連接數 -->    
  27.    <property name="minPoolSize" value="1"></property>    
  28.        
  29.    <!-- 初始化連接池中的 連接數,取值 在  minPoolSize 和 maxPoolSize 之間,default:3-->    
  30.    <property name="initialPoolSize" value="3"/>    
  31.        
  32.    <!-- 最大空閒時間,60s內該連接沒有被使用則被丟棄,若爲0 永不丟棄.default:0 -->    
  33.    <property name="maxIdleTime" value="60"/>    
  34.        
  35.    <!-- 當連接數不夠時,每次同時創建多少個連接 -->    
  36.    <property name="acquireIncrement" value="1"/>    
  37.        
  38.    <!-- 每60s檢查連接池中的所有空間連接,如果沒有被使用,就被放棄, default:0 -->    
  39.    <property name="idleConnectionTestPeriod" value="60"/>    
  40. </bean>   
  41.   
  42.   <!-- 從c3p0數據源中抽取出JDBC的代理對象-->    
  43. <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor"  lazy-init="true" />     
  44.     
  45. <!--9i: org.springframework.jdbc.support.lob.OracleLobHandler  -->    
  46. <!--10g以後:org.springframework.jdbc.support.lob.DefaultLobHandler(mysql,DB2等都可以用這個)  -->    
  47. <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">    
  48.   <!-- 9i: 指定操作lob類型數據的jdbc代理對象 如果上面的 lobHandler 換了下面的就不需要了 -->    
  49.   <property name="nativeJdbcExtractor">    
  50.     <ref local="nativeJdbcExtractor" />    
  51.   </property>    
  52. </bean>   
  53.   
  54. <!-- 使用jdbc 來管理事務 -->  
  55. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  56.     <property name="dataSource" ref="dataSource"/>  
  57. </bean>  
  58.   
  59. <!-- 配置 mybatis 的sqlSessionFactory 由 spring 的 SqlSessionFactoryBean 代理 -->  
  60. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  61.     <property name="dataSource" ref="dataSource"/>  
  62.     <property name="configLocation" value="classpath:mybatis-config.xml"/>  
  63. </bean>  
  64.   
  65. <!-- 使用spring 的 SqlSessionTemplate 創建一個 可以批量操作的sqlSession  -->  
  66. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  67.     <constructor-arg index="0" ref="sqlSessionFactory"/>  
  68. </bean>  
  69.   
  70.   
  71. <!-- =============================== -->  
  72. <!--  /////////    dao 的配置              /////-->  
  73. <!-- =============================== -->  
  74. <bean id="studentDAO" class="com.mybatis.student.IStudentDAOImpl">  
  75.     <property name="sqlSession" ref="sqlSession"/>  
  76. </bean>  
  77.   
  78. <!-- 使用   sqlSessionTemplate 創建的 sqlSession -->  
  79. <bean id="studentDAO1" class="com.mybatis.student.IStudentDAOImpl_sqlSessionTemplate">  
  80. <constructor-arg index="0" ref="sqlSessionFactory"/>  
  81. </bean>  
  82.   
  83. <bean id="studentDAO2" class="com.mybatis.student.IStudentDAOImpl_sqlSessionDaoSupport">  
  84.     <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
  85.     <!-- 或者 使用   sqlSessionTemplate  如果兩個都配置了 會忽略 sqlSessionFactory -->  
  86. </bean>  
  87.   
  88. <!-- 採用MapperFactoryBean  -->  
  89. <bean id="classesDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  90.     <property name="mapperInterface" value="com.mybatis.classes.IClassesDAO"/>  
  91.     <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
  92. </bean>  
  93. <!-- =============================== -->  
  94. <!--  /////   Serivce 的配置                /////-->  
  95. <!-- =============================== -->  
  96. <bean id="studentService" class="com.mybatis.student.IStudentServiceImpl">  
  97.     <property name="sudentDAO" ref="studentDAO"/>  
  98. </bean>  
  99.   
  100. <bean id="classesService" class="com.mybatis.classes.IClassesServiceImpl">  
  101.     <property name="classesDAO" ref="classesDAO"/>  
  102. </bean>  
  103.   
  104. </beans>     


mybatis-config.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3.   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4. <configuration>  
  5.   
  6.     <!-- 配置的元素順序 properties?, settings?, typeAliases?, typeHandlers?,   
  7.     objectFactory?, objectWrapperFactory?, proxyFactory?, plugins?,   
  8.     environments?, databaseIdProvider?, mappers -->  
  9.        
  10.     <!-- 配置mybatis的緩存,延遲加載等等一系列屬性 -->    
  11.     <settings>    
  12.         <!-- 全局映射器啓用緩存 -->    
  13.         <setting name="cacheEnabled" value="true" />    
  14.         <!-- 查詢時,關閉關聯對象即時加載以提高性能 -->    
  15.         <setting name="lazyLoadingEnabled" value="true" />    
  16.         <!-- 設置關聯對象加載的形態,此處爲按需加載字段(加載字段由SQL指 定),不會加載關聯表的所有字段,以提高性能 -->    
  17.         <setting name="aggressiveLazyLoading" value="false" />    
  18.         <!-- 對於未知的SQL查詢,允許返回不同的結果集以達到通用的效果 -->    
  19.         <setting name="multipleResultSetsEnabled" value="true" />    
  20.         <!-- 允許使用列標籤代替列名 -->    
  21.         <setting name="useColumnLabel" value="true" />    
  22.         <!-- 允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作爲鍵值),數據表的PK生成策略將被覆蓋 -->    
  23.         <!-- <setting name="useGeneratedKeys" value="true" /> -->    
  24.         <!-- 給予被嵌套的resultMap以字段-屬性的映射支持 -->    
  25.         <setting name="autoMappingBehavior" value="FULL" />    
  26.         <!-- 對於批量更新操作緩存SQL以提高性能 -->    
  27.         <setting name="defaultExecutorType" value="SIMPLE" />    
  28.         <!-- 數據庫超過25000秒仍未響應則超時 -->    
  29.         <setting name="defaultStatementTimeout" value="25000" />    
  30.     </settings>     
  31.        
  32.     <!-- 使用屬性文件 而且可以在這裏這是 覆蓋文件中的值 -->  
  33.      
  34.       
  35.     <!-- 別名的配置 -->  
  36.     <typeAliases>  
  37.         <typeAlias type="com.mybatis.student.Student" alias="Student"/>  
  38.         <typeAlias type="com.mybatis.classes.Classes" alias="Classes"/>  
  39.         <!--   
  40.             也可以使用 包範圍來配置  
  41.             <package name="com.mybatis"/>  
  42.          -->  
  43.     </typeAliases>  
  44.       
  45.     <!-- 環境的配置 -->  
  46.       
  47.     <!-- 映射文件的配置 -->  
  48.     <mappers>  
  49.         <mapper resource="com/mybatis/student/StudentMapper.xml"/>  
  50.         <mapper resource="com/mybatis/classes/ClassesMapper.xml"/>  
  51.     </mappers>  
  52.       
  53. </configuration>  


struts.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.     <package name="student" namespace="/student" extends="struts-default">  
  8.         <action name="allStudent" class="com.mybatis.action.StudentAction" method="getAllStudent">  
  9.             <result name="success">/index.jsp</result>  
  10.         </action>  
  11.         <action name="updateAndSelect" class="com.mybatis.action.StudentAction" method="getAllStudentAfterupdate">  
  12.             <result name="success">/index.jsp</result>  
  13.         </action>  
  14.         <action name="delStudentById" class="com.mybatis.action.StudentAction" method="delStudentById">  
  15.             <result name="success">/index.jsp</result>  
  16.         </action>  
  17.     </package>  
  18.       
  19.     <package name="classes" namespace="/classes" extends="struts-default">  
  20.         <action name="queryClassesById" class="com.mybatis.action.ClassesAction" method="queryClassesById">  
  21.             <result name="success">/index1.jsp</result>  
  22.         </action>  
  23.         <action name="delClassesById" class="com.mybatis.action.ClassesAction" method="delClassesById">  
  24.             <result name="success">/index1.jsp</result>  
  25.         </action>  
  26.     </package>  
  27. </struts>  


web.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:applicationContext.xml</param-value>  
  11.     </context-param>  
  12.       
  13.     <listener>          
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.     <filter>  
  17.         <filter-name>struts2</filter-name>  
  18.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  19.     </filter>  
  20.     <filter-mapping>  
  21.         <filter-name>struts2</filter-name>  
  22.         <url-pattern>/*</url-pattern>  
  23.     </filter-mapping>  
  24.       
  25.   <welcome-file-list>  
  26.     <welcome-file>index.jsp</welcome-file>  
  27.   </welcome-file-list>  
  28. </web-app>  


--映射文件 
StudentMapper.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3.  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4.  <mapper namespace="com.mybatis.student">  
  5.     <!-- <!ELEMENT mapper (  
  6.         cache-ref | cache | resultMap* | parameterMap* | sql*   
  7.         | insert* | update* | delete* | select* )+> -->  
  8.       
  9.     <!-- 設置緩存 如果用戶需要登錄 需要設置這種類型 type=org.mybatis.caches.oscache.LoggingOSCache-->  
  10.     <cache eviction="FIFO" readOnly="true" size="256" flushInterval="60000"/>  
  11.        
  12.     <!-- 定義可以重用的sql 代碼片段  -->  
  13.     <sql id="studentColumns">sid,sname,score</sql>  
  14.   
  15.     <!-- 自定義結果集 -->     
  16.     <resultMap type="Student" id="studentResultMap">  
  17.         <id property="sid" column="SID"/>  
  18.         <result property="sname" column="SNAME"/>  
  19.         <result property="score" column="SCORE"/>  
  20.     </resultMap>  
  21.       
  22.     <resultMap type="Student" id="studentAllResultMap">  
  23.         <id property="sid" column="SID"/>  
  24.         <result property="sname" column="SNAME"/>  
  25.         <result property="major" column="MAJOR"/>  
  26.         <result property="birth" column="BIRTH"/>  
  27.         <result property="score" column="SCORE"/>  
  28.         <result property="cid" column="CID"/>  
  29.         <result property="status" column="STATUS"/>  
  30.     </resultMap>  
  31.       
  32.     <!-- 只用構造函數 創建對象 對於那些 比較少的列 -->  
  33.     <resultMap type="Student" id="studentAndClassesResultMap">  
  34.         <constructor>  
  35.             <idArg column="SID" javaType="int"/>  
  36.             <arg column="SNAME" javaType="String"/>  
  37.             <arg column="SCORE" javaType="float"/>  
  38.         </constructor>  
  39.         <association property="classes" javaType="Classes" resultMap="com.mybatis.classes.classesResultMap"/>  
  40.     </resultMap>  
  41.    
  42.    
  43.     <select id="selectStudentAndClassBySname" parameterType="String" resultMap="studentAndClassesResultMap">  
  44.         select s.sid,s.sname,s.score,c.cid,c.cname,c.teacher,c.createdate from student s left join classes c on s.cid=c.cid where s.sname=#{sname}  
  45.     </select>  
  46.    
  47.     <insert id="addStudentBySequence" parameterType="Student" >  
  48.     <selectKey keyProperty="sid" resultType="int" order="BEFORE">  
  49.         select STUDENT_SEQ.nextVal from dual  
  50.     </selectKey>  
  51.         insert into student(sid,sname,major,birth,score)  
  52.         values (#{sid},#{sname},#{major},#{birth},#{score})  
  53.     </insert>  
  54.       
  55.     <insert id="addStudent" parameterType="Student">  
  56.         insert into student(sid,sname,major,birth,score)  
  57.         values (#{sid},#{sname},#{major},#{birth},#{score})  
  58.     </insert>  
  59.       
  60.     <delete id="delStudentById" parameterType="int">  
  61.         delete student where sid=#{sid}  
  62.     </delete>  
  63.       
  64.     <select id="queryAllStudent" resultType="Student" useCache="true" flushCache="false" timeout="10000">  
  65.         select * from student order by sid  
  66.     </select>  
  67.       
  68.     <!-- 參數可以指定一個特定的數據類型  還可以使用自定類型處理: typeHandler=MyTypeHandler -->  
  69.     <select id="queryStudentByName" resultType="Student" parameterType="String">  
  70.         select * from student where sname like #{property,javaType=String,jdbcType=VARCHAR}  
  71.     </select>  
  72.       
  73.     <!-- 參數可以指定一個特定的數據類型 對於數字類型 ,numericScale=2  用於設置小數類型  -->  
  74.     <select id="queryStudentById" resultType="Student" parameterType="int">  
  75.         select * from student where sid=#{property,javaType=int,jdbcType=NUMERIC}  
  76.     </select>  
  77.       
  78.       
  79.     <update id="updStudentById" parameterType="Student">  
  80.         update student   
  81.         <trim prefix="SET" suffixOverrides=",">  
  82.             <if test="sname !=null">sname=#{sname},</if>  
  83.             <if test="major !=null">majoir=#{major},</if>  
  84.             <if test="birth !=null">birth=#{birth},</if>  
  85.             <if test="score !=null">score=#{score}</if>  
  86.         </trim>  
  87.         where sid=#{sid}  
  88.     </update>  
  89.       
  90.     <!-- 在這裏 利用了 可重用的sql代碼片段 -->  
  91.     <select id="selectMapResult" resultMap="studentResultMap" parameterType="String">  
  92.         select <include refid="studentColumns"/> from STUDENT where sname like #{sname}  
  93.     </select>   
  94.       
  95.     <!-- Dynamic  Sql 使用  if -->  
  96.     <select id="selectStudentByDynamicSql" parameterType="Student" resultType="Student">  
  97.         select * from student   
  98.         <where>  
  99.             <if test="sname !=null">  
  100.                 sname like #{sname}  
  101.             </if>  
  102.             <if test="sid !=null">  
  103.                 AND sid=#{sid}  
  104.             </if>  
  105.         </where>  
  106.     </select>  
  107.       
  108.     <!-- 採用 OGNL 表達式  來配置動態sql 使用trim 去掉 where 中多餘的  and 或者 or  where  choose  when otherwise-->  
  109.     <select id="selectStudentByDynamicSqlChoose" parameterType="Student" resultType="Student">  
  110.         select * from student   
  111.         <trim prefix="WHERE" prefixOverrides="AND | OR ">  
  112.             <choose>  
  113.                 <when test=" sname !=null and sname.length() >0 ">   
  114.                     sname like #{sname}  
  115.                 </when>  
  116.                 <when test="sid !=null and sid>0">  
  117.                     AND sid = #{sid}  
  118.                 </when>  
  119.                 <otherwise>  
  120.                     AND status='1'  
  121.                 </otherwise>  
  122.             </choose>  
  123.         </trim>  
  124.     </select>  
  125.       
  126.     <!-- 使用foreach 遍歷list  只能小寫-->  
  127.     <select id="selectStudentByIds" resultType="Student">  
  128.         select * from student   
  129.         where sid in  
  130.         <foreach collection="list" item="itm" index="index" open="(" separator="," close=")">  
  131.             #{itm}  
  132.         </foreach>  
  133.     </select>  
  134.       
  135.     <!-- 使用foreach 遍歷arry 只能小寫 -->  
  136.     <select id="selectStudentByIdArray" resultType="Student">  
  137.         select * from student   
  138.         where sid in  
  139.         <foreach collection="array" item="itm" index="index" open="(" separator="," close=")">  
  140.             #{itm}  
  141.         </foreach>  
  142.     </select>  
  143.       
  144.     <parameterMap type="map" id="procedureParam">  
  145.         <parameter property="sid" javaType="int" jdbcType="NUMERIC" mode="IN" />  
  146.         <parameter property="sname" javaType="String" jdbcType="VARCHAR" mode="IN" />  
  147.         <parameter property="studentList" javaType="ResultSet" jdbcType="CURSOR" mode="OUT" resultMap="studentAllResultMap"/>   
  148.     </parameterMap>  
  149.     <!--傳入map集合參數 ,調用  待用遊標存儲過程(先執行 修改後然後查詢所有)  -->  
  150.     <select id="getAllStudentAfterupdate" statementType="CALLABLE" useCache="false" parameterMap="procedureParam">  
  151.         {call LUOB.pro_getallstudent(?,?,?)}  
  152.     </select>  
  153.   
  154.  </mapper>  


ClassesMapper.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3.  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4.    
  5.  <!--注意:  
  6.     1. 這裏的 namespace 要指定到 mapperInterface 的 接口類的路徑   
  7.     2. 這裏的 statement 的id 要和 接口類中的 方法名一樣  
  8.  -->  
  9.  <mapper namespace="com.mybatis.classes.IClassesDAO">  
  10.       
  11.     <!-- 設置 緩存共享 -->  
  12.     <cache-ref namespace="com.mybatis.student"/>  
  13.       
  14.     <resultMap type="Classes" id="classesResultMap">  
  15.         <id column="CID" property="cid"/>  
  16.         <result column="CNAME" property="cname"/>  
  17.         <result column="TEACHER" property="teacher"/>  
  18.         <result column="CREATEDATE" property="createDate"/>  
  19.     </resultMap>  
  20.       
  21.     <!-- columnPrefix:別名前綴 -->  
  22.     <resultMap type="Classes" id="classesAndStudentListResultMap">  
  23.         <id column="CID" property="cid"/>  
  24.         <result column="CNAME" property="cname"/>  
  25.         <result column="TEACHER" property="teacher"/>  
  26.         <result column="CREATEDATE" property="createDate"/>  
  27.         <collection property="students" ofType="Student" resultMap="com.mybatis.student.studentResultMap" columnPrefix="stu_"/>  
  28.     </resultMap>  
  29.       
  30.     <!-- 下面採用了 別名 stu_ 來區分列名 -->  
  31.     <select id="selectClassAndStudentListById" resultMap="classesAndStudentListResultMap" parameterType="int">  
  32.         select   
  33.             c.cid,  
  34.             c.cname,  
  35.             c.teacher,  
  36.             c.createdate,  
  37.             s.sid stu_sid,  
  38.             s.sname stu_sname,  
  39.             s.score stu_score  
  40.         from student s right join classes c on s.cid=c.cid where c.cid=#{cid}  
  41.     </select>  
  42.       
  43.     <delete id="delClassesBycid" parameterType="int">  
  44.         delete classes where cid=#{cid}  
  45.     </delete>  
  46.       
  47.  </mapper>  


--dao 和 impl 
IStudentDAO.java 
Java代碼  收藏代碼
  1. package com.mybatis.student;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.mybatis.classes.Classes;  
  8.   
  9. /** 
  10.  * 手動寫dao 然後注入sqlSession 或者 繼承 
  11.  * @author Administrator 
  12.  * 
  13.  */  
  14. public interface IStudentDAO {  
  15.     //手動添加 id  
  16.     public int addStudent(Student student);  
  17.   
  18.     //自動生成 id  
  19.     public int addStudentBySequence(Student student);  
  20.   
  21.     //根據id刪除  
  22.     public int delStudentById(int id);  
  23.       
  24.     //測試更新  
  25.     public int updStudentById(Student student);  
  26.   
  27.     //查詢所有  
  28.     public List<Student> queryAllStudent();  
  29.   
  30.     //測試 模糊查詢  
  31.     public List<Student> queryStudentByName(Student name);  
  32.   
  33.     //測試 id查詢  
  34.     public Student queryStudentById(int id);  
  35.       
  36.     //測試 自定義 resultMap  
  37.     List<Student> studentResultMap(String sname);  
  38.       
  39.     //測試 左連接查詢  
  40.     List<Student> selectStudentAndClassBySname(String sname);  
  41.   
  42.     //測試 右聯合查詢  
  43.     Classes selectClassAndStudentListById(int id);  
  44.       
  45.     //測試動態sql  
  46.     List<Student> selectStudentByDynamicSql(Student student);  
  47.       
  48.     //測試動態sql  
  49.     List<Student> selectStudentByDynamicSqlChoose(Student student);  
  50.       
  51.     //測試 foreach 和集合  
  52.     List<Student> selectStudentByIds(ArrayList<Integer> ids);  
  53.       
  54.     //測試 foreach 和 數組  
  55.     List<Student> selectStudentByIdArray(Integer[] idArry);  
  56.       
  57.     //測試 map  
  58.     Map getAllStudentAfterupdate(Map map);  
  59.       
  60. }  


IStudentDAOImpl.java 
Java代碼  收藏代碼
  1. package com.mybatis.student;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.ibatis.session.SqlSession;  
  8. import org.mybatis.spring.SqlSessionTemplate;  
  9.   
  10. import com.mybatis.classes.Classes;  
  11.   
  12. /** 
  13.  *  @author Administrator 
  14.  *  如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession  
  15.  *  可以只注入sqlSessionFactory 然後 像mybatis 中使用 sqlSession 一樣  openSession()  .close()  
  16.  *  否則  可以 繼承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") 
  17.  *  和  SqlSessionTemplate 得到 spring 管理的 線程安全的 sqlSession  
  18.  *  或者 簡單的使用XML中配置  MapperFactoryBean 這樣就省去了我們 獲取sqlSession了  
  19.  */  
  20. public class IStudentDAOImpl implements IStudentDAO {  
  21.   
  22.     private SqlSessionTemplate sqlSession;  
  23.       
  24.     public SqlSessionTemplate getSqlSession() {  
  25.         return sqlSession;  
  26.     }  
  27.     public void setSqlSession(SqlSessionTemplate sqlSession) {  
  28.         this.sqlSession = sqlSession;  
  29.     }  
  30.       
  31.     public int addStudent(Student student) {  
  32.         // TODO Auto-generated method stub  
  33.           
  34.         return getSqlSession().insert("com.mybatis.student.addStudent", student);  
  35.           
  36.     }  
  37.   
  38.     public int addStudentBySequence(Student student) {  
  39.         // TODO Auto-generated method stub  
  40.         return getSqlSession().insert("com.mybatis.student.addStudentBySequence", student);  
  41.     }  
  42.   
  43.     public int delStudentById(int id) {  
  44.         int rows=getSqlSession().delete("com.mybatis.student.delStudentById", id);  
  45.         System.out.println(rows);  
  46.         return rows;  
  47.     }  
  48.   
  49.     public List<Student> queryAllStudent() {  
  50.         List<Student> stuList=new ArrayList<Student>();  
  51.         stuList=getSqlSession().selectList("com.mybatis.student.queryAllStudent");  
  52.         return stuList;  
  53.     }  
  54.   
  55.     public Student queryStudentById(int id) {  
  56.         // TODO Auto-generated method stub  
  57.         return (Student)getSqlSession().selectOne("com.mybatis.student.queryStudentById",id);  
  58.     }  
  59.   
  60.     public Map getAllStudentAfterupdate(Map map) {  
  61.         // TODO Auto-generated method stub  
  62.          getSqlSession().selectOne("com.mybatis.student.getAllStudentAfterupdate",map);  
  63.          return map;  
  64.     }  
  65.   
  66.     public Classes selectClassAndStudentListById(int id) {  
  67.         // TODO Auto-generated method stub  
  68.         return (Classes)getSqlSession().selectOne("com.mybatis.classes.selectClassAndStudentListById",id);  
  69.     }  
  70.   
  71.     public List<Student> selectStudentAndClassBySname(String sname) {  
  72.         // TODO Auto-generated method stub  
  73.         List<Student> stuList=new ArrayList<Student>();  
  74.         stuList=getSqlSession().selectList("com.mybatis.student.selectStudentAndClassBySname",sname);  
  75.         return stuList;  
  76.     }  
  77.   
  78.     public List<Student> selectStudentByDynamicSql(Student student) {  
  79.         // TODO Auto-generated method stub  
  80.         return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSql",student);  
  81.     }  
  82.   
  83.     public List<Student> selectStudentByDynamicSqlChoose(Student student) {  
  84.         // TODO Auto-generated method stub  
  85.         return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student);  
  86.     }  
  87.   
  88.     public List<Student> selectStudentByIdArray(Integer[] idArry) {  
  89.         // TODO Auto-generated method stub  
  90.         return getSqlSession().selectList("com.mybatis.student.selectStudentByIdArray",idArry);  
  91.     }  
  92.   
  93.     public List<Student> selectStudentByIds(ArrayList<Integer> ids) {  
  94.         // TODO Auto-generated method stub  
  95.         return getSqlSession().selectList("com.mybatis.student.selectStudentByIds",ids);  
  96.     }  
  97.   
  98.     public List<Student> studentResultMap(String sname) {  
  99.         // TODO Auto-generated method stub  
  100.         return getSqlSession().selectList("com.mybatis.student.selectMapResult",sname);  
  101.     }  
  102.   
  103.     public List<Student> queryStudentByName(Student name) {  
  104.         // TODO Auto-generated method stub  
  105.         List<Student> stuList=new ArrayList<Student>();  
  106.         stuList=getSqlSession().selectList("com.mybatis.student.queryStudentByName","%"+name+"%");  
  107.         return stuList;  
  108.     }  
  109.   
  110.     public int updStudentById(Student student) {  
  111.         return getSqlSession().update("com.mybatis.student.addStudentBySequence", student);  
  112.     }  
  113.   
  114. }  


IStudentDAOImpl_sqlSessionDaoSupport.java 
Java代碼  收藏代碼
  1. package com.mybatis.student;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.mybatis.spring.support.SqlSessionDaoSupport;  
  8.   
  9. import com.mybatis.classes.Classes;  
  10.   
  11. /** 
  12.  *  @author Administrator 
  13.  *  如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession  
  14.  *  可以只注入sqlSessionFactory 然後 像mybatis 中使用 sqlSession 一樣  openSession()  .close()  
  15.  *  否則  可以 繼承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") 
  16.  *  和  SqlSessionTemplate 得到 spring 管理的 線程安全的 sqlSession  
  17.  *  或者 簡單的使用XML中配置  MapperFactoryBean 這樣就省去了我們 獲取sqlSession了  
  18.  */  
  19. public class IStudentDAOImpl_sqlSessionDaoSupport  extends SqlSessionDaoSupport  implements IStudentDAO {  
  20.   
  21.     public int addStudent(Student student) {  
  22.         // TODO Auto-generated method stub  
  23.         return getSqlSession().insert("com.mybatis.student.addStudent", student);  
  24.     }  
  25.   
  26.     public int addStudentBySequence(Student student) {  
  27.         // TODO Auto-generated method stub  
  28.         return getSqlSession().insert("com.mybatis.student.addStudentBySequence", student);  
  29.     }  
  30.   
  31.     public int delStudentById(int id) {  
  32.         int rows=getSqlSession().delete("com.mybatis.student.delStudentById", id);  
  33.         System.out.println(rows);  
  34.         return rows;  
  35.     }  
  36.   
  37.     public List<Student> queryAllStudent() {  
  38.         List<Student> stuList=new ArrayList<Student>();  
  39.         stuList=getSqlSession().selectList("com.mybatis.student.queryAllStudent");  
  40.         return stuList;  
  41.     }  
  42.   
  43.     public Student queryStudentById(int id) {  
  44.         // TODO Auto-generated method stub  
  45.         return (Student)getSqlSession().selectOne("com.mybatis.student.queryStudentById",id);  
  46.     }  
  47.   
  48.     public Map getAllStudentAfterupdate(Map map) {  
  49.         // TODO Auto-generated method stub  
  50.          getSqlSession().selectOne("com.mybatis.student.getAllStudentAfterupdate",map);  
  51.          return map;  
  52.     }  
  53.   
  54.     public Classes selectClassAndStudentListById(int id) {  
  55.         // TODO Auto-generated method stub  
  56.         return (Classes)getSqlSession().selectOne("com.mybatis.classes.selectClassAndStudentListById",id);  
  57.     }  
  58.   
  59.     public List<Student> selectStudentAndClassBySname(String sname) {  
  60.         // TODO Auto-generated method stub  
  61.         List<Student> stuList=new ArrayList<Student>();  
  62.         stuList=getSqlSession().selectList("com.mybatis.student.selectStudentAndClassBySname",sname);  
  63.         return stuList;  
  64.     }  
  65.   
  66.     public List<Student> selectStudentByDynamicSql(Student student) {  
  67.         // TODO Auto-generated method stub  
  68.         return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSql",student);  
  69.     }  
  70.   
  71.     public List<Student> selectStudentByDynamicSqlChoose(Student student) {  
  72.         // TODO Auto-generated method stub  
  73.         return getSqlSession().selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student);  
  74.     }  
  75.   
  76.     public List<Student> selectStudentByIdArray(Integer[] idArry) {  
  77.         // TODO Auto-generated method stub  
  78.         return getSqlSession().selectList("com.mybatis.student.selectStudentByIdArray",idArry);  
  79.     }  
  80.   
  81.     public List<Student> selectStudentByIds(ArrayList<Integer> ids) {  
  82.         // TODO Auto-generated method stub  
  83.         return getSqlSession().selectList("com.mybatis.student.selectStudentByIds",ids);  
  84.     }  
  85.   
  86.     public List<Student> studentResultMap(String sname) {  
  87.         // TODO Auto-generated method stub  
  88.         return getSqlSession().selectList("com.mybatis.student.selectMapResult",sname);  
  89.     }  
  90.   
  91.     public List<Student> queryStudentByName(Student name) {  
  92.         // TODO Auto-generated method stub  
  93.         List<Student> stuList=new ArrayList<Student>();  
  94.         stuList=getSqlSession().selectList("com.mybatis.student.queryStudentByName","%"+name+"%");  
  95.         return stuList;  
  96.     }  
  97.   
  98.     public int updStudentById(Student student) {  
  99.         return getSqlSession().update("com.mybatis.student.addStudentBySequence", student);  
  100.     }  
  101.   
  102. }  


IStudentDAOImpl_sqlSessionTemplate.java 
Java代碼  收藏代碼
  1. package com.mybatis.student;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.ibatis.session.ExecutorType;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.mybatis.spring.SqlSessionTemplate;  
  11. import org.mybatis.spring.support.SqlSessionDaoSupport;  
  12.   
  13. import com.mybatis.classes.Classes;  
  14.   
  15. /** 
  16.  *  @author Administrator 
  17.  *  如果要直接使用 mybatis 的 sqlSession 不由spring 管理 sqlSession  
  18.  *  可以只注入sqlSessionFactory 然後 像mybatis 中使用 sqlSession 一樣  openSession()  .close()  
  19.  *  否則  可以 繼承 SqlSessionDaoSupport ("getSqlSession() insert/select/delete/update") 
  20.  *  和  SqlSessionTemplate 得到 spring 管理的 線程安全的 sqlSession  
  21.  *  或者 簡單的使用XML中配置  MapperFactoryBean 這樣就省去了我們 獲取sqlSession了  
  22.  */  
  23. public class IStudentDAOImpl_sqlSessionTemplate extends SqlSessionTemplate implements IStudentDAO {  
  24.   
  25.     //同樣是 創建一個 可以批量操作的 sqlSession  
  26.     public IStudentDAOImpl_sqlSessionTemplate(  
  27.             SqlSessionFactory sqlSessionFactory) {  
  28.         super(sqlSessionFactory);  
  29.         // TODO Auto-generated constructor stub  
  30.     }  
  31.   
  32.     public int addStudent(Student student) {  
  33.         // TODO Auto-generated method stub  
  34.           
  35.         return this.insert("com.mybatis.student.addStudent", student);  
  36.           
  37.     }  
  38.   
  39.     public int addStudentBySequence(Student student) {  
  40.         // TODO Auto-generated method stub  
  41.         return this.insert("com.mybatis.student.addStudentBySequence", student);  
  42.     }  
  43.   
  44.     public int delStudentById(int id) {  
  45.         int rows=this.delete("com.mybatis.student.delStudentById", id);  
  46.         System.out.println(rows);  
  47.         return rows;  
  48.     }  
  49.   
  50.     public List<Student> queryAllStudent() {  
  51.         List<Student> stuList=new ArrayList<Student>();  
  52.         stuList=this.selectList("com.mybatis.student.queryAllStudent");  
  53.         return stuList;  
  54.     }  
  55.   
  56.     public Student queryStudentById(int id) {  
  57.         // TODO Auto-generated method stub  
  58.         return (Student)this.selectOne("com.mybatis.student.queryStudentById",id);  
  59.     }  
  60.   
  61.     public Map getAllStudentAfterupdate(Map map) {  
  62.         // TODO Auto-generated method stub  
  63.          this.selectOne("com.mybatis.student.getAllStudentAfterupdate",map);  
  64.          return map;  
  65.     }  
  66.   
  67.     public Classes selectClassAndStudentListById(int id) {  
  68.         // TODO Auto-generated method stub  
  69.         return (Classes)this.selectOne("com.mybatis.classes.selectClassAndStudentListById",id);  
  70.     }  
  71.   
  72.     public List<Student> selectStudentAndClassBySname(String sname) {  
  73.         // TODO Auto-generated method stub  
  74.         List<Student> stuList=new ArrayList<Student>();  
  75.         stuList=this.selectList("com.mybatis.student.selectStudentAndClassBySname",sname);  
  76.         return stuList;  
  77.     }  
  78.   
  79.     public List<Student> selectStudentByDynamicSql(Student student) {  
  80.         // TODO Auto-generated method stub  
  81.         return this.selectList("com.mybatis.student.selectStudentByDynamicSql",student);  
  82.     }  
  83.   
  84.     public List<Student> selectStudentByDynamicSqlChoose(Student student) {  
  85.         // TODO Auto-generated method stub  
  86.         return this.selectList("com.mybatis.student.selectStudentByDynamicSqlChoose",student);  
  87.     }  
  88.   
  89.     public List<Student> selectStudentByIdArray(Integer[] idArry) {  
  90.         // TODO Auto-generated method stub  
  91.         return this.selectList("com.mybatis.student.selectStudentByIdArray",idArry);  
  92.     }  
  93.   
  94.     public List<Student> selectStudentByIds(ArrayList<Integer> ids) {  
  95.         // TODO Auto-generated method stub  
  96.         return this.selectList("com.mybatis.student.selectStudentByIds",ids);  
  97.     }  
  98.   
  99.     public List<Student> studentResultMap(String sname) {  
  100.         // TODO Auto-generated method stub  
  101.         return this.selectList("com.mybatis.student.selectMapResult",sname);  
  102.     }  
  103.   
  104.     public List<Student> queryStudentByName(Student name) {  
  105.         // TODO Auto-generated method stub  
  106.         List<Student> stuList=new ArrayList<Student>();  
  107.         stuList=this.selectList("com.mybatis.student.queryStudentByName","%"+name+"%");  
  108.         return stuList;  
  109.     }  
  110.   
  111.     public int updStudentById(Student student) {  
  112.         return this.update("com.mybatis.student.addStudentBySequence", student);  
  113.     }  
  114.   
  115. }  


IClassesDAO.java 
Java代碼  收藏代碼
  1. package com.mybatis.classes;  
  2.   
  3. /** 
  4.  * 使用  MapperFactoryBean 來管理 sqlSession  
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public interface IClassesDAO {  
  9.     // 也可以在這裏使用註解配置 sql語句  這樣就不用寫 映射文件了  
  10.     Classes selectClassAndStudentListById(int cid);  
  11.     int delClassesBycid(int cid);  
  12. }  


Student.java 
Java代碼  收藏代碼
  1. package com.mybatis.student;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import com.mybatis.classes.Classes;  
  7.   
  8. public class Student implements Serializable {  
  9. private int sid;  
  10. private String sname;  
  11. private String major;  
  12. private Date birth;  
  13. private float score;  
  14. private int cid;  
  15. private int status;  
  16.   
  17. //get set()  
  18. }  


Classes.java 
Java代碼  收藏代碼
  1. package com.mybatis.classes;  
  2.   
  3. import java.sql.Date;  
  4. import java.util.List;  
  5.   
  6. import com.mybatis.student.Student;  
  7.   
  8. public class Classes {  
  9.     private int cid;  
  10.     private String cname;  
  11.     private String teacher;  
  12.     private Date createDate;  
  13.   
  14.     private List<Student> students;  
  15.     //get set  
  16. }  


BaseAction.java 
Java代碼  收藏代碼
  1. package com.mybatis.common;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import javax.servlet.ServletContext;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10. import org.springframework.web.context.WebApplicationContext;  
  11. import org.springframework.web.context.support.WebApplicationContextUtils;  
  12.   
  13. import com.opensymphony.xwork2.ActionContext;  
  14. import com.opensymphony.xwork2.ActionSupport;  
  15.   
  16. public class BaseAction extends ActionSupport{  
  17.   
  18.     public Object getServiceBean(String beanId){  
  19.         ServletContext sc=ServletActionContext.getServletContext();  
  20.         WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(sc);  
  21.         return ctx.getBean(beanId);  
  22.     }  
  23.       
  24.     public HttpServletRequest getRequest(){  
  25.         return ServletActionContext.getRequest();  
  26.     }  
  27.       
  28.     public HttpServletResponse getResponse(){  
  29.         return ServletActionContext.getResponse();  
  30.     }  
  31.       
  32.     public Map<String, Object> getSession() {  
  33.         ActionContext act=ActionContext.getContext();  
  34.         return act.getSession();  
  35.     }  
  36. }  


StudentAction.java 
Java代碼  收藏代碼
  1. package com.mybatis.action;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8.   
  9. import com.mybatis.common.BaseAction;  
  10. import com.mybatis.student.IStudentService;  
  11. import com.mybatis.student.Student;  
  12.   
  13. public class StudentAction extends BaseAction {  
  14.   
  15.     private Student student;  
  16.     private List<Student> stuList;  
  17.       
  18.     public String getAllStudent(){  
  19.         try {  
  20.             IStudentService stuService=(IStudentService)this.getServiceBean("studentService");  
  21.             stuList=stuService.queryAllStudent();  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         return SUCCESS;  
  26.     }  
  27.       
  28.     public String delStudentById(){  
  29.         try {  
  30.             HttpServletRequest request=this.getRequest();  
  31.             IStudentService stuService=(IStudentService)this.getServiceBean("studentService");  
  32.             int rows=stuService.delStudentById(student.getSid());  
  33.             if(rows>0){  
  34.                 request.setAttribute("msg""<script type='text/javascript'>alert('刪除成功!');</script>");  
  35.             }else{  
  36.                 request.setAttribute("msg""<script type='text/javascript'>alert('刪除失敗!');</script>");  
  37.             }  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return getAllStudent();  
  42.           
  43.     }  
  44.       
  45.     public String queryStudentById(){  
  46.         try {  
  47.             IStudentService stuService=(IStudentService)this.getServiceBean("studentService");  
  48.             student=stuService.queryStudentById(student.getSid());  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.         return SUCCESS;  
  53.     }  
  54.       
  55.     public String getAllStudentAfterupdate(){  
  56.         try {  
  57.             Map map=new HashMap();  
  58.             map.put("sid", student.getSid());  
  59.             map.put("sname", student.getSname());  
  60.             IStudentService stuService=(IStudentService)this.getServiceBean("studentService");  
  61.             map=stuService.getAllStudentAfterupdate(map);  
  62.             stuList=(List<Student>)map.get("studentList");  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.         return SUCCESS;  
  67.     }  
  68.   
  69.       
  70.     public List<Student> getStuList() {  
  71.         return stuList;  
  72.     }  
  73.   
  74.     public void setStuList(List<Student> stuList) {  
  75.         this.stuList = stuList;  
  76.     }  
  77.   
  78.     public Student getStudent() {  
  79.         return student;  
  80.     }  
  81.   
  82.     public void setStudent(Student student) {  
  83.         this.student = student;  
  84.     }  
  85.       
  86.       
  87. }  


ClassesAction.java 
Java代碼  收藏代碼
  1. package com.mybatis.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import com.mybatis.classes.Classes;  
  6. import com.mybatis.classes.IClassesService;  
  7. import com.mybatis.common.BaseAction;  
  8.   
  9. public class ClassesAction extends BaseAction {  
  10.   
  11.     private Classes classes;  
  12.       
  13.     /** 
  14.      * 根據 id 刪除 
  15.      * @return 
  16.      */  
  17.     public String delClassesById(){  
  18.         try {  
  19.             HttpServletRequest request=this.getRequest();  
  20.             IClassesService classesService=(IClassesService)this.getServiceBean("classesService");  
  21.             int rows=classesService.delClassesBycid(classes.getCid());  
  22.             if(rows>0){  
  23.                 request.setAttribute("msg""<script type='text/javascript'>alert('刪除成功!');</script>");  
  24.             }else{  
  25.                 request.setAttribute("msg""<script type='text/javascript'>alert('刪除失敗!');</script>");  
  26.             }  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         return SUCCESS;  
  31.     }  
  32.       
  33.       
  34.     /** 
  35.      * 測試 右連接查詢  會查詢出 班級中擁有的學生  
  36.      * @return 
  37.      */  
  38.     public String queryClassesById(){  
  39.         try {  
  40.             IClassesService classesService=(IClassesService)this.getServiceBean("classesService");  
  41.             classes=classesService.selectClassAndStudentListById(classes.getCid());  
  42.         } catch (Exception e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         return SUCCESS;  
  46.     }  
  47.     public Classes getClasses() {  
  48.         return classes;  
  49.     }  
  50.     public void setClasses(Classes classes) {  
  51.         this.classes = classes;  
  52.     }  
  53.       
  54. }  


index.jsp 
Html代碼  收藏代碼
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  
  5. <%  
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12.   <head>  
  13.     <base href="<%=basePath%>">  
  14.       
  15.     <title>MyBatis3.11 + Spring3.1.2 + Struts2</title>  
  16.   </head>  
  17.     
  18.   <body>  
  19.      <form action="${pageContext.request.contextPath}/student/updateAndSelect.action" method="post">  
  20.         <table cellpadding="0" cellspacing="0" bgcolor="#F4F4F4">  
  21.             <tr>  
  22.                 <td>學生id</td>  
  23.                 <td><input type="text" name="student.sid"/></td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td>學生姓名</td>  
  27.                 <td><input type="text" name="student.sname"/></td>  
  28.             </tr>  
  29.         </table>  
  30.         <input type="submit" value="修改姓名後測試procedure"/>  
  31.      </form>  
  32.      <input type="button" onclick="window.location.href='${pageContext.request.contextPath}/student/allStudent.action'" value="獲取所有學生"/><br/>  
  33.      <c:if test="${fn:length(stuList)>0}">  
  34.      <table style="border: 1px solid blue;" border="1px" cellspacing="0" bgcolor="#F4F4F4">  
  35.         <tr align="center">  
  36.             <td width="100px">sid</td>  
  37.             <td width="100px">sname</td>  
  38.             <td width="100px">major</td>  
  39.             <td width="150px">birth</td>  
  40.             <td width="100px">score</td>  
  41.             <td width="100px">操作</td>  
  42.         </tr>  
  43.         <c:forEach items="${stuList}" var="student">  
  44.         <tr>  
  45.             <td width="100px">${student.sid}</td>  
  46.             <td width="100px">${student.sname}</td>  
  47.             <td width="100px">${student.major}</td>  
  48.             <td width="180px"><fmt:formatDate value="${student.birth}" pattern="yyyy-MM-dd HH:mm:ss"/><br></td>  
  49.             <td width="100px">${student.score}</td>  
  50.             <td><input type="button" onclick="window.location.href='${pageContext.request.contextPath}/student/delStudentById.action?student.sid=12'" value="刪除"/> </td>  
  51.         </tr>  
  52.      </c:forEach>  
  53.      </table>  
  54.      </c:if>  
  55.      ${msg}  
  56.   </body>  
  57. </html>  


index1.jsp 
Html代碼  收藏代碼
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  
  5. <%  
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12.   <head>  
  13.     <base href="<%=basePath%>">  
  14.       
  15.     <title>MyBatis3.11 + Spring3.1.2 + Struts2</title>  
  16.   </head>  
  17.     
  18.   <body>  
  19.      <form action="${pageContext.request.contextPath}/classes/queryClassesById.action" method="post">  
  20.         <table cellpadding="0" cellspacing="0" bgcolor="#F4F4F4">  
  21.             <tr>  
  22.                 <td>班級id</td>  
  23.                 <td><input type="text" name="classes.cid"/></td>  
  24.             </tr>  
  25.         </table>  
  26.         <input type="submit" value="查詢"/>  
  27.      </form>  
  28.      <input type="button" onclick="window.location.href='${pageContext.request.contextPath}/classes/allStudent.action'" value="獲取所有學生"/><br/>  
  29.      <table style="border: 1px solid blue;" border="1px" cellspacing="0" bgcolor="#F4F4F4">  
  30.         <tr>  
  31.             <td width="100px">cid</td>  
  32.             <td width="100px">cname</td>  
  33.             <td width="100px">teacher</td>  
  34.             <td width="100px">crateDate</td>  
  35.             <td width="100px">操作</td>  
  36.         </tr>  
  37.         <tr>  
  38.             <td>${classes.cid}</td>  
  39.             <td>${classes.cname}</td>  
  40.             <td>${classes.teacher}</td>  
  41.             <td>${classes.createDate}</td>  
  42.             <td width="100px"><input type="button" onclick="window.location.href='${pageContext.request.contextPath}/classes/delClassesById.action?classes.cid=12'" value="刪除"/> </td></td>  
  43.         </tr>  
  44.      <c:if test="${fn:length(classes.students)>0}">  
  45.         <tr align="center">  
  46.             <td width="100px">sid</td>  
  47.             <td width="100px">sname</td>  
  48.             <td width="150px">birth</td>  
  49.             <td width="100px">score</td>  
  50.             <td width="100px"></td>  
  51.         </tr>  
  52.         <c:forEach items="${classes.students}" var="student">  
  53.         <tr>  
  54.             <td width="100px">${student.sid}</td>  
  55.             <td width="100px">${student.sname}</td>  
  56.             <td width="180px"><fmt:formatDate value="${student.birth}" pattern="yyyy-MM-dd HH:mm:ss"/><br></td>  
  57.             <td width="100px">${student.score}</td>  
  58.             <td><input type="button" onclick="window.location.href='${pageContext.request.contextPath}/student/delStudentById.action?student.sid=12'" value="刪除"/> </td>  
  59.         </tr>  
  60.      </c:forEach>  
  61.      </c:if>  
  62.       </table>  
  63.      ${msg}  
  64.   </body>  
  65. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章