SpringMVC+Spring+JDBC

一.项目需求
1.使用Spring+SpringMVC+JDBC实现用户的登录和注销操作,并添加全局的异常处理。
二.解决思路
2.1.创建工程,添加项目的依赖。
2.2.创建用户实体类。
2.3.创建数据访问层接口以及实现类。‘
2.4.创建业务逻辑层的接口以及实现类。
2.5.创建控制器类,用来接收请求和处理请求。
2.6.配置Spring,SpringMVC以及Web.xml核心配置文件,并在Web.xml文件中加载Spring和SpringMVC的核心配置文件。
项目依赖下载地址:

链接:https://pan.baidu.com/s/1LTQG2jzIi4XGuPV2doiuxg
提取码:cg0n


三.实现步骤
3.1.创建工程,添加依赖
SpringMVC+Spring+JDBC
文章图片

3.2创建用户实体类
代码如下:
package com.atguigu.springmvc.pojo; /** * @author sd * @date 2020-07-21 20:14 */ public class User { private Integer id; private String namess; private String passwords; public Integer getId() { return id; }public void setId(Integer id) { this.id = id; }public String getNamess() { return namess; }public void setNamess(String namess) { this.namess = namess; }public String getPasswords() { return passwords; }public void setPasswords(String passwords) { this.passwords = passwords; } }

3.3创建数据访问层接口,和实现类
自定义工具类定义获取数据库连接以及关闭有关对象的方法
package com.atguigu.springmvc.utils; import java.sql.*; /** * @author sd * @date 2020-07-21 20:05 */ public class Conn { private static Connection connection; static { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis_01", "root", "root"); } catch (Exception e) { e.printStackTrace(); } } /* * 获取数据库的连接对象 * */ public static Connection getConnection(){ return connection; } /* * 关闭数据库资源 * */ public static void closeConnection(Connection conn, ResultSet resultSet, Statement statement){ if (conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } }}

数据访问层的接口:
package com.atguigu.springmvc.dao; import com.atguigu.springmvc.pojo.User; /** * @author sd * @date 2020-07-21 20:19 */ public interface UserDao { //根据用户的名称和密码来进行登录 public User queryByUserNameAndPassword(String username,String password); }

数据访问层的实现类:
package com.atguigu.springmvc.dao.Impl; import com.atguigu.springmvc.dao.UserDao; import com.atguigu.springmvc.pojo.User; import com.atguigu.springmvc.utils.Conn; import org.springframework.stereotype.Repository; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; /** * @author sd * @date 2020-07-21 20:21 */ @Repository public class UserDaoImpl implements UserDao { private Connection connection; private ResultSet resultSet; private Statement statement; @Override public User queryByUserNameAndPassword(String username, String password) { //创建实体对象 User user=null; //获取数据库连接 connection= Conn.getConnection(); //编写Sql语句 String sql="select * from user where namess='"+username +"'and passwords='"+password+"'"; try { statement=connection.createStatement(); resultSet=statement.executeQuery(sql); if (resultSet != null){ while (resultSet.next()){ user=new User(); user.setNamess((String) resultSet.getObject("namess")); user.setPasswords((String)resultSet.getObject("passwords")); } } } catch (Exception e) { e.printStackTrace(); } //关闭数据库连接 //Conn.closeConnection(connection,resultSet,statement); return user; } }

数据库访问对象类的测试:
package com.atguigu.springmvc.test; import com.atguigu.springmvc.utils.Conn; import org.junit.Test; import java.sql.Connection; /** * @author sd * @date 2020-07-21 20:12 */ public class TestConnection { @Test public void test1(){ Connection connection = Conn.getConnection(); System.out.println(connection); Conn.closeConnection(connection,null,null); } }

数据访问层实现类的测试:
package com.atguigu.springmvc.test; import com.atguigu.springmvc.dao.Impl.UserDaoImpl; import com.atguigu.springmvc.dao.UserDao; import com.atguigu.springmvc.pojo.User; import com.atguigu.springmvc.utils.Conn; import org.junit.Test; /** * @author sd * @date 2020-07-21 20:30 */ public class TestUserDaoImpl { @Test public void test1(){ UserDao userDao=new UserDaoImpl(); User user = userDao.queryByUserNameAndPassword("admin", "ly@168"); System.out.println(user.getNamess()+user.getPasswords()); //关系数据库连接} }

3.4业务逻辑层的接口以及实现类
接口:
package com.atguigu.springmvc.service; import com.atguigu.springmvc.pojo.User; /** * @author sd * @date 2020-07-21 20:43 */ public interface UserService { ///根据用户的名称和密码来进行登录 public User queryByUserNameAndPassword(String username, String password); }

业务逻辑层的实现类:
package com.atguigu.springmvc.service.Impl; import com.atguigu.springmvc.dao.UserDao; import com.atguigu.springmvc.pojo.User; import com.atguigu.springmvc.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author sd * @date 2020-07-21 20:43 */ @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 queryByUserNameAndPassword(String username, String password) { return userDao.queryByUserNameAndPassword(username,password); } }

3.5.控制器类的实现
SpringMVC+Spring+JDBC
文章图片

代码实现:
package com.atguigu.springmvc.controller; import com.atguigu.springmvc.pojo.User; import com.atguigu.springmvc.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * @author sd * @date 2020-07-21 20:15 */ @Controller @RequestMapping("/user") public class UserController {@Resource UserService userService=null; @RequestMapping("/login") public String login(String username, String password, HttpServletRequest request){ User user = userService.queryByUserNameAndPassword(username, password); if (user == null){ request.setAttribute("errors","用户名或者密码错误!!!"); return "logins"; }else { HttpSession session = request.getSession(); session.setAttribute("currentUser",user); return "success"; } }/* * 用户的注销操作 * */ @RequestMapping("/clear") public String clear(HttpServletRequest request){ request.getSession().invalidate(); return "logins"; } }

3.6.配置文件的配置
Spring配置文件的配置:

SpringMVC的配置:
error

Web.xml的配置:
contextConfigLocationclasspath:applictionContext.xml org.springframework.web.context.ContextLoaderListener springmvc org.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springMVCContext.xml springmvc /default *.jpg

【SpringMVC+Spring+JDBC】

    推荐阅读