User.java-->User.hbm.xml-->Hibernate.cfg.xml-->HibernateUtil.java
-->UserDao.java-->UserDaoImpl.java-->UserManager.java-->UserManagerImpl.java
|-->UserManagerTest.java
-->| |-->registFail.jsp
|-->regist.jsp-->registDeal.jsp-->|
|-->registSuccess.jsp
User.java
package com.bjsxt.registration.model;
//贫血模型 充血模型
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- This mapping demonstrates how to use an "inverse" join to map an association
table as an association with one-to-many multiplicity at the level of the
object model. -->
<hibernate-mapping package="com.bjsxt.registration.model">
<class name="User" table="user">
<id name="id" type="integer" column="id">
<generator class="native" />
</id>
<property name="username" type="string" column="username"/>
<property name="password" type="string" column="password"/>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<!-- 数据库连接:方言、驱动、连接、用户名、密码 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/spring</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 自动构建表结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- 映射配置 -->
<mapping resource="com/bjsxt/registration/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package com.bjsxt.registration.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
UserDao.java
package com.bjsxt.registration.dao;
import com.bjsxt.registration.model.User;
public interface UserDao {
/** 保存数据 */
public void save(User user);
/** 查询某个数据是多少条 */
public Long getCount(User user);
}
UserDaoImpl.java
package com.bjsxt.registration.dao.impl;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.util.HibernateUtil;
public class UserDaoImpl implements UserDao {
public void save(User user) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
}
public Long getCount(User user) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Long count = (Long) session.createQuery(//
"select count(u) from User u where username=:name")//
.setParameter("name", user.getUsername())//
.uniqueResult();
tx.commit();
session.close();
return count;
}
}
UserManager.java
package com.bjsxt.registration.service;
import com.bjsxt.registration.model.User;
public interface UserManager {
/** 判断是否存在 */
public boolean exists(User user);
/** 增加数据 */
public void add(User user);
}
UserManagerImpl.java
package com.bjsxt.registration.service.impl;
import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.dao.impl.UserDaoImpl;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;
public class UserManagerImpl implements UserManager {
private UserDao userDao = new UserDaoImpl();
public boolean exists(User user) {
Long count = userDao.getCount(user);
if (count > 0) {
return true;
}
return false;
}
public void add(User user) {
userDao.save(user);
}
}
UserManagerTest.java
package com.bjsxt.registration.service;
import org.junit.Assert;
import org.junit.Test;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.impl.UserManagerImpl;
public class UserManagerTest {
private UserManager um = new UserManagerImpl();
@Test
public void testExists() {
User u = new User();
u.setUsername("a");
boolean exists = um.exists(u);
Assert.assertEquals(true, exists);
}
@Test
public void testAdd() {
User u = new User();
u.setUsername("123");
u.setpassword("a");
boolean exists = um.exists(u);
if (!exists) {
um.add(u);
u.setUsername(u.getUsername());
Assert.assertEquals(true, um.exists(u));
} else {
Assert.fail("not added");
}
}
}
regist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h1>用户注册</h1><br/>
<form action="registDeal.jsp" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
确认密码:<input type="password" name="password2"/><br/>
<input type="submit" value=" 提 交 "/>
</form>
</body>
</html>
registDeal.jsp
<%@ page language="java" import="java.util.*, java.sql.*"
pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.registration.model.User"%>
<%@ page import="com.bjsxt.registration.service.UserManager"%>
<%@ page import="com.bjsxt.registration.service.impl.UserManagerImpl"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
User u = new User();
u.setUsername(username);
u.setpassword(password);
UserManager um = new UserManagerImpl();
boolean exits = um.exists(u);
if (exits) {
response.sendRedirect("registFail.jsp");
return;
}
um.add(u);
response.sendRedirect("registSuccess.jsp");
%>
registFail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>失败</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h1>Fail!</h1>
</body>
</html>
registSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h1>Success!</h1>
</body>
</html>