5、Spring+Struts2+MyBatis+分页(无代理)增删改查

1、创建如下项目结构

2、在src下的com.entity包下创建Dept.java

 1 package com.entity;
 2 /**
 3  * 部门表
 4  * @author Holly老师
 5  *
 6  */
 7 public class Dept {
 8     private Integer deptno; //部门编号
 9     private String dname; //部门名称
10     private String loc; //位置
11
12
13     public Dept() {
14     }
15
16     public Dept(Integer deptno, String dname, String loc) {
17         this.deptno = deptno;
18         this.dname = dname;
19         this.loc = loc;
20     }
21
22     public Integer getDeptno() {
23         return deptno;
24     }
25
26     public void setDeptno(Integer deptno) {
27         this.deptno = deptno;
28     }
29
30     public String getDname() {
31         return dname;
32     }
33
34     public void setDname(String dname) {
35         this.dname = dname;
36     }
37
38     public String getLoc() {
39         return loc;
40     }
41
42     public void setLoc(String loc) {
43         this.loc = loc;
44     }
45
46 }

Dept.java

3、在src下的com.page包下创建Page.java

 1 package com.page;
 2 /**
 3  *
 4  * @author Holly老师
 5  *
 6  */
 7 public class Page {
 8     private Integer pageno;
 9     private Integer pagesize;
10     private Integer totalcount;
11     private Integer totalpage;
12     private Integer startrow;
13     private Integer endrow;
14
15
16     public Page() {
17     }
18     public Page(Integer pageno, Integer pagesize, Integer totalcount) {
19         this.pageno = pageno;
20         this.pagesize = pagesize;
21         this.startrow=pageno*pagesize;
22         this.endrow=(pageno-1)*pagesize;
23         this.totalcount = totalcount;
24         this.setTotalpage(totalcount);
25     }
26     public Integer getPageno() {
27         return pageno;
28     }
29     public void setPageno(Integer pageno) {
30         this.pageno = pageno;
31     }
32     public Integer getPagesize() {
33         return pagesize;
34     }
35     public void setPagesize(Integer pagesize) {
36         this.pagesize = pagesize;
37     }
38     public Integer getTotalcount() {
39         return totalcount;
40     }
41     public void setTotalcount(Integer totalcount) {
42         this.totalcount = totalcount;
43     }
44     public Integer getTotalpage() {
45         return totalpage;
46     }
47     /**
48      * 总条数
49      * @param totalcount
50      */
51     public void setTotalpage(Integer totalcount) {
52         this.totalpage = totalcount%pagesize==0? totalcount/pagesize:totalcount/pagesize+1;
53     }
54     public Integer getStartrow() {
55         return startrow;
56     }
57     public void setStartrow(Integer startrow) {
58         this.startrow = startrow;
59     }
60     public Integer getEndrow() {
61         return endrow;
62     }
63     public void setEndrow(Integer endrow) {
64         this.endrow = endrow;
65     }
66
67
68 }

Page.java

4、在src下的com.mapper包下创建DeptMapper.java

 1 package com.mapper;
 2
 3 import java.util.List;
 4
 5 import com.entity.Dept;
 6 import com.page.Page;
 7
 8 /**
 9  * 数据访问层接口
10  * @author Holly老师
11  *
12  */
13 public interface DeptMapper {
14
15     //查询所有
16     public List<Dept> findAll();
17     //分页查询
18     public List<Dept> findPage(Page page);
19     //查询总记录数
20     public Integer findCount();
21     //根据id查询
22     public Dept findById(int id);
23     //保存对象
24     public int saveDept(Dept dept);
25     //修改对象
26     public int updateDept(Dept dept);
27     //根据id删除
28     public int deleteById(int id);
29 }

DeptMapper.java

5、在src下的com.mapper包下创建DeptMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.mapper.DeptMapper">
 4 <!-- 查询所有 -->
 5   <select id="findAll" resultType="dept">
 6     select * from dept
 7   </select>
 8   <!-- 分页查询 -->
 9   <select id="findPage" resultType="dept" parameterType="page">
10   <![CDATA[
11      select * from
12      (
13        select rownum r,deptno,dname,loc from dept
14        where rownum<=#{startrow}
15        )d
16       where d.r> #{endrow}
17   ]]>
18   </select>
19   <!-- 查询总记录数 -->
20   <select id="findCount" resultType="Integer">
21     select count(*) from dept
22   </select>
23   <!-- 根据id查询 -->
24   <select id="findById" parameterType="int" resultType="dept">
25     select * from dept where deptno=#{deptno}
26   </select>
27   <!-- 保存对象 -->
28   <insert id="saveDept" parameterType="dept">
29      insert into dept values(#{deptno},#{dname},#{loc})
30   </insert>
31   <!-- 修改对象 -->
32   <update id="updateDept" parameterType="dept">
33      update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
34   </update>
35   <!-- 根据id删除 -->
36   <delete id="deleteById" parameterType="int">
37      delete from dept where deptno=#{deptno}
38   </delete>
39
40 </mapper>

DeptMapper.xml

6、在src下创建mybatis-config.xml主配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
 3 <configuration>
 4 <!-- 别名映射 -->
 5   <typeAliases>
 6     <typeAlias type="com.entity.Dept" alias="dept"/>
 7     <typeAlias type="com.page.Page" alias="page"/>
 8   </typeAliases>
 9   <!-- 注册xml文件 -->
10   <mappers>
11     <mapper resource="com/mapper/DeptMapper.xml"/>
12   </mappers>
13 </configuration>

mybatis-config.xml

7、在src下创建log4j.properties日志属性文件

 1 log4j.rootLogger=DEBUG, Console
 2
 3 #Console
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender
 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout
 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
 7
 8 log4j.logger.java.sql.ResultSet=INFO
 9 log4j.logger.org.apache=INFO
10 log4j.logger.java.sql.Connection=DEBUG
11 log4j.logger.java.sql.Statement=DEBUG
12 log4j.logger.java.sql.PreparedStatement=DEBUG   

log4j.properties

8、在src下的com.mapper.impl包下创建DeptMapperImpl.java

  1 package com.mapper.impl;
  2
  3 import java.util.List;
  4
  5 import org.mybatis.spring.SqlSessionTemplate;
  6
  7 import com.entity.Dept;
  8 import com.mapper.DeptMapper;
  9 import com.page.Page;
 10 /**
 11  * 数据访问层接口的实现类
 12  * @author Holly老师
 13  *
 14  */
 15 public class DeptMapperImpl implements DeptMapper {
 16     //注入seqsession对象
 17     private SqlSessionTemplate sqlSessionTemplate;
 18
 19     //必须存在getter和setter否则xml文件不能注入,会报没有session
 20     public SqlSessionTemplate getSqlSessionTemplate() {
 21         return sqlSessionTemplate;
 22     }
 23
 24     public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
 25         this.sqlSessionTemplate = sqlSessionTemplate;
 26     }
 27     /**
 28      * 1.根据id删除
 29      */
 30     public int deleteById(int id) {
 31         int num=sqlSessionTemplate.delete("com.mapper.DeptMapper.deleteById", id);
 32         if(num>0){
 33             System.out.println("删除成功");
 34         }else{
 35             System.out.println("删除失败");
 36         }
 37         return num;
 38     }
 39
 40     /**
 41      * 2.添加部门信息
 42      */
 43     public int saveDept(Dept dept) {
 44         int num=sqlSessionTemplate.insert("com.mapper.DeptMapper.saveDept", dept);
 45         if(num>0){
 46             System.out.println("添加成功");
 47         }else{
 48             System.out.println("添加失败");
 49         }
 50         return num;
 51     }
 52     /**
 53      * 3.修改
 54      */
 55     public int updateDept(Dept dept) {
 56         int num=sqlSessionTemplate.update("com.mapper.DeptMapper.updateDept", dept);
 57         if(num>0){
 58             System.out.println("修改成功");
 59         }else{
 60             System.out.println("修改失败");
 61         }
 62         return num;
 63     }
 64
 65     /**
 66      * 4.查询所有
 67      */
 68     public List<Dept> findAll() {
 69         List<Dept> list=sqlSessionTemplate.selectList("com.mapper.DeptMapper.findAll");
 70         if(list!=null){
 71             System.out.println("查到所有");
 72         }else{
 73             System.out.println("没有查到所有");
 74         }
 75         return list;
 76     }
 77     /**
 78      * 5.根据id没有查到
 79      */
 80     public Dept findById(int id) {
 81         Dept dept=sqlSessionTemplate.selectOne("com.mapper.DeptMapper.findById",id);
 82         if(dept!=null){
 83             System.out.println("根据id查到");
 84         }else{
 85             System.out.println("根据id没有查到");
 86         }
 87         return dept;
 88     }
 89    /**
 90     * 分页查询
 91     */
 92     public List<Dept> findPage(Page page) {
 93         List<Dept> list=sqlSessionTemplate.selectList("com.mapper.DeptMapper.findPage",page);
 94         if(list!=null){
 95             System.out.println("分页查到了");
 96         }else{
 97             System.out.println("分页没有查到");
 98         }
 99         return list;
100     }
101    /**
102     * 查询总记录数
103     */
104     public Integer findCount() {
105         Integer totalCount=sqlSessionTemplate.selectOne("com.mapper.DeptMapper.findCount");
106         return totalCount;
107     }
108
109
110
111
112 }

DeptMapperImpl.java

9、在src下的com.service包下创建DeptService.java

 1 package com.service;
 2
 3 import java.util.List;
 4
 5 import com.entity.Dept;
 6 import com.page.Page;
 7 /**
 8  * 业务逻辑层接口
 9  * @author Holly老师
10  *
11  */
12 public interface DeptService {
13     //查询所有
14     public List<Dept> findAll();
15     //分页查询
16     public List<Dept> findPage(Page page);
17     //查询总记录数
18     public Integer findCount();
19     //根据id查询
20     public Dept findById(int id);
21     //保存对象
22     public int saveDept(Dept dept);
23     //修改对象
24     public int updateDept(Dept dept);
25     //根据id删除
26     public int deleteById(int id);
27
28 }

DeptService.java

10、在src下的com.service.impl包下创建DeptServiceImpl.java

 1 package com.service.impl;
 2
 3 import java.util.List;
 4
 5 import com.entity.Dept;
 6 import com.mapper.DeptMapper;
 7 import com.page.Page;
 8 import com.service.DeptService;
 9 /**
10  * 业务逻辑层接口实现类
11  * @author Holly老师
12  *
13  */
14 public class DeptServiceImpl implements DeptService {
15     //注入数据访问层对象
16     private DeptMapper dao;
17     //必须存在getter和setter,否则xml注入不了
18     public DeptMapper getDao() {
19         return dao;
20     }
21
22     public void setDao(DeptMapper dao) {
23         this.dao = dao;
24     }
25     /**
26      * 1.删除
27      */
28     public int deleteById(int id) {
29         return dao.deleteById(id);
30     }
31     /**
32      * 2.查询所有
33      */
34     public List<Dept> findAll() {
35         return dao.findAll();
36     }
37    /**
38     * 3.根据id查询
39     */
40     public Dept findById(int id) {
41         return dao.findById(id);
42     }
43     /**
44      * 4.保存对象
45      */
46     public int saveDept(Dept dept) {
47         return dao.saveDept(dept);
48     }
49     /**
50      * 5.修改对象
51      */
52     public int updateDept(Dept dept) {
53         return dao.updateDept(dept);
54     }
55   /**
56    * 6.分页查询
57    */
58     public List<Dept> findPage(Page page) {
59         return dao.findPage(page);
60     }
61 /**
62  * 7.查询总记录数
63  */
64 public Integer findCount() {
65     return dao.findCount();
66 }
67
68 }

DeptServiceImpl.java

11、在src下的com.action包下创建DeptAction.java

  1 package com.action;
  2
  3 import java.util.List;
  4
  5 import javax.servlet.http.HttpServletRequest;
  6
  7 import org.apache.struts2.ServletActionContext;
  8
  9 import com.entity.Dept;
 10 import com.opensymphony.xwork2.ActionSupport;
 11 import com.page.Page;
 12 import com.service.DeptService;
 13 /**
 14  *
 15  * @author Holly老师
 16  *
 17  */
 18 public class DeptAction extends ActionSupport {
 19
 20      //注入业务逻辑层对象
 21      private DeptService service;
 22
 23      //struts标签中动态代理赋值对象
 24      private Dept dept;
 25
 26      //分页对象
 27      private Page page;
 28
 29      /**
 30       * 1.查询所有
 31       * @return
 32       */
 33      public String findAll(){
 34          List<Dept> list=service.findAll();
 35          if(list!=null){
 36              HttpServletRequest request=ServletActionContext.getRequest();
 37              request.setAttribute("list", list);
 38              return SUCCESS;
 39          }else{
 40              return ERROR;
 41          }
 42
 43      }
 44
 45      /**
 46       * 1.分页查询所有
 47       * @return
 48       */
 49      public String findPage(){
 50          //当前页
 51          Integer pageNo=1;
 52          if(page!=null){
 53              System.out.println("page不为空");
 54              pageNo=page.getPageno();
 55          }else{
 56              System.out.println("page为空");
 57          }
 58          //页面大小
 59          Integer pageSize=3;
 60          //查询总条数
 61          Integer totalCount= service.findCount();
 62          page=new Page(pageNo, pageSize, totalCount);
 63          List<Dept> list=service.findPage(page);
 64          if(list!=null){
 65              HttpServletRequest request=ServletActionContext.getRequest();
 66              request.setAttribute("list", list);
 67              request.setAttribute("page", page);
 68              return SUCCESS;
 69          }else{
 70              return ERROR;
 71          }
 72
 73      }
 74      /**
 75       * 根据id查询
 76       * @return
 77       */
 78      public String findById(){
 79          if(dept!=null){
 80              Dept dt=service.findById(dept.getDeptno());
 81              if(dt!=null){
 82                  HttpServletRequest request=ServletActionContext.getRequest();
 83                  request.setAttribute("dt", dt);
 84                  return SUCCESS;
 85              }
 86          }
 87             return ERROR;
 88
 89      }
 90      /**
 91       * 添加
 92       * @return
 93       */
 94      public String save(){
 95          if(dept!=null){
 96              int num=service.saveDept(dept);
 97              if(num>0){
 98                  return SUCCESS;
 99              }else{
100                  return ERROR;
101              }
102          }
103          return ERROR;
104      }
105      /**
106       * 修改
107       * @return
108       */
109      public String update(){
110          if(dept!=null){
111              int num=service.updateDept(dept);
112              if(num>0){
113                  return SUCCESS;
114              }else{
115                  return ERROR;
116              }
117          }
118          return ERROR;
119
120      }
121      /**
122       * 删除
123       * @return
124       */
125      public String delete(){
126          if(dept!=null){
127              int num=service.deleteById(dept.getDeptno());
128              if(num>0){
129                  return SUCCESS;
130              }else{
131                  return ERROR;
132              }
133          }
134          return ERROR;
135      }
136
137
138     public DeptService getService() {
139         return service;
140     }
141
142     public void setService(DeptService service) {
143         this.service = service;
144     }
145
146     public Dept getDept() {
147         return dept;
148     }
149
150     public void setDept(Dept dept) {
151         this.dept = dept;
152     }
153
154     public Page getPage() {
155         return page;
156     }
157
158     public void setPage(Page page) {
159         this.page = page;
160     }
161
162
163 }

DeptAction.java

12、在src下创建Spring的配置文件applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:tx="http://www.springframework.org/schema/tx"
 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans
 6 http://www.springframework.org/schema/beans/spring-beans.xsd ">
 7  <!-- 1.数据源配置 -->
 8  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 9    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
10    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
11    <property name="username" value="scott"/>
12    <property name="password" value="tiger"/>
13  </bean>
14
15  <!-- 2.配置事务 -->
16  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
17     <!--  事务中注入数据源-->
18     <property name="dataSource" ref="dataSource"/>
19  </bean>
20
21  <!-- 3.配置SqlSessionFactory -->
22  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
23     <!-- 注入数据源 -->
24     <property name="dataSource" ref="dataSource"/>
25     <!-- 注入mybaits主配置文件 -->
26     <property name="configLocation">
27       <value>classpath:mybatis-config.xml</value>
28     </property>
29  </bean>
30
31  <!-- 4.获取SqlSessionTemplate -->
32  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
33     <!--通过构造注入sqlsessionFactory-->
34     <!-- index表示构造的参数索引位置,type表示构造参数的类型-->
35     <constructor-arg index="0" ref="sqlSessionFactory"/>
36  </bean>
37
38  <!-- 5.实例化dao -->
39  <bean id="dao" class="com.mapper.impl.DeptMapperImpl">
40    <!-- 注入sqlSessionTemplate -->
41    <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
42  </bean>
43
44  <!-- 6.实例化service -->
45  <bean id="service" class="com.service.impl.DeptServiceImpl">
46     <!-- 注入dao -->
47     <property name="dao" ref="dao"/>
48  </bean>
49
50  <!-- 7.实例化action -->
51  <bean id="DeptAction" class="com.action.DeptAction">
52    <!-- 注入service -->
53    <property name="service" ref="service"/>
54  </bean>
55
56 </beans>

applicationContext.xml

13、在src下创建Struts2的配置文件struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "struts-2.1.7.dtd" >
 3 <struts>
 4    <!-- 中文乱码处理 -->
 5   <constant name="struts.i18n.encoding" value="UTF-8"/>
 6     <package name="default" namespace="/" extends="struts-default">
 7          <!-- 默认首起项配置
 8
 9          <default-action-ref name="findPage" />
10          -->
11          <!-- ssm框架集合时,class的值是spring配置文件的实例化action的bean的id值 -->
12          <!-- 1.查询所有 -->
13          <action name="findAll" class="DeptAction" method="findAll">
14            <result name="success">index.jsp</result>
15            <result name="error">fail.jsp</result>
16          </action>
17
18          <!-- 1.查询所有 -->
19          <action name="findPage" class="DeptAction" method="findPage">
20            <result name="success">index.jsp</result>
21            <result name="error">fail.jsp</result>
22          </action>
23
24          <!-- 2.根据id查询 -->
25          <action name="findById" class="DeptAction" method="findById">
26            <result name="success">update.jsp</result>
27            <!-- 重定向到action -->
28            <result name="error" type="redirectAction">findAll</result>
29          </action>
30
31          <!-- 3.添加 -->
32          <action name="save" class="DeptAction" method="save">
33            <!-- 重定向到action -->
34            <result name="success" type="redirectAction">findAll</result>
35            <result name="error">insert.jsp</result>
36          </action>
37
38          <!-- 4.修改 -->
39          <action name="update" class="DeptAction" method="update">
40            <!-- 重定向到action -->
41            <result name="success" type="redirectAction">findAll</result>
42            <result name="error">fail.jsp</result>
43          </action>
44
45          <!-- 5.删除 -->
46          <action name="delete" class="DeptAction" method="delete">
47            <!-- 重定向到action -->
48            <result name="success" type="redirectAction">findAll</result>
49            <result name="error">fail.jsp</result>
50          </action>
51
52
53     </package>
54
55 </struts>

struts.xml

14、编辑WebRoot下的WEB-INF下web.xml

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

web.xml

15、在WebRoot下创建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 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12
13     <title>My JSP ‘index.jsp‘ starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23
24   <body>
25      <center>
26        <a href="insert.jsp">添加</a>
27        <table border="1">
28           <tr><td>部门编号</td><td>部门名称</td><td>部门地址</td><td>操作</td></tr>
29           <c:forEach var="i" items="${requestScope.list}">
30             <tr>
31               <td>${i.deptno}</td>
32               <td>${i.dname}</td>
33               <td>${i.loc}</td>
34               <td>
35                    <a href="delete.action?dept.deptno=${i.deptno}">删除</a>
36                    &nbsp;|&nbsp;
37                    <a href="findById.action?dept.deptno=${i.deptno}">修改</a>
38               </td>
39             </tr>
40           </c:forEach>
41        </table>
42        第${page.pageno}/${page.totalpage }页
43        <a href="findPage.action?page.pageno=1">首页</a>&nbsp;
44        <c:choose>
45         <c:when test="${page.pageno>1}">
46        <a href="findPage.action?page.pageno=${page.pageno-1 }">上一页</a>&nbsp;
47         </c:when>
48         <c:otherwise>
49        <a href="javascript:alert(‘已经是第一页了!‘);">上一页</a>&nbsp;
50
51         </c:otherwise>
52        </c:choose>
53
54        <c:choose>
55         <c:when test="${page.pageno<page.totalpage}">
56        <a href="findPage.action?page.pageno=${page.pageno+1 }">下一页</a>&nbsp;
57         </c:when>
58         <c:otherwise>
59        <a href="javascript:alert(‘已经是最后一页了!‘);">上一页</a>&nbsp;
60
61         </c:otherwise>
62        </c:choose>
63        <a href="findPage.action?page.pageno=${page.totalpage}">末页</a>&nbsp;
64        总${page.totalcount }条
65      </center>
66   </body>
67 </html>

index.jsp

16、在WebRoot下创建insert.jsp文件

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12
13     <title>My JSP ‘index.jsp‘ starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23
24   <body>
25      <center>
26        <form action="save.action" method="post">
27            <table border="1">
28              <tr>
29                <td>部门编号:</td>
30                <td><input type="text" name="dept.deptno"/></td>
31              </tr>
32              <tr>
33                <td>部门名称:</td>
34                <td><input type="text" name="dept.dname"/></td>
35              </tr>
36              <tr>
37                <td>部门地址:</td>
38                <td><input type="text" name="dept.loc"/></td>
39              </tr>
40              <tr>
41                <td><input type="submit" value="提交"/></td>
42                <td><input type="reset" value="重置"/></td>
43              </tr>
44            </table>
45        </form>
46      </center>
47   </body>
48 </html>

insert.jsp

17、在WebRoot下创建update.jsp文件

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12
13     <title>My JSP ‘index.jsp‘ starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23
24   <body>
25      <center>
26        <fieldset>
27          <legend>修改操作</legend>
28          <form action="update.action" method="post">
29            <table border="1">
30              <tr>
31                <td>部门编号:</td>
32                <td><input type="text" name="dept.deptno" value="${dt.deptno}" readonly="readonly"/></td>
33              </tr>
34              <tr>
35                <td>部门名称:</td>
36                <td><input type="text" name="dept.dname" value="${dt.dname}"/></td>
37              </tr>
38              <tr>
39                <td>部门地址:</td>
40                <td><input type="text" name="dept.loc" value="${dt.loc}"/></td>
41              </tr>
42              <tr>
43                <td><input type="submit" value="提交"/></td>
44                <td><input type="reset" value="重置"/></td>
45              </tr>
46            </table>
47        </form>
48        </fieldset>
49
50      </center>
51   </body>
52 </html>

update.jsp

18、在WebRoot下创建fail.jsp文件

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12
13     <title>My JSP ‘index.jsp‘ starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23
24   <body>
25      操作失败
26   </body>
27 </html>

fail.jsp

19、运行项目如下

时间: 2024-10-16 18:37:39

5、Spring+Struts2+MyBatis+分页(无代理)增删改查的相关文章

MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多

一.用到的实体类如下: Student.java [html] view plaincopy package com.company.entity; import java.io.Serializable; import java.util.Date; public class Student implements Serializable{ private static final long serialVersionUID = 1L; private int id; private Stri

idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD

在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下. 原文在这里:https://my.oschina.net/finchxu/blog/3007984 工作环境: Windows 10 jdk8(1.8) IntelliJ IDEA spring 4 和 springMVC MySQL 5.7 maven 3.3 mybatis 3.4 DBCP Tomcat 8.5 项目上传到了Github方便查看:https://gith

Mybatis实现单表增删改查操作

mybatis是对持久层进行了封装.mybatis文档地址:https://mybatis.org/mybatis-3/zh/index.html 下面实现单表的增删改查操作. 1.新建maven项目命名为mybatis.并在pom.xml中引入相关依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"

MyBatis学习--简单的增删改查

jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: 1 Public static void main(String[] args) { 2 Connection connection = null; 3 PreparedStatement preparedStatement = null; 4 ResultSet resultSet = null; 5 6 try { 7 //加载数据库驱动 8 Class.forName

Mybatis实现部门表增删改查以及排序

废话不说,直接开门见山! 需要在WebContent下的lib下导入两个包 mybatis-3.2.5.jar ojdbc6.jar 1 package com.xdl.entity; 2 3 import java.io.Serializable; 4 5 public class Dept implements Serializable{ 6 private Integer deptno;//类型和名称与表保持一致 7 private String dname; 8 private Stri

spring boot(9) redis(连接,增删改查,集群,和session一起使用)

1.建立连接 1.1 pom.xml <!-- redis 相关支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.3.2.RELEASE</version> </dependency> 1.2 app

mybatis学习之普通增删改查

mybatis下载地址:https://github.com/mybatis/mybatis-3/releases 选择你要下载的版本,这里我下载的3.4.2版 项目结构: 将mybatis中的所有jar包导入还要导入链接mysql数据库的jar包. 其中db.properties,mybatis-config.xml,log4j.properties是在src下. 在mysql中新建mybatis数据库,在数据库中新建表t_customer,表结构如下: 随后向其中随便插入几条数据. Cust

Spring+Struts2+MyBatis+分页(mybatis无代理)增删改查

1.创建如下项目结构 2.在src下的com.entity包下创建Dept.java package com.entity; /** * 部门表 * @author CHIL * */ public class Dept { private Integer deptno; //部门编号 private String dname; //部门名称 private String loc; //位置 public Dept() { } public Dept(Integer deptno, String

Springmvc+Spring+Mybatis+Maven简单的增删改查

这是适合新手搭建的最简单的SSM整合框架 这是我的下载资源网址:http://download.csdn.net/my/uploads 首先整合框架的话,至少需要了解一下这几个框架 Spring介绍 它是为了解决企业应用开发的复杂性而创建的.Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情.然而, Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java 应用都可以从 Spring 中受益. 简单来说, Spring 是一个轻