SSH开发中的注解使用
在SSH中使用注解可以减少配置XML文件,毕竟随着项目规模的扩大,配置bean将把Spring的配置文件(applicationContext.xml)变得很混乱
在Spring的配置文件中开启注解扫描
<context:component-scan base-package="cn.lynu"></context:component-scan>
注意这个base-package就是指定了要扫描的包范围,这里可以指定一个共有的包名以扫描所有类的注解
这里给一个Spring配置文件:
现在只需要配置C3P0和hibernate并开启注解扫描就可以了
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easyui"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <!-- 配置hibernate -->
<!-- 1.先配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean> <!-- 2.开启事务管理器,用注解方式使用事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务,记得在需要开启事务的类上 使用@Transaction-->
<tx:annotation-driven transaction-manager="transactionManager"/> <!-- 开启注解扫描 -->
<context:component-scan base-package="cn.lynu"></context:component-scan> </beans>
开始使用注解
1.先从Dao开始,在Dao类上使用@Repository注解声明自己
然后是很关键的一步,我使用了HibernateDaoSupport,需要在Dao中注入sessionFactory,而Dao中就需要调用HibernateDaoSupport中的sessionFactory
@Resource(name="sessionFactory")
public void setSF(SessionFactory sf){
super.setSessionFactory(sf);
}
setSF这个名字不是固定的,只要是以set打头就行了
2.在Service类上使用注解@Service
然后使用@Resource注入Dao(使用注解注入属性就不需要set方法)
@Resource(name="adminDao")
private AdminDao adminDao;
3.在Action类上面使用@Controller指明自己是控制器,使用@Scope("prototype")指明为多实例,action都是多实例的
同样还要使用@Resource(J2EE规范注解,按照名称进行装配)注入Service(使用注解注入属性就不需要set方法)
@Resource(name="adminService")
private AdminService adminService;
SSH开发中的注解使用的更多相关文章
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- SSH开发中 使用超链接到action 其excute方法会被执行两次 actual row count: 0; expected: 1
由于执行两次excute,所以在做删除操作的时候会出现 Batch update returned unexpected row count from update [0]; actual row c ...
- 廖师兄springboot微信点餐开发中相关注解使用解释
package com.imooc.dataobject;import lombok.Data;import org.hibernate.annotations.DynamicUpdate;impor ...
- 【详细】总结JavaWeb开发中SSH框架开发问题(用心总结,不容错过)
在做JavaWeb的SSH框架开发的时候,遇到过很多的细节问题,这里大概记录下 我使用的IDE是Eclipse(老版本)三大框架:Spring4.Struts2.Hibernate5 1.web.xm ...
- MyBatis 项目开发中是基于 XML 还是注解?
只要你对 MyBatis 有所认识和了解,想必知道 MyBatis 有两种 SQL 语句映射模式,一种是基于注解,一种是基于XML. 基于 XML <mapper namespace=" ...
- 开发中常见的@NotNull,@NotBlank,@NotEmpty注解的区别
开发中常看见@NotNull,@NotBlank,@NotEmpty三个注解,但却没有深入了解过,下面介绍一下他们的应用场景和区别 @NotNull:主要用在基本数据类型上(Int,Integer,D ...
- SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...
- 注解式项目开发!详细解析Java中各个注解的作用和使用方式
@Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...
- SSH开发环境搭建
断断续续学习hibernate也有一段时间了,在这里研究一下SSH开发环境的搭建过程,自己简单的搭建一个SSH的开发环境.采用maven搭建. 0.项目结构: 1.导包:(maven项目) pom.x ...
随机推荐
- CSLA验证规则总结
CSLA业务规则 验证规则所在空间: Csla.Rules 基类 BusinessBase 的属性 BusinessRules 中记录了业务类的验证规则 验证规格的写法 private class ...
- 用I/O口模拟总线时序
在做总线通信过程中,我们很少会用到这样方法,一般在我们选择MCU的时候都会带有你所需要的通信接口.但是,对于一些简单的通信应该用的场合,一般在一些传感器的数据通信过程中,传感器厂商会将通信协议做一些改 ...
- Spring 在xml配置里配置事务
事先准备:配置数据源对象用<bean>实例化各个业务对象. 1.配置事务管理器. <bean id="transactionManager" class=&quo ...
- python基础之网络基础
一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 剑指offer--30.二叉搜索树的后序遍历序列
正常情况下,因为二叉搜索树,左子树所有结点比根小,右子树所有结点比根大,所以循环一遍就能结束 ----------------------------------------------------- ...
- js鼠标拖动(转自刘68)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android 进阶10:进程通信之 Messenger 使用与解析
读完本文你将了解: Messenger 简介 Messenger 的使用 服务端 客户端 运行效果 使用小结 总结 代码地址 Thanks 前面我们介绍了 AIDL 的使用与原理,这篇文章来介绍下 A ...
- js类 的小例子
class Flyer { constructor(fname, speed) { this.fname = fname; this.speed = speed; } fly(){ console.l ...
- what is out of band mode.
Most of the steps are the same, except instead of sending an URL as the oauth_callback to request_to ...