这里使用Spring3.0+Hibernate3.3作为例子。
例子中的实体类也是用的Hibernate注解里的实体(上一篇Blog)
一、Spring的一些常用注解
1.@Autowired注解(不推荐使用,建议使用@Resource)
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。
2.@Resource注解
JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序:
a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配
3.@Component注解 (不推荐使用)
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替@Component。
二、Spring3.0的头部文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> </beans>
三、dao和daoImpl
dao:
package org.e276.dao;
import java.util.List;
import org.e276.entity.Employee;
/**
* 接口
*
* @author miao
*
*/
public interface EmployeeDao {
/**
* 根据id查询员工
*
* @param id
* @return
*/
public Employee findEmployeeById(Integer id);
/**
* 删除多个员工
*
* @param ids
* @return
*/
public int deleteEmployees(Integer... ids);
/**
* 查询所有的工资,并按升序排序
*
* @return
*/
public List<Employee> findAllEmployees();
/**
* 查询所有大于平均工资的员工信息
*
* @return
*/
public List<Employee> findEmployeeGtAvgSalary();
}
daoImpl:
package org.e276.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.e276.dao.EmployeeDao;
import org.e276.entity.Employee;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
/**
* 接口实现类
*
* @author miao
*
*/
@Repository("employeeDao")
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
/**
* 注入会话工厂,方法名字在可以随便起,给Spring用来注入
*/
@Autowired
public void injectSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public Employee findEmployeeById(Integer id) {
return super.getHibernateTemplate().get(Employee.class, id);
}
@Override
public int deleteEmployees(Integer... ids) {
int count = 0;
for (Integer id : ids) {
super.getHibernateTemplate().bulkUpdate("delete from Employee where id=?", id);
count++;
}
return count;
}
@SuppressWarnings("unchecked")
@Override
public List<Employee> findAllEmployees() {
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
criteria.addOrder((Order.asc("salary")));
return super.getHibernateTemplate().findByCriteria(criteria);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<Employee> findEmployeeGtAvgSalary() {
return (List<Employee>) super.getHibernateTemplate().execute(new HibernateCallback() {
String sql = "select id, name, sex, salary, birthday, depart_id from employee where salary>(select avg(salary) from employee)";
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql).addEntity(Employee.class);
return query.list();
}
});
}
}
四、biz和bizImpl
biz:
package org.e276.biz;
import java.util.List;
import org.e276.entity.Employee;
import org.springframework.transaction.annotation.Transactional;
/**
* 业务接口
*
* @author miao
*
*/
public interface EmployeeBiz {
/**
* 根据id查询员工
*
* @param id
* @return
*/
public Employee findEmployeeById(Integer id);
/**
* 删除多个员工,支持注解事务
*
* @param ids
* @return
*/
@Transactional
public int deleteEmployees(Integer... ids);
/**
* 查询所有的工资,并按升序排序
* @return
*/
public List<Employee> findAllEmployees();
/**
* 查询所有大于平均工资的员工信息
* @return
*/
public List<Employee> findEmployeeGtAvgSalary();
}
bizImpl:
package org.e276.biz.impl;
import java.util.List;
import org.e276.biz.EmployeeBiz;
import org.e276.dao.EmployeeDao;
import org.e276.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 业务接口实现类
* @author miao
*
*/
@Service("employeeBiz")
public class EmployeeBizImpl implements EmployeeBiz {
//无需set方法
@Autowired
private EmployeeDao employeeDao;
@Override
public Employee findEmployeeById(Integer id) {
return employeeDao.findEmployeeById(id);
}
@Override
public int deleteEmployees(Integer... ids) {
return employeeDao.deleteEmployees(ids);
}
@Override
public List<Employee> findAllEmployees() {
return employeeDao.findAllEmployees();
}
@Override
public List<Employee> findEmployeeGtAvgSalary() {
return employeeDao.findEmployeeGtAvgSalary();
}
}
五、Spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 哪个包里面的类由Spring管理 --> <context:component-scan base-package="org.e276" /> <!-- 表示隐式的向Spring容器注册 --> <context:annotation-config /> <!-- 会话工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="file:src/hibernate.cfg.xml"> </property> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 有关注解式事务的配置 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
六、测试类
package org.e276.test;
import java.util.List;
import org.e276.biz.EmployeeBiz;
import org.e276.entity.Employee;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
*
* @author miao
*
*/
public class SpringEmployee {
static ApplicationContext context;
EmployeeBiz employeeBiz;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
context = null;
}
@Before
public void setUp() throws Exception {
employeeBiz = context.getBean("employeeBiz", EmployeeBiz.class);
}
@After
public void tearDown() throws Exception {
employeeBiz = null;
}
/**
* 根据id查询员工
*/
public void findEmployeeById() {
Employee employee = employeeBiz.findEmployeeById(1000);
System.out.println(employee);
}
/**
* 删除多条记录 删除编号50,100,150的员工
*/
public void deleteEmployees() {
int row = employeeBiz.deleteEmployees(50, 100, 150);
System.out.println("共" + row + "条记录被删除!");
}
/**
* 查询所有的员工,并按工资升序排序
*/
public void findAllEmployees() {
List<Employee> list = employeeBiz.findAllEmployees();
for (Employee employee : list) {
System.out.println(employee);
}
}
/**
* 查询所有大于平均工资的员工信息
*/
@Test
public void findEmployeeGtAvgSalary() {
List<Employee> list = employeeBiz.findEmployeeGtAvgSalary();
for (Employee employee : list) {
System.out.println(employee);
}
}
}
七、demo