ssj框架整合

SpringMVC +Spring+ SpringJDBC整合实例:

文件结构:
ssj框架整合_第1张图片

相关的jar包

ssj框架整合_第2张图片

1、web.xml的配置:



    contextConfigLocation
    classpath:applicationContext-*.xml




    springmvc1
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:springmvc-servlet.xml
    
    1


    springmvc1
    /


    org.springframework.web.context.ContextLoaderListener

2、applicationContext.xml的配置:


使spring扫描包下的所有类,让标注spring注解的类生效

-->


3、c3p0.properties的配置:
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql:///spring-mvc-jdbc
c3p0.user=root
c3p0.password=1234

4、springmvc-servlet.xml的配置




mvc:annotation-driven/




	
	

5、User类
package com.offcn.entity;

import java.io.Serializable;

public class User implements Serializable {
private int uid;
private String uname;
private String upwd;
private int usex;

public int getUid() {
    return uid;
}

public void setUid(int uid) {
    this.uid = uid;
}

public String getUname() {
    return uname;
}

public void setUname(String uname) {
    this.uname = uname;
}

public String getUpwd() {
    return upwd;
}

public void setUpwd(String upwd) {
    this.upwd = upwd;
}

public int getUsex() {
    return usex;
}

public void setUsex(int usex) {
    this.usex = usex;
}

}
6、UserController类
package com.offcn.controller;

import com.offcn.entity.User;
import com.offcn.service.UserService;
import com.offcn.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(“userController”)
public class UserController {

@Resource
private UserService userService;


/*----提交方式--*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String loginIn(@RequestParam(value = "uname",required = false)String uname,
                      @RequestParam(value = "upwd",required = false)String upwd, Model model){

    System.out.println();
    User user = userService.selectByName(uname, upwd);

    if(user!=null){
        model.addAttribute("user",user);
        /*---这个代表重定向---*/
        //默认转发:forward
        return "redirect:toPage";
    }

    return "error";
}

@RequestMapping("toPage")
public String toPage(@RequestParam(value = "currentPageNo",required = false) String currentPageNo
                    ,@RequestParam(value ="pageSize" ,required = false) String pageSize, Model model){

    if(currentPageNo==""||currentPageNo==null){

        currentPageNo="1";
    }

    if(pageSize==null||pageSize==""){
        pageSize="5";
    }

    PageUtils pageUtils=new PageUtils();
    //给pageUtils所有属性赋值
    List list = userService.selectAllList(Integer.parseInt(currentPageNo), Integer.parseInt(pageSize));
    //查询总记录数
    Integer countPage = userService.selectUserCount();
    //总页数
   Integer totalPageSize=countPage%Integer.parseInt(pageSize)==0?
           countPage/Integer.parseInt(pageSize):countPage/Integer.parseInt(pageSize)+1;

    //赋值
    pageUtils.setTotalCount(countPage);
    pageUtils.setTotalPageSize(totalPageSize);
    pageUtils.setCurrentPageNo(Integer.parseInt(currentPageNo));
    pageUtils.setUserList(list);
    pageUtils.setPageSize(Integer.parseInt(pageSize));

    model.addAttribute("pageUtils", pageUtils);


    return "showInfo";
}

}
7、UserService接口
package com.offcn.service;

import com.offcn.entity.User;

import java.util.List;

public interface UserService {

User selectByName(String userName, String upwd);


//获取数据-第一个参数当前页,第二个参数是单页的总量
List selectAllList(Integer currentPageNo, Integer pageSize);

//查出总记录数
Integer selectUserCount();

}
8、UserServiceImpl实现类:
package com.offcn.service.impl;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import com.offcn.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/—注解扫描Service层并取别名—/
@Service(“userService”)
public class UserServiceImpl implements UserService {

@Resource/*注入userDao*/
private UserDao userDao;
@Override

public User selectByName(String uName, String upwd) {

    User user=userDao.selectByName(uName);


        if (user.getUpwd().equals(upwd)) {
            return user;
        }

    return null;
}

@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {
    return userDao.selectAllList(currentPageNo,pageSize);
}


@Override
public Integer selectUserCount() {
    return userDao.selectUserCount();
}

}

**9、UserDao接口:**与UserService一样的方法

public interface UserDao {

User selectByName(String userName);

//获取数据-第一个参数当前页,第二个参数是单页的总量
List selectAllList(Integer currentPageNo,Integer pageSize);

//查出总记录数
Integer selectUserCount();

}
10、UserDaoImpl实现类:
执行sql语句的操作:
package com.offcn.dao.impl;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import com.offcn.handle.ArrayResultHandler;
import com.offcn.handle.BeanListResultHandler;
import com.offcn.handle.BeanResultHandler;
import com.offcn.utils.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(“userDao”)
public class UserDaoImpl implements UserDao {

//登陆验证
@Override
public User selectByName(String userName) {

    String sql = "select * from user where uname=?";

    return (User) BaseDao.executeQuery(sql,new Object[]{userName}, new BeanResultHandler(User.class));
}



//查询分页
@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {

    String sql="select * from user limit ?,?";
    Object []objects={(currentPageNo-1)*pageSize,pageSize};

    return (List) BaseDao.executeQuery(sql,objects, new BeanListResultHandler(User.class));
}

@Override
//查询总记录数
public Integer selectUserCount() {
    String sql="select count(1) from user";


    List list =(List) BaseDao.executeQuery(sql, null, new ArrayResultHandler());
  String count= list.get(0)+"";

    return Integer.parseInt(count);
}

}
11、实现分页功能的工具类
package com.offcn.utils;

import com.offcn.entity.User;
import java.util.List;

//分页的工具类:
public class PageUtils {
//当前页码
private Integer currentPageNo;
//每页显示多少条
private Integer pageSize;
//总记录数
private Integer totalCount;
//总页数
private Integer totalPageSize;
//查询出的集合
private List userList;

public Integer getCurrentPageNo() {
    return currentPageNo;
}

public void setCurrentPageNo(Integer currentPageNo) {
    this.currentPageNo = currentPageNo;
}

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 getTotalPageSize() {
    return totalPageSize;
}

public void setTotalPageSize(Integer totalPageSize) {
    this.totalPageSize = totalPageSize;
}

public List getUserList() {
    return userList;
}

public void setUserList(List userList) {
    this.userList = userList;
}

}
12、最后创建一个取值的jsp文件,相关配置如下:

<%–
Created by IntelliJ IDEA.
User: ibm
Date: 2018/10/17
Time: 19:32
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

Title

    
        ${user.uid}
        ${user.uname}
        ${user.upwd}
    





    
        首页
        上一页

    


    首页
    上一页



[${pageUtils.currentPageNo}/${pageUtils.totalPageSize}]




    下一页

    末页



    下一页
    末页


编号 姓名 密码
**测试结果如下:**

![在这里插入图片描述](https://img-blog.csdn.net/20181017230657519?watermark/2/textssj框架整合_第3张图片

你可能感兴趣的:(ssj框架整合)