ssm框架整合+pageHelper--实现了简单的增删改查与分页

转载请标明出处:
https://blog.csdn.net/weixin_41965979/article/details/80926877
本文出自付付讶的博客

首先附上maven项目架构图(代码中标红的全部是要换成自己项目里相对应的名字)

ssm框架整合+pageHelper--实现了简单的增删改查与分页_第1张图片

1:在pom里写上ssm整合需要的jar包


  UTF-8
  1.8
  1.8
  4.3.14.RELEASE
  3.4.1


  
  
    org.springframework
    spring-context
    ${spring.version}
  
  
    org.springframework
    spring-core
    ${spring.version}
  
  
    org.springframework
    spring-beans
    ${spring.version}
  
  
    org.springframework
    spring-web
    ${spring.version}
  

  
    org.springframework
    spring-webmvc
    ${spring.version}
  

  
    org.springframework
    spring-aop
    ${spring.version}
  

  
    org.springframework
    spring-jdbc
    ${spring.version}
  

  
    org.springframework
    spring-tx
    ${spring.version}
  

  
    org.springframework
    spring-expression
    ${spring.version}
  

  
    org.aspectj
    aspectjweaver
    1.8.6
  


  
  
    org.mybatis
    mybatis
    ${mybatis.version}
  

  
    org.mybatis
    mybatis-spring
    1.3.1
  

  
    com.alibaba
    druid
    1.1.5
  
  
  
    com.alibaba
    fastjson
    1.1.41
  

  
    mysql
    mysql-connector-java
    6.0.6
  

  
    org.slf4j
    slf4j-api
    1.7.7
  

  
    org.slf4j
    slf4j-log4j12
    1.7.7
  
  
  
    commons-dbcp
    commons-dbcp
    1.2.2
  

  
    commons-fileupload
    commons-fileupload
    1.3.1
  

  
    commons-io
    commons-io
    2.4
  

  
    commons-codec
    commons-codec
    1.9
  

  
    org.apache.commons
    commons-lang3
    3.3.2
  

  
    org.apache.poi
    poi
    3.14
  

  
  
  
    com.github.pagehelper
    pagehelper
    5.1.1
  

  
  
    org.codehaus.jackson
    jackson-mapper-asl
    1.9.13
  

  
  
    javax
    javaee-api
    7.0
  

  
    jstl
    jstl
    1.2
  

  
    javax.servlet
    javax.servlet-api
    3.1.0
    provided
  

  
    javax.servlet
    jstl
    1.2
  

  
    log4j
    log4j
    1.2.17
  

  
    junit
    junit
    4.11
    test
  

 

 

2:配置web.xml

 

 





  
  
    contextConfigLocation
    
      classpath*:spring/applicationContext.xml
    

  
  
    log4jConfigLocation
    classpath:log4j.properties
  

  
  
    org.springframework.web.context.request.RequestContextListener
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    org.springframework.web.util.IntrospectorCleanupListener
  
  
  
    org.springframework.web.util.Log4jConfigListener
  

  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath*:spring/spring-mvc.xml
    
    1
  
  
    dispatcherServlet
    /
  

  
  
    DruidWebStatFilter
    com.alibaba.druid.support.http.WebStatFilter
    
      exclusions
      *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
    
  
  
    DruidWebStatFilter
    /*
  


  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    encodingFilter
    /*
  

  
  
    /index.jsp
  


3:配置spring-mvc.xml

 



    
    







    
    
    
    

4:配置与mybatis整合,名字自取我这里叫(applicationContext.xml)

      注意:数据库连接池的value一定要与jdbc.properties一样

 



    







    
    
    
    
    




    
    
    
   





    
    
    
    




    
    

5:数据库连接配置:jdbc.properties(park为数据库名称)

 

#mysql
db.Type=mysql
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/park?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
db.username=fu
db.password=123456

6:日志配置:log4j.properties

 

log4j.rootLogger=DEBUG,console,file

#-----------------------------------#
#1 定义日志输出目的地为控制台
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
####可以灵活地指定日志输出格式,下面一行是指定具体的格式 ###
#%c: 输出日志信息所属的类目,通常就是所在类的全名
#%m: 输出代码中指定的消息,产生的日志具体信息
#%n: 输出一个回车换行符,Windows平台为"/r/n",Unix平台为"/n"输出日志信息换行
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#-----------------------------------#
#2 文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.file = org.apache.log4j.RollingFileAppender
#日志文件输出目录
log4j.appender.file.File=log/tibet.log
#定义文件最大大小
log4j.appender.file.MaxFileSize=10mb
###输出日志信息###
#最低级别
log4j.appender.file.Threshold=ERROR
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#4 mybatis 显示SQL语句部分
log4j.logger.org.mybatis=DEBUG
#log4j.logger.cn.tibet.cas.dao=DEBUG
#log4j.logger.org.mybatis.common.jdbc.SimpleDataSource=DEBUG#
#log4j.logger.org.mybatis.common.jdbc.ScriptRunner=DEBUG#
#log4j.logger.org.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG#
#log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

7:配置mybatis的映射资源文件mybatis-config.xml

 




    
    
        
    


    
    
        
        
    

8:配置结束了,接下来看方法和实体

userEntity

 
public class UserEntity {
    
    private int id;
    private String userName;   
    private String age;    
      
    @Override  
    public String toString() {  
        return "User [id=" + id + ", age=" + age + ", userName=" + userName  
                + "]";  
    }  
    public UserEntity(){
        super();    
    }    
    public int getId() {
        return id;    
    }    
    public void setId(int id) {
        this.id = id;    
    }    
    public String getAge() {
        return age;    
    }    
    public void setAge(String age) {    
        this.age = age;    
    }    
    public String getUserName() {    
        return userName;    
    }    
    public void setUserName(String userName) {    
        this.userName = userName;    
    }    
    public UserEntity(int id, String age, String userName) {
        super();    
        this.id = id;    
        this.age = age;    
        this.userName = userName;    
    }    
 

UserService

 

@Service
public class UserService{
    @Autowired
    private UserDao userDao;
    public void save(UserEntity user){
       userDao.insert(user);
    }
    public boolean edit(UserEntity user){
       return userDao.edit(user);
    }
    public boolean deleteUser(int id){
        return userDao.deleteUser(id);
    }
    public UserEntity findById(int id){
        return  userDao.findById(id);
    }
    public List findAll(){
        return userDao.findAll();
    }
}  

userDao

@Repository
public interface UserDao{
    int insert(UserEntity user);
    boolean edit(UserEntity user);
    boolean deleteUser(int id);
    UserEntity findById(int id);
    List findAll();
}    
 

userDao.xml类(如果dao.xml与dao在同一层包目录下则applicationContext.xml里不用配置自动扫描mapping.xml文件)

!--
    namespace:必须与对应的接口全类名一致  UserMapper.java
    id :必须与对应接口的某个对应的方法名一致即必须要和UserMapper.java接口中的方法同名。
 -->



    insert into t_user(userName,age) values(#{userName},#{age})



    update t_user set userName=#{userName},age=#{age} where id=#{id}



    delete from t_user where id=#{id}







@Controller
@RequestMapping("/user")
public class UserController {  
    @Autowired
    private UserService userService;
    /**  
     * 获取所有用户列表  
     * @param request  
     * @return  
     */
    @RequestMapping("/getAllUser")  
    public String getAllUser(@RequestParam(required = false, defaultValue = "1") Integer page,
                             HttpServletRequest request, Model model){
        PageHelper.startPage(page,2);
        List user = userService.findAll();
        PageInfo p = new PageInfo(user);
        model.addAttribute("page",p);
        model.addAttribute("userList", user);
        request.setAttribute("userList", user);
        return "test/allUser";
    }  
    /**  
     * 跳转到添加用户界面  
     * @param
     * @return  
     */  
    @RequestMapping("/toAddUser")  
    public String toAddUser(){  
        return "test/addUser";
    }  
    /**  
     * 添加用户并重定向  
     * @param user  
     * @param
     * @return  
     */  
    @RequestMapping("/addUser")  
    public String addUser(UserEntity user){
        userService.save(user);
        return "redirect:/user/getAllUser";
    }  
    /**  
     *编辑用户  
     * @param user  
     * @param request  
     * @return  
     */  
    @RequestMapping("/updateUser")  
    public String updateUser(UserEntity user, HttpServletRequest request, Model model){
        if(userService.edit(user)){
            user = userService.findById(user.getId());  
            request.setAttribute("user", user);  
            model.addAttribute("user", user);  
            return "redirect:/user/getAllUser";  
        }else{  
            return "/error";  
        }
    }  
    /**  
     * 根据id查询单个用户  
     * @param id  
     * @param request  
     * @return  
     */  
    @RequestMapping("/getUser")  
    public String getUser(int id,HttpServletRequest request,Model model){
        request.setAttribute("user", userService.findById(id));  
        model.addAttribute("user", userService.findById(id));  
        return "test/editUser";
    }  
    /**  
     * 删除用户  
     * @param id  
     * @param
     * @param
     */  
    @RequestMapping("/delUser")
    public String delUser(int id){
        userService.deleteUser(id);
        return "redirect:/user/getAllUser";
    }
}  

9:方法结束了,接下来看页面(index.jsp)

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>




    
    首页


进入用户管理页

allUser.jsp

 

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



    
    
    用户列表

    



添加用户
姓名 年龄 操作
${user.userName } ${user.age } 编辑 删除

每页${page.pageSize}条 当前页${page.size}条 ${page.pageNum}/${page.pages}页 总条数${page.total}

首页 首页 上一页 上一页 下一页 下一页 末页 末页

addUser.jsp

 

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



    

    添加用户

    




添加用户

姓名: 年龄:

editUser.jsp

 

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



    

    编辑用户

    




添加用户

姓名: 年龄:

接下来效果图

ssm框架整合+pageHelper--实现了简单的增删改查与分页_第2张图片

 

 

 

项目就到这结束了,有问题可以留言,项目源码点击打开链接

你可能感兴趣的:(ssm)