博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring+Struts2+MyBatis+分页(mybatis无代理)增删改查
阅读量:6687 次
发布时间:2019-06-25

本文共 17510 字,大约阅读时间需要 58 分钟。

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 dname, String loc) {        this.deptno = deptno;        this.dname = dname;        this.loc = loc;    }    public Integer getDeptno() {        return deptno;    }    public void setDeptno(Integer deptno) {        this.deptno = deptno;    }    public String getDname() {        return dname;    }    public void setDname(String dname) {        this.dname = dname;    }    public String getLoc() {        return loc;    }    public void setLoc(String loc) {        this.loc = loc;    }    }Dept.java
Dept.java

 

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

package com.page;/** *  * @author CHIL * */public class Page {    private Integer pageno;      private Integer pagesize;    private Integer totalcount;    private Integer totalpage;    private Integer startrow;    private Integer endrow;            public Page() {    }    public Page(Integer pageno, Integer pagesize, Integer totalcount) {        this.pageno = pageno;        this.pagesize = pagesize;        this.startrow=pageno*pagesize;        this.endrow=(pageno-1)*pagesize;        this.totalcount = totalcount;        this.setTotalpage(totalcount);    }    public Integer getPageno() {        return pageno;    }    public void setPageno(Integer pageno) {        this.pageno = pageno;    }    public Integer getPagesize() {        return pagesize;    }    public void setPagesize(Integer pagesize) {        this.pagesize = pagesize;    }    public Integer getTotalcount() {        return totalcount;    }    public void setTotalcount(Integer totalcount) {        this.totalcount = totalcount;    }    public Integer getTotalpage() {        return totalpage;    }    /**     * 总条数     * @param totalcount     */    public void setTotalpage(Integer totalcount) {        this.totalpage = totalcount%pagesize==0? totalcount/pagesize:totalcount/pagesize+1;    }    public Integer getStartrow() {        return startrow;    }    public void setStartrow(Integer startrow) {        this.startrow = startrow;    }    public Integer getEndrow() {        return endrow;    }    public void setEndrow(Integer endrow) {        this.endrow = endrow;    }    }Page.java
Page.java

 

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

package com.mapper;import java.util.List;import com.entity.Dept;import com.page.Page;/** * 数据访问层接口 * @author CHIL * */public interface DeptMapper {    //查询所有    public List
findAll(); //分页查询 public List
findPage(Page page); //查询总记录数 public Integer findCount(); //根据id查询 public Dept findById(int id); //保存对象 public int saveDept(Dept dept); //修改对象 public int updateDept(Dept dept); //根据id删除 public int deleteById(int id);}DeptMapper.java
DeptMapper.java

 

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

insert into dept values(#{deptno},#{dname},#{loc})
update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
delete from dept where deptno=#{deptno}
DeptMapper.xml
DeptMapper.xml

 

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

mybatis-config.xml
mybatis-config.xml

 

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

log4j.rootLogger=DEBUG, Console    #Console  log4j.appender.Console=org.apache.log4j.ConsoleAppender  log4j.appender.Console.layout=org.apache.log4j.PatternLayout  log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n    log4j.logger.java.sql.ResultSet=INFO  log4j.logger.org.apache=INFO  log4j.logger.java.sql.Connection=DEBUG  log4j.logger.java.sql.Statement=DEBUG  log4j.logger.java.sql.PreparedStatement=DEBUG   log4j.properties
log4.properties

 

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

package com.mapper.impl;import java.util.List;import org.mybatis.spring.SqlSessionTemplate;import com.entity.Dept;import com.mapper.DeptMapper;import com.page.Page;/** * 数据访问层接口的实现类 * @author CHIL * */public class DeptMapperImpl implements DeptMapper {    //注入seqsession对象    private SqlSessionTemplate sqlSessionTemplate;        //必须存在getter和setter否则xml文件不能注入,会报没有session    public SqlSessionTemplate getSqlSessionTemplate() {        return sqlSessionTemplate;    }    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {        this.sqlSessionTemplate = sqlSessionTemplate;    }    /**     * 1.根据id删除      */    public int deleteById(int id) {        int num=sqlSessionTemplate.delete("com.mapper.DeptMapper.deleteById", id);        if(num>0){            System.out.println("删除成功");        }else{            System.out.println("删除失败");        }        return num;    }    /**     * 2.添加部门信息     */    public int saveDept(Dept dept) {        int num=sqlSessionTemplate.insert("com.mapper.DeptMapper.saveDept", dept);        if(num>0){            System.out.println("添加成功");        }else{            System.out.println("添加失败");        }        return num;    }    /**     * 3.修改     */    public int updateDept(Dept dept) {        int num=sqlSessionTemplate.update("com.mapper.DeptMapper.updateDept", dept);        if(num>0){            System.out.println("修改成功");        }else{            System.out.println("修改失败");        }        return num;    }        /**     * 4.查询所有     */    public List
findAll() { List
list=sqlSessionTemplate.selectList("com.mapper.DeptMapper.findAll"); if(list!=null){ System.out.println("查到所有"); }else{ System.out.println("没有查到所有"); } return list; } /** * 5.根据id没有查到 */ public Dept findById(int id) { Dept dept=sqlSessionTemplate.selectOne("com.mapper.DeptMapper.findById",id); if(dept!=null){ System.out.println("根据id查到"); }else{ System.out.println("根据id没有查到"); } return dept; } /** * 分页查询 */ public List
findPage(Page page) { List
list=sqlSessionTemplate.selectList("com.mapper.DeptMapper.findPage",page); if(list!=null){ System.out.println("分页查到了"); }else{ System.out.println("分页没有查到"); } return list; } /** * 查询总记录数 */ public Integer findCount() { Integer totalCount=sqlSessionTemplate.selectOne("com.mapper.DeptMapper.findCount"); return totalCount; } }DeptMapperImpl.java
DeptMapperImpl.java

 

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

package com.service;import java.util.List;import com.entity.Dept;import com.page.Page;/** * 业务逻辑层接口 * @author CHIL * */public interface DeptService {    //查询所有    public List
findAll(); //分页查询 public List
findPage(Page page); //查询总记录数 public Integer findCount(); //根据id查询 public Dept findById(int id); //保存对象 public int saveDept(Dept dept); //修改对象 public int updateDept(Dept dept); //根据id删除 public int deleteById(int id);}DeptService.java
DeptService.java

 

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

package com.service;import java.util.List;import com.entity.Dept;import com.page.Page;/** * 业务逻辑层接口 * @author CHIL * */public interface DeptService {    //查询所有    public List
findAll(); //分页查询 public List
findPage(Page page); //查询总记录数 public Integer findCount(); //根据id查询 public Dept findById(int id); //保存对象 public int saveDept(Dept dept); //修改对象 public int updateDept(Dept dept); //根据id删除 public int deleteById(int id);}DeptService.java
DeptSerivceImpl.java

 

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

package com.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.entity.Dept;import com.opensymphony.xwork2.ActionSupport;import com.page.Page;import com.service.DeptService;/** *  * @author CHIL * */public class DeptAction extends ActionSupport {         //注入业务逻辑层对象     private DeptService service;          //struts标签中动态代理赋值对象     private Dept dept;          //分页对象     private Page page;          /**      * 1.查询所有      * @return      */     public String findAll(){         List
list=service.findAll(); if(list!=null){ HttpServletRequest request=ServletActionContext.getRequest(); request.setAttribute("list", list); return SUCCESS; }else{ return ERROR; } } /** * 1.分页查询所有 * @return */ public String findPage(){ //当前页 Integer pageNo=1; if(page!=null){ System.out.println("page不为空"); pageNo=page.getPageno(); }else{ System.out.println("page为空"); } //页面大小 Integer pageSize=3; //查询总条数 Integer totalCount= service.findCount(); page=new Page(pageNo, pageSize, totalCount); List
list=service.findPage(page); if(list!=null){ HttpServletRequest request=ServletActionContext.getRequest(); request.setAttribute("list", list); request.setAttribute("page", page); return SUCCESS; }else{ return ERROR; } } /** * 根据id查询 * @return */ public String findById(){ if(dept!=null){ Dept dt=service.findById(dept.getDeptno()); if(dt!=null){ HttpServletRequest request=ServletActionContext.getRequest(); request.setAttribute("dt", dt); return SUCCESS; } } return ERROR; } /** * 添加 * @return */ public String save(){ if(dept!=null){ int num=service.saveDept(dept); if(num>0){ return SUCCESS; }else{ return ERROR; } } return ERROR; } /** * 修改 * @return */ public String update(){ if(dept!=null){ int num=service.updateDept(dept); if(num>0){ return SUCCESS; }else{ return ERROR; } } return ERROR; } /** * 删除 * @return */ public String delete(){ if(dept!=null){ int num=service.deleteById(dept.getDeptno()); if(num>0){ return SUCCESS; }else{ return ERROR; } } return ERROR; } public DeptService getService() { return service; } public void setService(DeptService service) { this.service = service; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } }DeptAction.java
DeptAction.java

 

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

classpath:mybatis-config.xml
applicationContext.xml
applocationContext.xml

 

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

index.jsp
fail.jsp
index.jsp
fail.jsp
update.jsp
findAll
findAll
insert.jsp
findAll
fail.jsp
findAll
fail.jsp
struts.xml
struts.xml

 

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

contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
web.xml
web.xml

 

15、在WebRoot下创建index.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
添加
部门编号 部门名称 部门地址 操作
${i.deptno} ${i.dname} ${i.loc} 删除  |  修改
第${page.pageno}/${page.totalpage }页
首页 
上一页 
上一页 
下一页 
上一页 
末页  总${page.totalcount }条
index.jsp
index.jsp

 

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

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
部门编号:
部门名称:
部门地址:
insert.jsp
insert.jsp

 

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

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
修改操作
部门编号:
部门名称:
部门地址:
update.jsp
update.jsp

 

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

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
操作失败 fail.jsp
fail.jsp

 

19、运行项目如下

转载于:https://www.cnblogs.com/ChineseIntelligentLanguage/p/6431490.html

你可能感兴趣的文章
win7控制面板中网络适配器不可见
查看>>
iPhone X的UI设计技巧
查看>>
编辑器
查看>>
马哥笔记第十六天故障排除、trap、sed、awk、bash数组、bash字符串操作
查看>>
在ubuntu系统中配置《汇编语言的编程艺术》开发环境
查看>>
关闭windows的默认共享
查看>>
react开发环境搭建
查看>>
数据库读写分离
查看>>
atoi() 与 itoa()函数的用法
查看>>
stm32h7 __attribute__((weak)) 使用说明
查看>>
关于异常
查看>>
spark-submit性能调优
查看>>
三年的职业生涯
查看>>
Linux身份验证策略
查看>>
社交是微信营销
查看>>
2008 R2 证书服务器应用详解
查看>>
logger使用
查看>>
Python 学习笔记 - socket(粘包及其处理方式)
查看>>
Mac平台下数据乱码原因
查看>>
我的友情链接
查看>>