接着之前的一篇文章“框架技术--S2SH框架整合(使用myeclipse自动生成)”,这里我使用了注解搭建了下,和大家分享下。
目前只将spring、hibernate两层框架使用了注解的方式,struts2暂时还没替换,待后续我替换上,在整理文章与大家分享。
以下仅是这两天使用注解搭建框架的一些方式,以此记录,便于后续使用。
hibernate框架:
1、使用hierbernate框架,其中hibernate.cfg.xml和*.hbm.xml是之前我文章中的用法,hibernate.cfg.xml是hibernate的配置文件,*.hbm.xml是实体Bean中属性和数据库的对应关系配置。
2、使用了注解后,我们就不需要使用*.hbm.xml来配置实体Bean中属性和数据库了。可以通过注解实现这些功能。
代码如下:
package com.gp.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* TUser entity.
*
* @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="t_user")
public class TUser implements java.io.Serializable {
// Constructors
/** default constructor */
public TUser() {
}
/** full constructor */
public TUser(String name, String password) {
this.name = name;
this.password = password;
}
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
hibernate.cfg.xml配置如下:
这里我们将一些数据库连接的属性配置上,另外我们还要告诉hibernate需要映射的实体bean类路径,详见18行。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://192.168.200.27:3306/liveEpg?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping class="com.gp.bean.TUser" />
</session-factory>
</hibernate-configuration>
以上这些就是hibernate的注解配置,到此基本是完成了简单的配置,下面我们来修改spring。
spring框架
1、spring框架核心配置文件applicationContext.xml,之前我们需要进行一些注入的配置。
如图(可详见http://blog.youkuaiyun.com/gaopeng0071/article/details/9895845 ):
在使用了注解我们就不需要进行这么多的配置,下面分别记录下使用注解表示各个层的用法:
dao底层,访问数据库的,在代码中通过@Repository来代替上图xml中103-105行代码。
package com.gp.daoImpl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.gp.dao.UserDao;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List findUser() throws Exception {
// TODO Auto-generated method stub
List list = new ArrayList();
Session session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
try {
// Hql
Query query = session.createQuery("from TUser usg");
list = query.list();
} catch (HibernateException e) {
e.printStackTrace();
}
return list;
}
}
service层,其中@Resource(name="userDao"),如上图xml中的102~105行代码。
package com.gp.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gp.dao.UserDao;
import com.gp.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;
//添加事务处理,指明readOnly的话,Spring会对其有一定的优化
public List findUser() throws Exception {
// TODO Auto-generated method stub
List list = userDao.findUser();
return list;
}
public UserDao getUserDao() {
return userDao;
}
@Resource(name="userDao")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
action层代码如下,@Resource(name="userService")如上图xml中的108~112行代码:
package com.gp.action;
import java.util.List;
import javax.annotation.Resource;
import com.gp.bean.TUser;
import com.gp.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private UserService userService;
private List<TUser> list;
public String findUser() throws Exception {
list = userService.findUser();
return SUCCESS;
}
public UserService getUserService() {
return userService;
}
@Resource(name="userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
public List<TUser> getList() {
return list;
}
public void setList(List<TUser> list) {
this.list = list;
}
}
在上面的service层与dao层分别使用了@Service("userService")和@Service("userService")两种用法,这样使用是根据不同层级不同的职责,使用不同的用法。
详见:http://www.cnblogs.com/chenzhao/archive/2012/02/25/2367978.html
applicationContext.xml源码如下:
这里的事物,我并没有采用注解的形式,扔是使用的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- dataSource注入到sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<context:component-scan base-package="com.gp"></context:component-scan>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.gp.service.*.*(..))"
id="serviceMethod" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>