struts2+ibatis+spring框架整合(二)

MyBatis3.1.1+Spring3.1.2+Struts2.3.4.1框架整合

先来看目录结构

来看配置文件

applicationContext.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

    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

    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

 

    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

    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

    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

 

    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.       

IStudentDAOImpl.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.   

IStudentDAOImpl_sqlSessionDaoSupport.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.   

 

IStudentDAOImpl_sqlSessionTemplate.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.   

IClassesDAO.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);  

Student.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()  

 

Classes.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  

BaseAction.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.     }  

StudentAction.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.       

ClassesAction.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.       

index.jsp

    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

    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> 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/SunDexu/archive/2013/05/24/3097698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值