利用Spring-MVC访问数据库

1、创建相关的项目。首先相关路径,在pom.xml中添加具有Spring-mvc、mysql、dbcp的三个包。

利用Spring-MVC访问数据库_第1张图片

2、首先创建Admin类与数据库信息对应。

2.1、数据库:

2.2、 jdbc-properties(名称不可改变)   user是数据库名 

url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
driverClass=com.mysql.jdbc.Driver
user=root
password=123456
maxActive=1
initSize=1

2.3、Admin类

package com.zhiyou100.dao;
import java.io.Serializable;
public class  Admin implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String adminId;
	private String password;
	private String telephone;
	private String adminCode;
	private String name;
	
	public Admin(String adminId, String password, String telephone, String adminCode, String name) {
		super();
		this.adminId = adminId;
		this.password = password;
		this.telephone = telephone;
		this.adminCode = adminCode;
		this.name = name;
	}
	public Admin(String password, String telephone, String adminCode, String name) {
		super();
		this.password = password;
		this.telephone = telephone;
		this.adminCode = adminCode;
		this.name = name;
	}
	public Admin() {
		super();
	}
	public String getAdminId() {
		return adminId;
	}
	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getAdminCode() {
		return adminCode;
	}
	public void setAdminCode(String adminCode) {
		this.adminCode = adminCode;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
 
}

3.创建AdminDao接口,为通过findByCode方法,实现与数据库连接、查询操作。

package com.zhiyou100.dao;

public interface AdminDao {
Admin findByCode(String adminCode);
}

4、创建AdminDaoImp类实现AdminDao接口的方法

package com.zhiyou100.dao;

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

import javax.annotation.Resource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;

@Repository
public class AdminDaoImp implements AdminDao {
	//注入数据源
	@Resource
	private BasicDataSource ds;
	public Admin findByCode(String adminCode) {
		if(adminCode==null) {
			return null;
		}
		Connection  conn=null;
		try {
			conn=ds.getConnection();
			String sql="select *from user where adminCode=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, adminCode);
			ResultSet rSet=ps.executeQuery();
			if(rSet.next()) {
				Admin admin=new Admin();
				admin.setAdminCode(rSet.getString("adminCode"));
				admin.setAdminId(rSet.getString("adminId"));
				admin.setName(rSet.getString("name"));
				admin.setPassword(rSet.getString("password"));
				admin.setTelephone(rSet.getString("telephone"));
				return admin;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}

5、创建LoginService,利用LoginService逻辑判断是否登录成功

package com.zhiyou100.service;
import com.zhiyou100.dao.Admin;
//利用LoginService逻辑判断是否登录成功
public interface LoginService {
   Admin checkAdmin(String adminCode,String password);
}

6、创建LoginServiceImpl的类实现LoginService,判断是否登录成功

package com.zhiyou100.service;

import javax.annotation.Resource;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Service;
import com.zhiyou100.Exception.AdminCodeException;
import com.zhiyou100.Exception.PasswordException;
import com.zhiyou100.dao.Admin;
import com.zhiyou100.dao.AdminDao;

@Service
public class LoginServiceImpl implements LoginService {
	@Resource
	private AdminDao adminDao;

	public Admin checkAdmin(String adminCode, String password) 
			throws PasswordException, AdminCodeException {
		Admin admin = adminDao.findByCode(adminCode);
		if (admin == null) {
			throw new AdminCodeException("账号出错");
		} else if (!admin.getPassword().equals(password)) {
			throw new PasswordException("密码出错");
		} else {
			return admin;
		}
	}
}

7.在运行过程中会有异常的出现,所以处理异常

AdminCodeException异常

package com.zhiyou100.Exception;

/**
 * @author Administrator
 *
 */
public class AdminCodeException extends RuntimeException{
	public AdminCodeException(String message) {
		super(message);
	}
}

PasswordException异常

/**
 * 
 */
package com.zhiyou100.Exception;

/**
 * @author Administrator
 *
 */
public class PasswordException extends RuntimeException{
  public PasswordException(String message) {
	  super(message);
  }
}

8.创建LoginContrller类,约定方法可以返回一个ModelAndView对象,封装了处理结果数据和视图名称信息。

package com.zhiyou100.etity;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.Exception.AdminCodeException;
import com.zhiyou100.Exception.PasswordException;
import com.zhiyou100.dao.Admin;
import com.zhiyou100.service.LoginService;

@Controller
@RequestMapping("/login")
public class LoginContrller {
	@Resource
	private LoginService loginService;
	@RequestMapping("/toLogin.do")
	public String toLogin() {
		return "main/login";
	}
	@RequestMapping("/toIndex.do")
	public String toIndex() {
		return "main/index";
	}
	@RequestMapping("/checkLogin.do")
	public String checkLogin(HttpSession session,
			String adminCode,
			String password,
			ModelMap model) {
		try {
			Admin admin=loginService.checkAdmin(adminCode, password);
			  //登录成功后,把用户信息放进Session中
			session.setAttribute("admin",admin);
		} catch (AdminCodeException e) {
			// TODO: handle exception
		model.addAttribute("message",e.getMessage());
		model.addAttribute("adminCode", adminCode);
		model.addAttribute("password", password);
		return "main/login";
		}
		catch (PasswordException e) {
			// TODO: handle exception
		model.addAttribute("message",e.getMessage());
		model.addAttribute("adminCode", adminCode);
		model.addAttribute("password", password);
		return "main/login";
		}
		//登录成功后重定向到主界面
		return "redirect:toIndex.do";
//		BasicDataSource
	}
}

9.在WEB-INF 下建立对应的jsp.

在运行后,输入相应的网址eg:login/toLogin.do ,login/toIndex.do,login/checkLogin会跳转到相对的jsp.

刚开始写博客,有错误的地方,欢迎指出!

不喜勿喷!!!

你可能感兴趣的:(SpringMVC)