雇员管理系统-SSH版(一)

一 介绍
ssh(struts+spring+hibernate)三大框架配合使用来开发项目,是目前java ee最流行的开发方式,必须掌握。
我们通过一个实际的案例,来介绍整合,我们使用一个雇员薪资管理系统(crud),每加入一个框架,我们就测试通过。

二 具体步骤
创建web项目
1 先搞定spring。
2 引入spring开发包。
3 编写applicationContext.xml文件(或者beans.xml),我们把该文件放在 src目录下。
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:context="http://www.springframework.org/schema/context";
                xmlns:tx="http://www.springframework.org/schema/tx";
                xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";>
<!-- 启用注解扫描 -->
<context:annotation-config/>
<!-- 配置和一个testService对象 -->
<bean id="testServcie" class="com.hsp.test.TestServcie">
<property name="name" value="test"/>
</bean>
</beans>
4 测试一下spring,是否ok,目前spring可以工作。 
package com.hsp.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hsp.domain.Department;
import com.hsp.domain.Employee;
import com.hsp.service.interfaces.DepartmentServiceInter;
import com.hsp.service.interfaces.EmployeeServiceInter;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        TestServcie ts = (TestServcie)ac.getBean("testServcie");
        System.out.println(ts.getName());
    }
}
测试结果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
test
5 加入hibernate开发包。
6 因为我们是ssh,所以我们hibernate的核心,就被spring接管了。hibernate.cfg.xml文件、对象映射文件、SessionFactory在spring的文件中配置即可。
7 在applicationContext.xml中配置数据源。
8 配置SessionFactory对象。
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:context="http://www.springframework.org/schema/context";
                xmlns:tx="http://www.springframework.org/schema/tx";
                xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";>
<!-- 启用注解扫描 -->
<context:annotation-config/>
<!-- 配置和一个testService对象 -->
<bean id="testServcie" class="com.hsp.test.TestServcie">
<property name="name" value="test"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
             <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="3"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="500"/>
 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2"/>
        <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1"/>
</bean>
<!-- 配置会话工厂() -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <!-- 设置数据源 -->
     <property name="dataSource" ref="dataSource"/>
     <!-- 接管了hibernate对象映射文件 -->
     <property name="mappingResources">
            <list>
              <value>com/hsp/domain/Employee.hbm.xml</value>
              <value>com/hsp/domain/Department.hbm.xml</value>
            </list>
     </property>
     <property name="hibernateProperties">
            <value>
                        hibernate.dialect=org.hibernate.dialect.MySQLDialect
                        hibernate.hbm2ddl.auto=update
                                hibernate.show_sql=true
                                hibernate.cache.use_second_level_cache=true
                    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                    hibernate.generate_statistics=true             
         </value>
    </property>
</bean>
<!-- 配置EmployeeService对象 -->
<!-- 
<bean id="employeeService" class="com.hsp.service.imp.EmployeeService">
        <property name="sessionFactory" ref="sessionFactory" />
</bean>-->
<!-- 配置EmployeeService对象通过注解的方式来注入属性值,这是一个知识点 -->
<bean id="employeeService" class="com.hsp.service.imp.EmployeeService"/>
<bean id="departmentService" class="com.hsp.service.imp.DepartmentService">
        <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置action -->
<bean name="/login" scope="prototype" class="com.hsp.web.action.LoginAction"/>
<bean name="/employee" scope="prototype" class="com.hsp.web.action.EmployeeAction">
<property name="departmentService" ref="departmentService"/>
<property name="employeeService" ref="employeeService"/>
</bean>
<!-- 配置事务管理器,统一管理sessionFactory的事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
9 我们编写domain对象和映射文件Employee.hbm.xml ->测试srping和hibernate是否可以结合使用->ok
9.1 Employee
package com.hsp.domain;
import java.util.Date;
public class Employee {
        private Integer id;
        private String name;
        private String email;
        private String pwd;
        private Integer grade;
        private java.util.Date hiredate;
        private Float salary;
        
        //empoyee->department
        private Department department;
        
        public Department getDepartment() {
                return department;
        }
        public void setDepartment(Department department) {
                this.department = department;
        }
        public Employee(){
                
        }
        
        public Employee(String name, String email, String pwd, Integer grade,
                        Date hiredate, Float salary) {
                
                this.name = name;
                this.email = email;
                this.pwd = pwd;
                this.grade = grade;
                this.hiredate = hiredate;
                this.salary = salary;
        }
        public Integer getId() {
                return id;
        }
        public void setId(Integer id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getEmail() {
                return email;
        }
        public void setEmail(String email) {
                this.email = email;
        }
        public java.util.Date getHiredate() {
                return hiredate;
        }
        public void setHiredate(java.util.Date hiredate) {
                this.hiredate = hiredate;
        }
        public Float getSalary() {
                return salary;
        }
        public void setSalary(Float salary) {
                this.salary = salary;
        }
        public String getPwd() {
                return pwd;
        }
        public void setPwd(String pwd) {
                this.pwd = pwd;
        }
        public Integer getGrade() {
                return grade;
        }
        public void setGrade(Integer grade) {
                this.grade = grade;
        }
}
9.2 Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";>
<hibernate-mapping package="com.hsp.domain">
        <class  name="Employee" table="employee">
        <cache usage="read-write"/>
        <!-- 主键策略 -->
        <id name="id" type="java.lang.Integer">
        <generator class="native"/> 
        </id>
        <property name="email" type="java.lang.String">
        <column name="email" length="64" />
        </property>
        <property name="hiredate" type="java.util.Date">
        <column name="hiredate"/>
        </property>
        <property name="name" type="java.lang.String">
        <column name="name" length="64"/>
        </property>
        <property name="salary" type="java.lang.Float">
        <column name="salary"/>
        </property>
        <property name="pwd" type="java.lang.String">
        <column name="pwd" length="32"/>
        </property>
        <property name="grade" type="java.lang.Integer">
        <column name="grade" length="3"/>
        </property>
        <!-- 一个雇员属于一个部门 -->
        <many-to-one name="department" column="department_id" />
        </class>
</hibernate-mapping>
9.3 测试代码:
package com.hsp.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hsp.domain.Department;
import com.hsp.domain.Employee;
import com.hsp.service.interfaces.DepartmentServiceInter;
import com.hsp.service.interfaces.EmployeeServiceInter;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
        Session s=sf.openSession();
        Employee employee=new Employee("韩顺平1","aa@sohu.com","145",1,new java.util.Date(),234.56f);
        Transaction tx = s.beginTransaction();
        s.save(employee);
        tx.commit();        
    }
}
9.4 测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值