使用Myeclipse10.0自动生成搭建SSH框架(数据库表自动反向转换成Hibernate实体)实现用户登陆

11 篇文章 0 订阅
我这里使用的数据库是mysql5.0 数据是上课用的。这些都不是重点,重要的是学会这个方法;

创建好数据库:

create database jboadefault character set utf8;
show create database jboa1;

然后执行数据脚本

/*
Navicat MySQL Data Transfer

Source Server         : MysqlLoal
Source Server Version : 50022
Source Host           : localhost:3306
Source Database       : jboa1

Target Server Type    : MYSQL
Target Server Version : 50022
File Encoding         : 65001

Date: 2014-05-26 07:22:09
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for biz_account
-- ----------------------------
DROP TABLE IF EXISTS `biz_account`;
CREATE TABLE `biz_account` (
  `id` int(11) NOT NULL auto_increment,
  `usr_id` varchar(13) default NULL,
  `acc_type` varchar(2) default NULL,
  `acc_money` double default NULL,
  `sheet_id` varchar(13) default NULL,
  `acc_time` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of biz_account
-- ----------------------------

-- ----------------------------
-- Table structure for biz_check_result
-- ----------------------------
DROP TABLE IF EXISTS `biz_check_result`;
CREATE TABLE `biz_check_result` (
  `id` bigint(20) NOT NULL auto_increment,
  `checker_sn` varchar(20) NOT NULL COMMENT '审核人',
  `sheet_type` varchar(20) NOT NULL COMMENT '单据类型',
  `sheet_id` bigint(20) NOT NULL COMMENT '单据编号',
  `check_time` datetime NOT NULL COMMENT '审核时间',
  `type` varchar(20) NOT NULL COMMENT '审核类型',
  `result` varchar(20) NOT NULL COMMENT '审核结果',
  `comm` varchar(255) default NULL COMMENT '审核意见',
  PRIMARY KEY  (`id`),
  KEY `FK5E0EAAE06F568938` (`checker_sn`),
  CONSTRAINT `FK5E0EAAE06F568938` FOREIGN KEY (`checker_sn`) REFERENCES `sys_employee` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of biz_check_result
-- ----------------------------

-- ----------------------------
-- Table structure for biz_claim_voucher
-- ----------------------------
DROP TABLE IF EXISTS `biz_claim_voucher`;
CREATE TABLE `biz_claim_voucher` (
  `id` bigint(20) NOT NULL auto_increment,
  `create_sn` varchar(20) NOT NULL COMMENT '填报人',
  `next_deal_sn` varchar(20) default NULL COMMENT '待处理人',
  `create_time` date NOT NULL COMMENT '填写时间',
  `event` varchar(255) NOT NULL COMMENT '事由',
  `total_account` double NOT NULL COMMENT '总金额',
  `status` varchar(20) NOT NULL COMMENT '状态',
  PRIMARY KEY  (`id`),
  KEY `FKF5F9537FB1925E91` (`create_sn`),
  KEY `FKF5F9537F34489BD5` (`next_deal_sn`),
  CONSTRAINT `FKF5F9537F34489BD5` FOREIGN KEY (`next_deal_sn`) REFERENCES `sys_employee` (`sn`),
  CONSTRAINT `FKF5F9537FB1925E91` FOREIGN KEY (`create_sn`) REFERENCES `sys_employee` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of biz_claim_voucher
-- ----------------------------

-- ----------------------------
-- Table structure for biz_claim_voucher_detail
-- ----------------------------
DROP TABLE IF EXISTS `biz_claim_voucher_detail`;
CREATE TABLE `biz_claim_voucher_detail` (
  `id` bigint(20) NOT NULL auto_increment,
  `main_id` bigint(20) NOT NULL COMMENT '报销单(主单)编号',
  `item` varchar(20) NOT NULL COMMENT '项目',
  `account` double NOT NULL COMMENT '金额',
  `des` varchar(200) NOT NULL COMMENT '费用说明',
  PRIMARY KEY  (`id`),
  KEY `FKEC02A511C5CE4518` (`main_id`),
  CONSTRAINT `FKEC02A511C5CE4518` FOREIGN KEY (`main_id`) REFERENCES `biz_claim_voucher` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of biz_claim_voucher_detail
-- ----------------------------

-- ----------------------------
-- Table structure for sys_department
-- ----------------------------
DROP TABLE IF EXISTS `sys_department`;
CREATE TABLE `sys_department` (
  `id` int(11) NOT NULL auto_increment,
  `manager_sn` varchar(20) default NULL COMMENT '部门经理',
  `name` varchar(45) NOT NULL COMMENT '部门名称',
  PRIMARY KEY  (`id`),
  KEY `FK1AC9880422A0A640` (`manager_sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sys_department
-- ----------------------------
INSERT INTO `sys_department` VALUES ('1', '1', '1');

-- ----------------------------
-- Table structure for sys_dictionary
-- ----------------------------
DROP TABLE IF EXISTS `sys_dictionary`;
CREATE TABLE `sys_dictionary` (
  `id` bigint(20) NOT NULL auto_increment,
  `type` varchar(20) NOT NULL,
  `item` varchar(20) NOT NULL,
  `value` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sys_dictionary
-- ----------------------------

-- ----------------------------
-- Table structure for sys_employee
-- ----------------------------
DROP TABLE IF EXISTS `sys_employee`;
CREATE TABLE `sys_employee` (
  `sn` varchar(20) NOT NULL,
  `department_id` int(11) NOT NULL COMMENT '部门',
  `position_id` int(11) NOT NULL COMMENT '职务编号',
  `password` varchar(45) NOT NULL COMMENT '密码',
  `name` varchar(45) NOT NULL COMMENT '姓名',
  `status` varchar(20) NOT NULL COMMENT '状态',
  PRIMARY KEY  (`sn`),
  KEY `FKDE6BAFE015B8AA9F` (`department_id`),
  KEY `FKDE6BAFE019C7389` (`position_id`),
  CONSTRAINT `FKDE6BAFE015B8AA9F` FOREIGN KEY (`department_id`) REFERENCES `sys_department` (`id`),
  CONSTRAINT `FKDE6BAFE019C7389` FOREIGN KEY (`position_id`) REFERENCES `sys_position` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sys_employee
-- ----------------------------
INSERT INTO `sys_employee` VALUES ('admin', '1', '1', 'admin', 'admin', '1');

-- ----------------------------
-- Table structure for sys_position
-- ----------------------------
DROP TABLE IF EXISTS `sys_position`;
CREATE TABLE `sys_position` (
  `id` int(11) NOT NULL auto_increment,
  `name_cn` varchar(45) NOT NULL COMMENT '职务名称(中文)',
  `name_en` varchar(45) NOT NULL COMMENT '职务名称(英文)',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sys_position
-- ----------------------------
INSERT INTO `sys_position` VALUES ('1', '管理员', 'admin');

创建好Web Project后,加入Hibernate支持



选择配置好的数据库:

new 新建一个包:

添加Spring支持:


下面的意思是 : 在配置文件里面创建sessionFactory的引用

反向生成Hibernate 实体类: 右上角却换到Databse Explorer perspective 视图模式 下

选择要生成的表 右击 → Hibernate Reverse Engineering

MySql的主键自动生成策略可以用identity 或者native

需要改生成的类的 类名或者属性名 可以在这里改,一般我们要修改一下实体类的类名,因为有时候不可以和数据库一样的名字;

这里要指定主键类型,因为这个类主键不是自动生成的 是手动添加的,其他不用指定默认是前面指定的(自动生成)。

将自动生成的 类 和配置文件 都整理到com.jboa.model包中

将所有配置文件hbm.xml前面加上整理到的 包

<hibernate-mapping package="com.jboa.model">

然后将他们都配置到hibernate.cfg.xml文件可以直接选中配置文件然后拖拽到图形化界面的Hibernate配置文件,如图:

<mapping resource="com/jboa/model/Employee.hbm.xml" />
…..

在web.xml加上Spring的监听器

<!-- 整合Spring -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

添加Struts2支持

 

 

整合好了。

添加dao接口

public interface EmployeeDao extends Serializable {
	public Employee findById(String sn);
}

添加接口的实现类

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

	public Employee findById(String sn) {
		return this.getHibernateTemplate().get(Employee.class, sn);
	}

}

在Spring配置文件applicationContext.xml 配置HibernateDaoSupport的sessionFactory

<bean id="employeeDao" class="com.jboa.dao.impl.EmployeeDaoImpl" depends-on="sessionFactory">
		<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

创建Action ( EmployeeAction )

	private Employee employee;

	private EmployeeDao employeeDao;

	//设置set方法,这样在Spring配置文件就可以配置值了
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	//登陆验证
	public String login() {
		Employee e = employeeDao.findById(employee.getSn());
		if (e != null && employee.getPassword().equals(e.getPassword())) {
			ActionContext.getContext().getSession().put("loginUser", e);
			return SUCCESS;
		}
		return INPUT;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}


在dao的实现类实现findById方法

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

	public Employee findById(String sn) {
		return this.getHibernateTemplate().get(Employee.class, sn);
	}

}


在Spring的配置文件applicationContext.xml配置

<!-- scope="prototype"多例,表示每次使用都创建一个新的,单例则不创建新的 -->
	<bean id="employeeAction"  class="com.jboa.action.EmployeeAction" scope="prototype">
		<property name="employeeDao" ref="employeeDao"></property>
	</bean>

配置struts.xml文件

	<constant name="struts.devMode" value="true"></constant>
	<package name="jboa" extends="struts-default" namespace="/">
		<action name="login" class="employeeAction" method="login" >
			<result>/index.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
	</package>

这样就可以了 接下来在WebRoot创建登陆页面测试 login.jsp,在数据库employee表添加登陆测试数据

<form action="login" method="post">
    	登陆名:<input name="employee.sn" /><br/>
    	密码:<input type="password" name="employee.password" /><br/>
    	<input type="submit" />
   </form>

测试 部署启动tomcat,访问login.jsp,登陆成功;
成功!



 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值