Spring+SpringMVC+jdbc

第一步:JdbcUtil 类

package com.zll.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @auther zll
 * @create 2020/7/21-17:25
 */
public class JdbcUtil {


    public static Connection getConnection() throws Exception {
        //1.加载配置文件
        InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        //2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //3.加载驱动
        Class.forName(driverClass);

        //4.获取连接
        Connection connection = DriverManager.getConnection(url,user,password);

        return connection;
    }

    public static void closeConnection(Connection connection, ResultSet resultSet, PreparedStatement ps){
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if ((resultSet != null)) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if ((ps != null)) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 第二步:dao层:UserDao接口和实现类UserDaoImpl

public interface UserDao {

    public User getAll(String userCode,String userPassword);
}
package com.zll.dao.impl;

import com.zll.dao.UserDao;
import com.zll.pojo.User;
import com.zll.util.JdbcUtil;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @auther zll
 * @create 2020/7/21-17:23
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public User getAll(String userCode, String userPassword) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JdbcUtil.getConnection();
            String sql = "select * from smbms_user where userCode=? and userPassword=? ";
            ps = connection.prepareStatement(sql);
            ps.setString(1,userCode);
            ps.setString(2,userPassword);
            System.out.println(sql);

            resultSet = ps.executeQuery();

            while (resultSet.next()){
                user = new User();
                user.setUserName(resultSet.getString("userName"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }
}

第三步:service层:UserService接口  和 实现类UserServiceImpl

public interface UserService {

    public User getAllUser(String userCode, String userPassword);
}
package com.zll.service.impl;

import com.zll.dao.UserDao;
import com.zll.pojo.User;
import com.zll.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @auther zll
 * @create 2020/7/21-17:24
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User getAllUser(String userCode, String userPassword) {

        return userDao.getAll(userCode,userPassword);

    }
}

 第四步:controller层:UserController

package com.zll.controller;

import com.zll.pojo.User;
import com.zll.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @auther zll
 * @create 2020/7/21-17:45
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
//,method = RequestMethod.POST
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String doLogin(String userCode, String userPassword, HttpServletRequest request, HttpSession session){
        //调用业务层
        User user = userService.getAllUser(userCode, userPassword);

        if(user==null){
              request.setAttribute("errors","账号密码不正确");
              return "login";
        }else{
            return "main";
        }

    }
}

 第五步:web.xml




    
    

        contextConfigLocation

        classpath:applicationContext.xml

    

    
    

        org.springframework.web.context.ContextLoaderListener

    

    
    

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

        
        1
    

    
    
    
        springMVC
        /
    
    
        login.jsp
    

第六步:springmvc-servlet.xml




    

    


    

        

        

    


第七步:spring的配置文件




    
    

 第八步:login.jsp   和    main.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    login



账号:
密码:
${errors}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    main


欢迎来到主界面

 

 

 

你可能感兴趣的:(作业)