第一步:建立用户界面
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>login</title> </head> <body> <form action="login.do" method="post"> <!-- userName:<input type="text" name="userName"/><br/> passWord:<input type="password" name="userPwd"/><br/> --> <input type="submit" value="查询所有信息"/> </form> </body> </html>
这里也可以配合后面做成用户登录界面。
第二步:配置Web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mydb2</display-name> <welcome-file-list> <welcome-file>welcome.html</welcome-file> </welcome-file-list> <!-- 这里是一个总控制器 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 解决POST提交乱码问题 --> <filter> <filter-name>EncodingName</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingName</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
第三步:配置dispatcher文件
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 获取配置文件 --> <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db-config.properties</value> </list> </property> </bean> <!-- 获取数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${db.dirverClass}</value> </property> <property name="url"> <value>${db.url}</value> </property> <property name="username"> <value>${db.username}</value> </property> <property name="password"> <value>${db.password}</value> </property> </bean> <!-- 给jdbc模板注入数据源 在JdbcTemplate里有一个对应的私有属性dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 给userDao注入模板 在com.spring.db.UserDao里应该设有一个JdbcTemplate jdbcTemplate的私有属性,并且setter --> <bean id="userDao" class="com.spring.db.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 给控制器注入dao类 在com.spring.controller.LoginController里应该设有一个UserDao userDao的私有属性,并且setter --> <bean id="LoginController" class="com.spring.controller.LoginController"> <property name="userDao" ref="userDao"></property> </bean> <!-- 配置映射响应器,映射响应器会根据请求文件来决定进入到哪个控制器,--> <bean id="mapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!-- 这里的key就是你的action请求。后面的是要分派的控制器,控制器和是id对应的。--> <prop key="login.do">LoginController</prop> </props> </property> </bean> <!--定义视图 通过internalResourceView来表示 使用的是Servlet/jsp技术--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <!--jsp存放的目录--> <property name="prefix"> <value>/view/</value> </property> <!--jsp文件的后缀--> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
这儿控制MySQL的大致过程为,首先获得数据源,将数据源添加到JDBCTemplate模板,然后再将Jdbc模板添加到UserDao(负责和数据库交换)中,最后将UserDao添加到控制器中。
第四步:建立数据源文件
db-config.properties
db.url=jdbc:mysql:///test?characterEncoding=utf8&useSSL=true&serverTimezone=GMT db.username=root db.password=[email protected] db.dirverClass=com.mysql.cj.jdbc.Driver
第五步:建立Model
UserVO.java
package com.spring.model; public class UserVO { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
第六步:建立dao类(和数据库交换),代码为:
package com.spring.db; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import com.spring.model.UserVO; public class UserDao { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @SuppressWarnings({ "unchecked", "rawtypes" }) public List<UserVO> find(){ String sql="select * from userbo";//这里也可以添加查询限制条件 return jdbcTemplate.query(sql, new RowMapper(){ public Object mapRow(ResultSet rs, int num) throws SQLException { UserVO uservo=new UserVO(); uservo.setId(rs.getInt("USERID")); uservo.setName(rs.getString("USERNAME")); uservo.setPwd(rs.getString("USERPSW")); return uservo; } }); } }
第七步:建立控制器:
LoginController.java
package com.spring.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.spring.db.UserDao; import com.spring.model.UserVO; public class LoginController implements Controller { private UserDao userDao; @SuppressWarnings("unchecked") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // String name = request.getParameter("userName"); // String psw = request.getParameter("userPwd"); @SuppressWarnings("rawtypes") Map model=new HashMap(); List<UserVO> userVO=userDao.find(); model.put("uservo", userVO); return new ModelAndView("userInfo",model); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public UserDao getUserDao() { return userDao; } }
第八步:视图界面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.spring.model.UserVO"%> <% @SuppressWarnings("unchecked") /* *这儿是合法的,根据com.spring.db.controller.LoginController中handleRequest的返回数据类型进行设置的。 */ List<UserVO> str= (List<UserVO>)request.getAttribute("uservo"); %> <html> <head> <title>result</title> </head> <body> <table border="1"> <tr> <td >编号</td> <td >姓名</td> <td >年龄</td> </tr> <% for(UserVO user:str){ %> <tr> <td><%=user.getId() %></td> <td><%=user.getName() %></td> <td><%=user.getPwd() %></td> </tr> <% } %> </table> </body> </html>
数据库信息
mysql> select * From userbo; +-----------+---------------+--------------+ | USERID | USERNAME | USERPSW | +-----------+---------------+--------------+ | 1 | 李晓红 | 123456789 | | 2 | 刘梦雨 | 1234 | | 3 | 刘姐 | 134567 | +-----------+---------------+--------------+
参考原文:
java spring mvc+mysql 整合写的一个简单的登录
时间: 2024-10-11 21:56:22