1、创建数据库
create database crm; use crm;/*SQLyog v10.2 MySQL - 5.1.72-community : Database - crm_hibernate**********************************************************************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;/*Table structure for table `sys_user` */CREATE TABLE `sys_user` ( `user_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '用户id', `user_code` varchar(32) NOT NULL COMMENT '用户账号', `user_name` varchar(64) NOT NULL COMMENT '用户名称', `user_password` varchar(32) NOT NULL COMMENT '用户密码', `user_state` char(1) NOT NULL COMMENT '1:正常,0:暂停', PRIMARY KEY (`user_id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;/*Data for the table `sys_user` */LOCK TABLES `sys_user` WRITE;insert into `sys_user`(`user_id`,`user_code`,`user_name`,`user_password`,`user_state`) values (5,'m0003','小军','123','1'),(6,'m0001','小红','123','1'),(7,'m0001','小明','123','1'),(8,'m0001','小红','123','1');UNLOCK TABLES;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
2、创建用户实体/crm/src/com/louis/domain/User.java
package com.louis.domain;public class User { // 主键 private Long user_id; // 登录名称 private String user_code; // 用户姓名 private String user_name; // 密码(保存的时候,需要加密处理) private String user_password; // 用户的状态 1正常 0暂停 private String user_state; public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public String getUser_state() { return user_state; } public void setUser_state(String user_state) { this.user_state = user_state; }}
3、编写hibernate映射文件/crm/src/com/louis/domain/User.hbm.xml
4、引入映射文件/crm/src/applicationContext.xml
com/louis/domain/User.hbm.xml
5、创建用户Action/crm/src/com/louis/web/action/UserAction.java
package com.louis.web.action;import com.louis.domain.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class UserAction extends ActionSupport implements ModelDriven{ private static final long serialVersionUID = -3413092622818913571L; private User user = new User(); public User getModel() { return user; }}
6、配置用户模块/crm/src/applicationContext.xml
7、配置用户请求/crm/src/struts.xml
/index.jsp
8、创建服务类/crm/src/com/louis/service/impl/UserServiceImpl.java
package com.louis.service.impl;import org.springframework.transaction.annotation.Transactional;import com.louis.service.UserService;@Transactionalpublic class UserServiceImpl implements UserService {}
9、配置服务类/crm/src/applicationContext.xml
10、action类添加依赖注入/crm/src/com/louis/service/impl/UserServiceImpl.java
private UserService userService; public void setUserService(UserService userService) { this.userService = userService; }
11、配置依赖注入/crm/src/applicationContext.xml
12、创建dao/crm/src/com/louis/dao/impl/UserDaoImpl.java
package com.louis.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.louis.dao.UserDao;public class UserDaoImpl extends HibernateDaoSupport implements UserDao {}
13配置dao/crm/src/applicationContext.xml
14、service注入dao
private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; }
15、配置dao的属性
至此,基本的项目建好了
16、创建注册页面regist.jsp
// 验证登录名 function checkCode(){ // 获取用户输入的登录名 var code = $("#user_code").val(); // 进行判断,说明没有输入登录名 if(code.trim() == ""){ // 给提示 $("#codeId").addClass("error"); $("#codeId").html("登录名不能为空"); }else{ // 登录名不为空,ajax请求,验证 var url = "${pageContext.request.contextPath}/user_checkCode.action"; var param = {"user_code":code}; $.post(url,param,function(data){ // 操作data,进行判断后台返回的是字符串,可以直接判断 if(data && data == "no"){ // 提示 $("#codeId").addClass("error"); $("#codeId").html("登录名已经存在"); }else{ $("#codeId").removeClass("error"); $("#codeId").html("可以注册"); } }); } } // 可以阻止表单的提交 function checkForm(){ // 先让校验名称的方法先执行以下 checkCode(); // 获取error的数量,如果数量 > 0,说明存在错误,不能提交表单 if($(".error").size() > 0){ return false; } }
17、/crm/src/com/louis/web/action/UserAction.java
/** * 通过登录名,判断,登录名是否已经存在 * @return */ public String checkCode(){ // 调用业务层,查询 User u = userService.checkCode(user.getUser_code()); // 获取response对象 HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); try { // 获取输出流 PrintWriter writer = response.getWriter(); // 进行判断 if(u != null){ // 说明:登录名查询到用户了,说明登录已经存在了,不能注册 writer.print("no"); }else{ // 说明,不存在登录名,可以注册 writer.print("yes"); } } catch (IOException e) { e.printStackTrace(); } return NONE; }
18、/crm/src/com/louis/service/UserServiceImpl.java
/** * 通过登录名进行验证 */ public User checkCode(String user_code) { return userDao.checkCode(user_code); }
19、/crm/src/com/louis/dao/UserDaoImpl.java
/** * 通过登录名进行验证 */ public User checkCode(String user_code) { Listlist = (List ) this.getHibernateTemplate().find("from User where user_code = ?", user_code); if (list != null && list.size() > 0) { return list.get(0); } return null; }
问题
scope="prototype" 配置文件
ModelDriven
HibernateDaoSupport