spring mvc + mybatis + mysql 整合的一個簡單的登入例子
阿新 • • 發佈:2019-02-03
今天用spring跟mybatis整合寫了一個簡單的登入例子,第一次整合,給自己做個筆記,可能註釋寫的有點少,做的不足的地方謝謝指出,也分享給需要的朋友,下面給出登入失敗和成功的效果圖:
這個登入例子用的工具是myeclipse8.6+mysql5.1,使用到的技術有spring3.0+mybatis3.2.3+mybatis-spring-1.1.1(這個是spring跟mybatis整合的包),專案的整體結構如圖:
專案新建完之後我們就把專案分次序把專案新建起來:
1:在web.xml檔案配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2: 新建vo類,程式碼如下:
package com.li.vo; public class UserVO { private int id; private String name; private String pwd; public UserVO(){} public void setPwd(String pwd) { this.pwd = pwd; } public String getPwd() { return pwd; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(int id) { this.id = id; } public int getId() { return id; } }
3: 新建UserDaoIMP介面:
package com.li.IMP;
import com.li.vo.UserVO;
public interface UserDaoIMP {
public UserVO selectUser(UserVO uservo);
public int insertUser(UserVO uservo);
public int updaqteUser(UserVO uservo);
public int deleteUserById(int user_id);
}
4: 新建UserDaoIMP.xml配置檔案:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.li.IMP.UserDaoIMP"> <select id="selectUser" parameterType="com.li.vo.UserVO" resultType="com.li.vo.UserVO"> select * from user where name=#{name} and pwd=#{pwd} </select> <insert id="insertUser" parameterType="com.li.vo.UserVO" flushCache="true"> insert into user(name,pwd) values(#{name},#{pwd}) </insert> <update id="updateUser" parameterType="com.li.vo.UserVO"> update user set name=#{name} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
5:新建UserServiceIMP服務介面:
package com.li.service;
import com.li.vo.UserVO;
public interface UserServiceIMP {
public UserVO selectUser(UserVO uservo);
public int insertUser(UserVO uservo);
public int updaqteUser(UserVO uservo);
public int deleteUserById(int user_id);
}
6:新建UserService繼承服務介面:
package com.li.service;
import com.li.IMP.UserDaoIMP;
import com.li.vo.UserVO;
public class UserService implements UserServiceIMP {
private UserDaoIMP userdao;
public int deleteUserById(int userId) {
// TODO Auto-generated method stub
return this.deleteUserById(userId);
}
public int insertUser(UserVO uservo) {
// TODO Auto-generated method stub
return this.userdao.insertUser(uservo);
}
public UserVO selectUser(UserVO uservo) {
// TODO Auto-generated method stub
return this.userdao.selectUser(uservo);
}
public int updaqteUser(UserVO uservo) {
// TODO Auto-generated method stub
return this.userdao.updaqteUser(uservo);
}
public void setUserdaoIMP(UserDaoIMP userdaoIMP) {
this.userdao = userdaoIMP;
}
public UserDaoIMP getUserdaoIMP() {
return userdao;
}
}
7:新建一個登陸控制器:
package com.li.controller;
import java.util.HashMap;
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.li.IMP.UserDaoIMP;
import com.li.service.UserService;
import com.li.vo.UserVO;
public class LoginController implements Controller {
private UserService userService;
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String name=request.getParameter("userName");
String password=request.getParameter("password");
//System.out.println("name-----"+name+"----password-----"+password);
UserVO uservo=new UserVO();
uservo.setName(name);
uservo.setPwd(password);
Map<String, Object> model=new HashMap<String, Object>();
if(userService.selectUser(uservo)!=null){
uservo=userService.selectUser(uservo);
System.out.println("能查到資訊");
model.put("uservo", uservo);
return new ModelAndView("WEB-INF/Main.jsp",model);
}else{
System.out.println("查不到資訊");
model.put("error", "使用者名稱或密碼錯誤");
return new ModelAndView("WEB-INF/Login.jsp",model);
}
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserService getUserService() {
return userService;
}
}
8: 新建一個mybatis-config.xml 配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="com/li/IMP/UserDaoIMP.xml"/><!--mapper對應的xml配置檔案-->
</mappers>
</configuration>
9:配置 applicationCotext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.li.IMP.UserDaoIMP"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="userService" class="com.li.service.UserService">
<property name="userdaoIMP" ref="userDao"></property>
</bean>
<bean id="LoginController" class="com.li.controller.LoginController">
<property name="userService" ref="userService"></property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">LoginController</prop>
</props>
</property>
</bean>
</beans>
10: 修改index.jsp的body部份為:
<body>
<jsp:forward page="WEB-INF/Login.jsp"></jsp:forward>
</body>
11:新增login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login.do" method="post">
<label>使用者名稱:</label><input type="text" name="userName"/><br/>
<label>密碼:</label><input type="password" name="password"/><br/>
<input type="submit" value="login"/><input type="reset" value="reset"/>
</form>
${error}
</body>
</html>
12: 新增main.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Main.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
進入主頁面<br/>
你的基本資訊如下:<br/>
id:${uservo.id}<br/>
使用者名稱:${uservo.name}<br/>
密碼:${uservo.pwd}<br/>
</body>
</html>