Department 实体类及映射文件
package cn.entity;
import java.util.HashSet;
import java.util.Set;
/**
* 部门实体类
* */
public class Department implements java.io.Serializable {
private Integer id;
private Employee employee;//管理者
private String name;
private Set employees = new HashSet(0);
public Department() {
}
public Department(Integer id) {
super();
this.id = id;
}
public Department(String name) {
this.name = name;
}
public Department(Employee employee, String name, Set employees) {
this.employee = employee;
this.name = name;
this.employees = employees;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getEmployees() {
return this.employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
}
<?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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="cn.entity.Department" table="SYS_DEPARTMENT" schema="JBOA">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">seq_department</param>
</generator>
</id>
<many-to-one name="employee" class="cn.entity.Employee"
fetch="select">
<column name="MANAGER_SN" length="50" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="true" />
</property>
<set name="employees" inverse="true">
<key>
<column name="DEPARTMENT_ID" precision="22" scale="0"
not-null="true" />
</key>
<one-to-many class="cn.entity.Employee" />
</set>
</class>
</hibernate-mapping>
Employee 实体类及映射文件
package cn.entity;
/**
* 员工实体类
* */
public class Employee implements java.io.Serializable {
private String sn;
private Position position;
private Department department;
private String password;
private String name;
private String status;
public String toString(){
return "Employee[sn="+sn+",password="+password+",name="+name+",status="
+status+"]";
}
public Employee(String sn,String password,
String name,String status,Position position, Department department) {
this.sn = sn;
this.position = position;
this.department = department;
this.password = password;
this.name = name;
this.status = status;
}
public Employee() {
}
public String getSn() {
return this.sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public Position getPosition() {
return this.position;
}
public void setPosition(Position position) {
this.position = position;
}
public Department getDepartment() {
return this.department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
}
<?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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="cn.entity.Employee" table="SYS_EMPLOYEE" schema="JBOA">
<id name="sn" type="java.lang.String">
<column name="SN" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="position" class="cn.entity.Position"
fetch="select">
<column name="POSITION_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="department" class="cn.entity.Department"
fetch="select">
<column name="DEPARTMENT_ID" precision="22" scale="0"
not-null="true" />
</many-to-one>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="50" not-null="true" />
</property>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="status" type="java.lang.String">
<column name="STATUS" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>
Position 实体类及映射文件
package cn.entity;
/**
* 职位实体类
* */
public class Position implements java.io.Serializable {
private Integer id;
private String nameCn;
private String nameEn;
public Position() {
}
public Position(Integer id) {
super();
this.id = id;
}
public Position(String nameCn, String nameEn) {
this.nameCn = nameCn;
this.nameEn = nameEn;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNameCn() {
return this.nameCn;
}
public void setNameCn(String nameCn) {
this.nameCn = nameCn;
}
public String getNameEn() {
return this.nameEn;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
}
<?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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="cn.entity.Position" table="SYS_POSITION" schema="JBOA">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">seq_position</param>
</generator>
</id>
<property name="nameCn" type="java.lang.String">
<column name="NAME_CN" length="50" not-null="true" />
</property>
<property name="nameEn" type="java.lang.String">
<column name="NAME_EN" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
EmployeeDao 数据访问层接口与实现
package cn.dao;
import java.util.List;
import cn.entity.Employee;
public interface EmployeeDao {
/**
* 查询所有员工
* @return
*/
List<Employee> findAll();
/**
* 添加员工
*/
String saveEmployee(Employee employee);
int deleteEmployee(String id);
List<Employee> findEmployees(String name);
/*
* 查询一共有多少个员工
*/
long findCount();
/*
* 查询一页数据
*/
List<Employee>findPage(int first , int max);
}
package cn.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.EmployeeDao;
import cn.entity.Employee;
@SuppressWarnings("unchecked")
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
public List<Employee> findAll() {
return super.getHibernateTemplate().loadAll(Employee.class);
}
public String saveEmployee(Employee employee) {
return (String) super.getHibernateTemplate().save(employee);
}
public int deleteEmployee(String id) {
return super.getHibernateTemplate().bulkUpdate("delete from Employee e where e.sn=?",id);
}
public List<Employee> findEmployees( final String name) {
//使用Criytera Query
return super.getHibernateTemplate().executeFind(new HibernateCallback<List<Employee>>(){
//内部
public List<Employee>doInHibernate(Session session)throws HibernateException,
SQLException{
Criteria criteria=session.createCriteria(Employee.class);
criteria.add(Restrictions.like("name",name,MatchMode.ANYWHERE));
return criteria.list();
}
});
}
public long findCount() {
return super.getHibernateTemplate().execute(new HibernateCallback<Long>(){
public Long doInHibernate(Session session) throws HibernateException,
SQLException {
return (Long) session.createQuery("select count(*) from Employee").uniqueResult();
}
});
}
public List<Employee> findPage(int first, int max) {
DetachedCriteria criteria =DetachedCriteria.forClass(Employee.class);
criteria.addOrder(Order.desc("sn"));
return getHibernateTemplate().findByCriteria(criteria,first,max);
}
}
applicationContext.xml 配置
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle11" />
<property name="username" value="jboa" />
<property name="password" value="123456" />
</bean>
<!-- 会话工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/entity/Employee.hbm.xml</value>
<value>cn/entity/Department.hbm.xml</value>
<value>cn/entity/Position.hbm.xml</value>
</list>
</property>
</bean>
<!-- 在DAO中注入会话工厂 -->
<bean id="employeeDao" class="cn.dao.impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
TestOA 测试数据
package cn.test;
import java.util.List;
import java.util.Random;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.dao.EmployeeDao;
import cn.entity.Employee;
import cn.entity.Position;
import cn.entity.Department;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestOA {
EmployeeDao employeeDao;
static ApplicationContext ctx;//全局 必须静态的
@BeforeClass
//在类加载之后运行的方法 只运行一次
public static void init(){
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Before
//在每一个测试方法运行之前运行的方法,运行多次
public void setUp(){
employeeDao=(EmployeeDao) ctx.getBean("employeeDao");
}
//@Test
//查找全部员工信息
public void testFindAll(){
List<Employee>employees=employeeDao.findAll();
for(Employee employee:employees){
System.out.println(employee);
}
}
//@Test
//查找所有员工数量
public void testCount(){
long num =employeeDao.findCount();
System.out.println("共有员工"+num+"名");
}
//@Test
//根据条件查询员工信息
public void testFindByName(){
List<Employee> employees=employeeDao.findEmployees("雄");
for(Employee employee:employees){
System.out.println(employee);
}
}
//@Test
//批量添加员工数据
public void testSave(){
//添加 200 条记录
for (int i = 1000; i <= 1200; i++) {
Employee employee=new Employee(i+"ABCD",String.valueOf(new Random()
.nextInt(5000)),"景临境"+i,"1",new Position(11),new Department(21));
String sn=employeeDao.saveEmployee(employee);
System.out.println("编号为:"+sn+employee+"成功添加");
}
}
@Test
//分页显示,显示第三页的数据
public void testPage(){
//每页显示5条 显示第三页
List<Employee> employees=employeeDao.findPage(10, 5);
for(Employee employee:employees){
System.out.println(employee);
}
}
//@Test
//根据 id 删除员工信息
public void testDelete(){
int num=employeeDao.deleteEmployee("1200ABCD");
System.out.println(num+"条员工被删除");
}
@After
public void tearDown(){
employeeDao=null;
}
@AfterClass
public static void destory(){
ctx=null;
}
}
效果图:
log4j.properties 日志配置
#to console#
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
#to file#
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=accp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
#fatal/error/warn/info/debug#
log4j.rootLogger=info, stdout, file