spring3.1 mvc jdbc 合成

2019-04-15 13:33发布

[img]http://dl.iteye.com/upload/picture/pic/120528/085e51d0-b901-321a-9da7-e83a19a65695.png[/img]
[img]http://dl.iteye.com/upload/picture/pic/120530/dc995d62-94ef-3da9-b258-6166b654799a.png[/img]
代码详见:
实现用户的登录和注册功能:

package com.model;
import java.io.Serializable;
/**
* 模型层
* @author qiyang
*
*/
@SuppressWarnings("serial")
public class User implements Serializable {
//http://my.oschina.net/huangcongmin12/blog/81216
private int id;
private String userName;
private String password;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return id+", "+userName+", "+password;
}
}

package com.dao.implement;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.dao.UserDao;
import com.model.User;
/**
*
* @author qiyang
* 数据层
*/
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/**find user by username*/
public User findUserByUserName(String userName) {
String sqlStr = "select id,uname,pwd from user where uname=?";
//注意此处为何如此写?
final User user = new User();
jdbcTemplate.query(sqlStr, new Object[]{userName},new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("uname"));
user.setPassword(rs.getString("pwd"));
}
});
return user;
}
/**register*/
public void register(User user) {
String sqlStr = "insert into user(uname,pwd) values(?,?)";
Object [] params = new Object[]{user.getUserName(),user.getPassword()};
jdbcTemplate.update(sqlStr,params);
}

}

package com.service.implement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.UserDao;
import com.model.User;
import com.service.UserService;
/**
* 业务层
* @author qiyang
*
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

public User loginCheck(User user) {
//此处获得的 u 会为空吗?如果会,程序有可能跑异常哦
User u = userDao.findUserByUserName(user.getUserName());
System.out.println("id="+u.getId()+", userName="+u.getUserName()+", Password="+u.getPassword());
if(user.getPassword().equals(u.getPassword())){
return u;
}else{
return null;
}
}

public boolean register(User user) {
//此处先查询用户名是否已经存在
User u = userDao.findUserByUserName(user.getUserName());
if(u.getId()==0){
//注册
userDao.register(user);
return true;
}else{
System.out.println("用户已经存在!");
System.out.println("id="+u.getId()+", userName="+u.getPassword()+", Password="+u.getPassword());
return false;
}
}

}

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.model.User;
import com.service.UserService;
/**
* 表示层--控制器
* @author qiyang
*
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/loginView")
public String loginView(){
return "login";
}

@RequestMapping("/user/registView")
public String registView(){
return "register";
}

@RequestMapping("/user/login")
public ModelAndView login(User user){
System.out.println("用户登录!!!");
ModelAndView mav = new ModelAndView();
User u = userService.loginCheck(user);
System.out.println(u);
if(null==u){
System.out.println("登录失败了........");
//登录失败
mav.setViewName("login");
mav.addObject("errorMsg", "用户名或密码错误!");
return mav;
}else{
//登录成功
System.out.println("登录成功了........");
mav.setViewName("success");
mav.addObject("user",u);
return mav;
}
}

@RequestMapping("/user/register")
public ModelAndView register(User user){
ModelAndView mav = new ModelAndView();
if(userService.register(user)){
//注册成功
mav.setViewName("register_succ");
return mav;
}else{
//注册失败
mav.setViewName("register");
mav.addObject("errorMsg","用户名已经被占用");
return mav;
}
}

}


主要配置文件:
beans.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">










${jdbc.driverClassName}


${jdbc.url}


${jdbc.username}


${jdbc.password}



5



30



10



60



5



0



60



30
































jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=*****

页面部分:
登录:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




login



Login
















${errorMsg}

uname:
pwd:

register






注册页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




login



Register
















${errorMsg}

uname:
pwd:




成功页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




login


亲爱的${user.userName },登录成功!





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




login


注册成功!


返回登录页面



web.xml:

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">

index.jsp



org.springframework.web.util.IntrospectorCleanupListener



spring
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:applicationContext.xml

1



spring
*.do



setcharacter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



setcharacter
/*