栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间:发布时间: 百科书网 趣学号
spring+springmvc+mybatis整合加PageHelper实现crud及进行分页(maven) 先导入pom.xml的配置文件

这里的配置有点多,不需要的可以自行进行删除

    
    
        org.springframework
        spring-context
        5.2.5.RELEASE
    

    
    
        org.springframework
        spring-beans
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-aop
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-aspects
        5.2.5.RELEASE
    
    
        junit
        junit
        4.13
        test
    
    
    
        org.aspectj
        aspectjweaver
        1.9.7
        runtime
    
    
    
        aopalliance
        aopalliance
        1.0
    

    
    
        org.springframework
        spring-test
        5.2.5.RELEASE
        test
    
    
    
        aspectj
        aspectjrt
        1.5.4
    
    
    
        org.springframework
        spring-tx
        5.2.5.RELEASE
    
    
    
        com.mchange
        c3p0
        0.9.5.5
    
    
    
        org.springframework
        spring-jdbc
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-orm
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-core
        5.2.5.RELEASE
    
    
    
    
        org.springframework
        spring-webmvc
        5.2.5.RELEASE
    
    
        org.springframework
        spring-web
        5.2.5.RELEASE
    
    
    
        org.slf4j
        slf4j-api
        1.7.5
    

    
        org.slf4j
        slf4j-log4j12
        1.7.12
    
    
    
        org.apache.logging.log4j
        log4j-core
        2.17.1
    
    
    
        javax.servlet
        javax.servlet-api
        4.0.1
        provided
    
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
        javax.servlet
        jstl
        1.2
    
    
    
        taglibs
        standard
        1.1.2
    
    
    
        org.apache.taglibs
        taglibs-standard-impl
        1.2.5
        runtime
    
    
    
        javax.servlet.jsp.jstl
        jstl-api
        1.2
    
    
    
        org.glassfish.web
        jstl-impl
        1.2
    


    
        mysql
        mysql-connector-java
        5.1.37
    

    
    
        org.mybatis
        mybatis
        3.5.6
    

    
    
        org.mybatis
        mybatis-spring
        2.0.6
    
    
    
        org.mybatis.generator
        mybatis-generator-core
        1.3.7
    


    
    
        com.github.pagehelper
        pagehelper
        5.3.0
    


    
    
        com.github.jsqlparser
        jsqlparser
        4.2
    

    
    
        org.hibernate
        hibernate-validator
        5.1.3.Final
    
    
    
        javax.validation
        validation-api
        1.1.0.Final
    
    
        org.jboss.logging
        jboss-logging
        3.1.3.GA
    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.13.2.2
    
相关配置 web.xml的配置




    contextConfigLocation
    
    classpath:spring/applicationContext.xml



    org.springframework.web.context.ContextLoaderListener




    springDispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:applicationContext-mvc.xml
    
    1




    springDispatcherServlet
    /




    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        utf-8
    
    
        forceEncoding
        true
    


    CharacterEncodingFilter
    /*




    HiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter


    HiddenHttpMethodFilter
    /*

#### spring的applicationContentext.xml的配置


    
    





    
    
    
    
    
    



    
    
    
    
    
    
    
        
            
                
                    
                        
                        offsetAsPageNum=true
                        reasonable=true
                        rowBoundsWithCount=true
                        pageSizeZero=true
                    
                
            
        
    



    



    



    
    



    
    
        
        
        
    

springMVC的applicationContentext-mvc.xml的配置



    




    
    






这里加了一个mybatis的逆向工程、其他的数据源配置(请移步百度)

这里直接开始进行controller

@RequestMapping("/emps")
public String emps(@RequestParam(value = "id",defaultValue = "1")Integer pn,Model model){

    PageHelper.startPage(pn,6);
    List list = employeeService.getAll();

    PageInfo info = new PageInfo<>(list, 6);
    System.out.println("当前页码:"+info.getPageNum());
    System.out.println("总页码:"+info.getPages());
    System.out.println("总记录数:"+info.getTotal());
    System.out.println("当前页有几条记录:"+info.getSize());
    System.out.println("当前页的pageSize:"+info.getPageSize());
    System.out.println("前一页:"+info.getPrePage());
    System.out.println("结果:"+info.getList());//查询结果
    int[] nums = info.getNavigatepageNums();
    model.addAttribute("pageInfo",info);
    return "list";
}
@RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
public String delById(@PathVariable("id")Integer id){
    System.out.println(id+"==========>");
    employeeService.delById(id);
    return  "redirect:/emps";
}

@RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
public String upById(@PathVariable("id")Integer id,Model model){
    Employees employees = employeeService.putEmployeeById(id);
    model.addAttribute("employee",employees);
    List departmentList = departmentServeice.selAll();
    model.addAttribute("depts",departmentList);
    System.out.println("==========>");
    return "upEmployee";
}


@RequestMapping(value = "/emp/{id}",method = RequestMethod.POST)
public String upEmployee(@ModelAttribute("employee") Employees employees){
    System.out.println("要修改的员工:" + employees);
    employees.setdId(employees.getDepartment().getId());
    employeeService.upEmpById(employees);
    return "redirect:/emps";
}

@ModelAttribute
public void getDeAll(@RequestParam(value = "id", required = false) Integer id,
                     Model model){
    if (id != null) {
        Employees employee = employeeService.putEmployeeById(id);
        model.addAttribute("employee", employee);
    }
    // 1、先查出所有部门
    Collection departments = departmentServeice.selAll();
    // 2、放在请求域中
    model.addAttribute("depts", departments);
}
@RequestMapping(value = "/addEmployee")
public String addEmp(Model model){
    model.addAttribute("employee",new Employees());
    return "addEmp";
}
@RequestMapping(value = "/emp",method = RequestMethod.POST)
public String putEmp(@Valid Employees employees, BindingResult bindingResult, Model model){

    System.out.println("要添加的员工:" + employees);
    model.addAttribute("employee",employees);

    // 获取是否有校验错误
    boolean hasErrors = bindingResult.hasErrors();
    Map errorsMap = new HashMap();
    if (hasErrors) {
        List errors = bindingResult.getFieldErrors();
        System.out.println(errors.size()+"===>");
        Object[] objects = errors.toArray();
        System.out.println(objects.toString());
        for (FieldError fieldError : errors) {
            System.out.println("错误消息提示:" + fieldError.getDefaultMessage());
            System.out.println("错误的字段是?" + fieldError.getField());
            System.out.println(fieldError);
            System.out.println("------------------------");
            errorsMap.put(fieldError.getField(),
                    fieldError.getDefaultMessage());
        }
        model.addAttribute("errorInfo", errorsMap);
        return "addEmp";
    }else{
        employees.setdId(employees.getDepartment().getId());
        employeeService.addEmployee(employees);
        return "redirect:/emps";
    }
}

}

其他的dao与serveice根据自己的需求进行配置 相关的页面配置(我这里用的是jsp)

web页面

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/986812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号