#配置文件:
applicationContext.xml
org.hibernate.dialect.MySQLDialect
com/sunline/entity/User.hbm.xml
/success.jsp
/error.jsp
User.java
package com.sunline.entity;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer userId;
private String userName;
private String userPassword;
private String userStatus;
// Constructors
/** default constructor */
public User() {
}
/** full constructor */
public User(String userName, String userPassword, String userStatus) {
this.userName = userName;
this.userPassword = userPassword;
this.userStatus = userStatus;
}
// Property accessors
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserStatus() {
return this.userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
}
UserDao.java
package com.sunline.dao;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.sunline.entity.User;
@Repository()
public class UserDao extends HibernateDaoSupport {
//hibernateTemplate是Spring提供的Hibernate Dao操作类,在applicationContext中已经完成bean注入,所以此处直接 可以拿过来使用
// @Resource(name="hibernateTemplate")
// private HibernateTemplate hibernateTemplate;
/*
* 用户登录
*/
@SuppressWarnings("unchecked")
public List Serarch(final User user){
return super.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
// TODO 自动生成方法存根
Criteria c=session.createCriteria(User.class);
if(user!=null){
if(user.getUserName()!=null && !user.getUserName().equals("")){
c.add(Restrictions.eq("userName", user.getUserName()));
}
if(user.getUserPassword()!=null && !user.getUserPassword().equals("")){
c.add(Restrictions.eq("userPassword", user.getUserPassword()));
}
}
return c.list();
}
});
}
/*
* 添加用户
*/
public void AddUser(User user){
System.out.println("成功添加用户");
this.getHibernateTemplate().save(user);
}
/*
* 根据id查询用户数据
*/
public User findById(int id) {
User user=(User)this.getHibernateTemplate().get(User.class,id);
return user;
}
/*
* 查找所有用户
*/
public List findAll(){
String sql="from User";
List list=this.getHibernateTemplate().find(sql);
return list;
}
/*
* 修改用户信息
*/
public void update(User user) {
System.out.println("成功修改数据!");
this.getHibernateTemplate().update(user);
}
/*
* 根据id删除用户信息
*/
public void DeleteUser(int id){
System.out.println("成功删除数据!");
this.getHibernateTemplate().delete(findById(id));
}
/*
* 根据用户名和密码查找用户
*/
@SuppressWarnings("unchecked")
public List GetUserInfo(User user){
String hsql= "from User where userName= :userName and userPassword = :userPassword";
String paramNames[] = {"userName" , "userPassword"};
String paramValues[] = {user.getUserName(),user.getUserPassword()};
//findByNamedParam方法提供hql方法参数查询,避免Sql注入漏洞
List result = (List) this.getHibernateTemplate().findByNamedParam(hsql, paramNames, paramValues);
// List result=(List) (((Query) (this.getHibernateTemplate().find(hsql, new String[]{userName, userPassword}))).uniqueResult());
return result;
}
/*
* 根据姓名查询得出对象
*/
@SuppressWarnings("unchecked")
public List findByName(String name) {
String hsql="from User where userName= :userName";
List result = (List) this.getHibernateTemplate().findByNamedParam(hsql,"userName",name);
// User result=(User)((Query) this.getHibernateTemplate().find(hsql)).uniqueResult();
return result;
}
/*
* 修改单个字段
*/
public void UpdateUserName(String userName, int userId){
this.getHibernateTemplate().bulkUpdate("update User set userName = ? where userId = ?",new Object[]{userName,userId}) ;
}
/*
* 修改多个字段
*/
public void UpdateManyParam(String userName,String userPassword, int userId){
String hsql ="update User set userName = ? , userPassword = ? where userId = ?";
System.out.println("成功修改数据!");
this.getHibernateTemplate().bulkUpdate(hsql, new Object[]{userName,userPassword,userId});
}
/*
* 多条件模糊查询
*/
@SuppressWarnings("unchecked")
public List findByMohu(String userName, String userPassword){
String hsql = "from User where userName like ? and userPassword = ?";
String user_name = "%"+userName+"%";
String []param = {user_name,userPassword};
List list = (List) this.getHibernateTemplate().find(hsql,param);
System.out.println("成功模糊查询到数据! "+list);
return list;
}
}
UserBiz.java
package com.sunline.biz;
import java.util.List;
import com.sunline.dao.UserDao;
import com.sunline.entity.User;
public class UserBiz {
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/*
* 登录功能逻辑层
*/
public List Login(User user){
return userDao.Serarch(user);
}
/*
* 添加用户逻辑层
*/
public void AddUser(User user){
userDao.AddUser(user);
}
/*
* 查找全部用户
*/
public List findALL() {
return userDao.findAll();
}
/*
* 修改用户信息
*/
public void UpdateUser(User user){
userDao.update(user);
}
/*
* 根据用户id删除用户信息
*/
public void deleteUser(int id) {
userDao.DeleteUser(id);
}
/*
* 根据用户名和密码查找用户
*/
public List getUser(User user ) {
return userDao.GetUserInfo(user);
}
/*
* 根据用户名查出对象
*/
public User findByName(String name) {
List result= userDao.findByName(name);
if(result.size() > 0) {
return result.get(0);
}
return null;
}
/*
* 修改单个字段
*/
public void UpdateUserName(String userName, int userId) {
userDao.UpdateUserName(userName, userId);
}
/*
* 修改多个字段
*/
public void UpdateManyParam(String userName,String userPassword, int userId){
userDao.UpdateManyParam(userName, userPassword, userId);
}
/*
* 多条件模糊查询
*/
public List findByMohu(String userName, String userPassword){
return userDao.findByMohu(userName, userPassword);
}
}
LoginAction.java
package com.sunline.action;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sunline.biz.UserBiz;
import com.sunline.entity.User;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1889710968497585080L;
private String userName;
private String userPassword;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Override
public String execute() throws Exception {
//加载applicationContext.xml配置
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取配置中的实例
UserBiz userBiz =(UserBiz)context.getBean("userBiz");
User user = new User();
user.setUserName(this.userName);
user.setUserPassword(this.userPassword);
List list = userBiz.Login(user);
if(list.size()>=1){
ActionContext text = ActionContext.getContext();
Map session = text.getSession();
// session.put("user",list);
session.put("user",this.userName);
return SUCCESS;
}else{
return ERROR;
}
}
}
Test.java
package com.sunline.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sunline.biz.UserBiz;
import com.sunline.entity.User;
public class Test {
public static void main(String[] args) {
/*
* 1.登录功能测试
*/
//加载applicationContext.xml配置
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取配置中的实例
UserBiz userBiz =(UserBiz)context.getBean("userBiz");
User user = new User();
user.setUserName("haige");
user.setUserPassword("123456");
List list = userBiz.Login(user);
if(list.size()>=1){
System.out.println("你的用户大小为"+list.size()+",用户名为:"+list+",登录成功!");
}else{
System.out.println("你的用户大小为"+list.size()+",用户名为:"+list+",登录失败!");
}
/*
* 2.添加用户测试
*/
user.setUserName("xiaozhang");
user.setUserPassword("888888");
user.setUserStatus("true");
userBiz.AddUser(user); //添加用户测试
/*
* 3.查找全部用户测试
*/
List list = userBiz.findALL();
for(User use : list){
System.out.println(use.getUserName()+" "+use.getUserPassword()+" "+use.getUserStatus());
}
/*
* 4.根据用户名和密码查找用户
*/
user.setUserName("haige");
user.setUserPassword("123456");
List list = (List)userBiz.getUser(user);
for(User use : list){
System.out.println("你的用户信息为:"+use.getUserName()+" "+use.getUserPassword()+" "+use.getUserStatus());
}
System.out.println("你的用户信息为:"+list.size());
/*
* 5.根据主键id删除数据
*/
userBiz.deleteUser(3);
/*
* 6.修改用户信息
*/
user.setUserId(2);
user.setUserName("sunhonglei");
user.setUserPassword("123456");
user.setUserStatus("true");
userBiz.UpdateUser(user);
/*
* 7.根据用户名查出对象
*/
User U = (User)userBiz.findByName("haige");
System.out.println("你的用户信息为:"+U.getUserName()+" "+ U.getUserPassword() + " "+ U.getUserStatus());
/*
* 8.修改单个字段
*/
userBiz.UpdateUserName("zhangxiaoyu", 2);
/*
* 9.修改多个字段
*/
userBiz.UpdateManyParam("helijian", "123456", 2);
/*
* 10.多条件模糊查询
*/
List list = userBiz.findByMohu("h", "123456");
for(User use :list){
System.out.println("你的用户信息为:"+use.getUserName()+" "+use.getUserPassword()+" "+use.getUserStatus());
}
System.out.println("你的集合大小为:" + list.size());
}
}
#前端视图层
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录验证
用户登录
${message}
${verify}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'success.jsp' starting page
登录成功!
欢迎你,${user}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'error.jsp' starting page
登录失败!
请检查用户名/密码输入是否正确!
工程源码下载地址: 点击----> 源码下载