Spring 雇员管理系统SSH 1

【配置Spring并测试】

   

引入Spring包,编写applicationContext.xml文件,该文件放在src目录下

   

建立domain包,编写雇员类Employee.java

   

Employee.java

   

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;

   

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;

}

   

//各属性字段的setget方法

}

   

applicationContext文件中配置bean

   

<bean id="employee" class="com.hsp.domain.Employee(类所在路径,包含包路径,不要后面的java)">

<property name="name" value="tong"/>

</bean>

   

新建测试类Test.java,在Test.java的主函数中测试

   

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

Employee employee=ac.getBean("employee");

   

【配置hibernate

   

hibernate.cfg.xml文件对象映射文件以及sessionFactorySpring的文件中配置即可

   

引入hibernate

   

配置数据源,因为Spring提倡对数据源进行开发

   

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<!-- 这部分与hibernate配置相同-->

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orclhsp"/>

<property name="username" value="scott"/>

<property name="password" value="tiger"/>

<!-- 连接池启动时的初始值 -->

<property name="initialSize" value="3"/>

<!-- 连接池的最大值 -->

<property name="maxActive" value="500"/>

<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->

<property name="maxIdle" value="2"/>

<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->

<property name="minIdle" value="1"/>

</bean>

   

为什么要用数据源这种机制

   

减少频繁的与数据库建立Connection和断开,数据源一开始就建立3个连接,相当于一个连接池,缓冲池,如果需要连接,直接从连接池中取,超过3个之后再与数据库建立连接

   

如果超过最大数,则等待

   

配置sessionFactory

   

<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>

</list>

</property>

<property name="hibernateProperties">

<value>

hibernate.dialect=org.hibernate.dialect.OracleDialect

        hibernate.hbm2ddl.auto=update

hibernate.show_sql=false

hibernate.format_sql=false        

</value>

</property>

</bean>

   

以及Employee.hbm.xml文件

   

<hibernate-mapping package="com.hsp.domain">

<class name="Employee" table="employee">

<!--主键策略-->

<id name="id" type="java.lang.Integer">

<generator class="native"/>

</id>

<property name="email" type="java.lang.String">

<column name="email" length="64"/>

</property>

   

进行测试

   

Test.javamain函数中编写

   

SessionFactory sf=ac.getBean("sessionFactory");

Session s=sf.openSession();

Employee employee=new Employee("子柳","ziliu@taobao.com","145",1,new java.util.Date(),234.56f);

Transaction tx=s.beginTransaction();

s.save(employee);

tx.commit();

   

查询数据库,得到

   

   

   

   

原文地址:https://www.cnblogs.com/keedor/p/4427770.html