框架技术--S2SH框架整合(注解 No 1)

2024-05-25 13:32
文章标签 技术 整合 框架 注解 s2sh

本文主要是介绍框架技术--S2SH框架整合(注解 No 1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接着之前的一篇文章“框架技术--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 accessorspublic 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.csdn.net/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;}@Resourcepublic void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public List findUser() throws Exception {// TODO Auto-generated method stubList list = new ArrayList();Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();try {// HqlQuery 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 stubList 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://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>






这篇关于框架技术--S2SH框架整合(注解 No 1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1001645

相关文章

SpringCloud中的@FeignClient注解使用详解

《SpringCloud中的@FeignClient注解使用详解》在SpringCloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解来标记Feign客户端接口,这篇文章... 在Spring Cloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

MQTT SpringBoot整合实战教程

《MQTTSpringBoot整合实战教程》:本文主要介绍MQTTSpringBoot整合实战教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录MQTT-SpringBoot创建简单 SpringBoot 项目导入必须依赖增加MQTT相关配置编写

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

MybatisPlus3.3.1整合clickhouse的过程

《MybatisPlus3.3.1整合clickhouse的过程》:本文主要介绍MybatisPlus3.3.1整合clickhouse的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定... 前言ClickHouse是俄罗斯Yandex发布的一款数据分析型数据库支持sql语法,详情可以访问官网,

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能... 目录@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案1

SpringCloud整合MQ实现消息总线服务方式

《SpringCloud整合MQ实现消息总线服务方式》:本文主要介绍SpringCloud整合MQ实现消息总线服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、背景介绍二、方案实践三、升级版总结一、背景介绍每当修改配置文件内容,如果需要客户端也同步更新,