(说明目的是要做在线聊天室的,也包含登陆注册部分,先用这部分做个例子)开发环境用的是STS(spring tool suite)基于Eclipse开发的spring整合开发环境。
主要的几个文件说明:userbean.java 用户的bean文件,userbean.hbm.xml是他的hibernate映射文件。application-config.xml文件是spring的配置文件,里面包含数据源配置(连数据库),bean设置等。mvc-config.xml文件是springmvc 的配置文件。
userbean.java
public class UserBean {
private String id;
private String username;
private String password;
private String phone;
private String email;
private String address;
public String getId() {
return id;
}
public void setId(String 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;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserDao.java
public interface UserDao {
public UserBean getUser(String id);
public void insert(UserBean user);
void delete(String id);
List queryUser(String username);
void updateAddress(String userid, String address);
void updatePhone(String userid, String phone);
void updateEmail(String userid, String email);
void updatePassword(String userid, String password);
void updateName(String userid, String username);
}
UserDaoImp.java
public class UserDaoImp implements UserDao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public UserBean getUser(String id){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, id);
session.close();
return user;
}
@Override
public void insert(UserBean user){
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
}
@Override
public void delete(String id){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, id);
session.delete(user);
session.getTransaction().commit();
session.close();
}
@Override
public List queryUser(String username){
Session session=sessionFactory.openSession();
session.beginTransaction();
String str="from UserBean user where username='"+username+"'";
Query query=session.createQuery(str);
List userlist=query.list();
session.close();
return userlist;
}
@Override
public void updateName(String userid,String username){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, userid);
user.setUsername(username);
session.update(user);
session.getTransaction().commit();
session.close();
}
@Override
public void updatePassword(String userid,String password){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, userid);
user.setPassword(password);;
session.update(user);
session.getTransaction().commit();
session.close();
}
@Override
public void updateEmail(String userid,String email){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, userid);
user.setEmail(email);;
session.update(user);
session.getTransaction().commit();
session.close();
}
@Override
public void updatePhone(String userid,String phone){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, userid);
user.setPhone(phone);;
session.update(user);
session.getTransaction().commit();
session.close();
}
@Override
public void updateAddress(String userid,String address){
Session session=sessionFactory.openSession();
session.beginTransaction();
UserBean user=(UserBean) session.get(UserBean.class, userid);
user.setAddress(address);;
session.update(user);
session.getTransaction().commit();
session.close();
}
public static void main(String args[]){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/application-config.xml");
UserDaoImp udi=context.getBean(UserDaoImp.class);
UserBean user=new UserBean();
user.setUsername("zhangsan");
user.setPassword("zhangsan");
udi.insert(user);
List ulist=udi.queryUser("zhangsan");
for(int i=0;i
SignupController.java
@Controller
@RequestMapping(value="/signup")
public class SignupController {
@RequestMapping()
public ModelAndView signup(){
ModelAndView mav=new ModelAndView();
mav.setViewName("signup");
mav.addObject("tag", 0);
return mav;
}
@RequestMapping(value="/adduser")
public ModelAndView addUser(String username,String password,String email,String phone,String address){
ModelAndView mav=new ModelAndView();
UserBean user=new UserBean();
user.setEmail(email);
user.setPassword(password);
user.setPhone(phone);
user.setUsername(username);
user.setAddress(address);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/application-config.xml");
UserDaoImp udi=context.getBean(UserDaoImp.class);
List userlist=udi.queryUser(username);
if(userlist.size()==0){
udi.insert(user);
mav.setViewName("login");
}else{
//用户名已经存在
mav.setViewName("signup");
mav.addObject("tag", 1);
}
return mav;
}
}
UserBean.hbm.xml
signup.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Sign Up
Sign
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
login
Log In
application-confg.xml
org.hibernate.dialect.MySQLDialect
update
true
true
classpath:/com/hainan/cs/hbm/*.hbm.xml
mvc-config.xml
web.xml
online_chart
contextConfigLocation
classpath:spring/application-config.xml
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/mvc-config.xml
1
dispatcherServlet
/
pom.xml
4.0.0
org.springframework.samples.service.service
online_chart
0.0.1-SNAPSHOT
war
1.6
UTF-8
UTF-8
2.2
1.2
2.5
3.2.3.RELEASE
4.2.1.Final
1.0.13
1.7.5
4.11
org.springframework
spring-webmvc
${spring-framework.version}
javax.servlet
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet.version}
provided
javax.servlet.jsp
jsp-api
${jsp.version}
provided
org.springframework
spring-tx
${spring-framework.version}
org.slf4j
slf4j-api
${slf4j.version}
compile
ch.qos.logback
logback-classic
${logback.version}
runtime
org.hibernate
hibernate-entitymanager
${hibernate.version}
org.springframework
spring-test
${spring-framework.version}
test
junit
junit
${junit.version}
test
org.springframework
spring-orm
${spring-framework.version}
mysql
mysql-connector-java
5.1.9
commons-dbcp
commons-dbcp
1.4
注册页面
提交后数据库显示
注册页面还包含邮箱验证等,关于在线聊天室的开始会在这个的基础上后期进行开发。