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 ...
随机推荐
- 远程调用之RMI、Hessian、Burlap、Httpinvoker、WebService的比较
一.综述 本文比较了RMI.Hessian.Burlap.Httpinvoker.WebService5这种通讯协议的在不同的数据结构和不同数据量时的传输性能. RMI是java语言本身提供的远程通讯 ...
- 【Demo】HTML5获取地理位置
HTML5获取地理位置简单实例 实例1--获取地理位置的经纬度: <!DOCTYPE html> <html> <head> <meta charset=& ...
- hash路由(哈希路由)
1.https://www.cnblogs.com/huanying2015/p/8047376.html (js 哈希路由原理实现) 2.https://www.cnblogs.com/yeer/a ...
- Ant Design
https://ant.design/components/form-cn/ (Ant Design of React 中文网) 1.Ant Design of react (相关资料) htt ...
- 快速掌握Android 虚拟机(AVD)方法
摘自 http://jingyan.baidu.com/article/15622f24707710fdfdbea553.html 参阅:http://jingyan.baidu.com/articl ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle
E. Dasha and Puzzle time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- mybatis分页插件使用注意
之前的项目用的数据库是mysql,在pom.xml引入这一个就能分页了. 后来又开了一个项目,使用的是oracle数据库,我写分页的时候发现不能实现,在网上找到资料说是必须要5.0以上的.我就导了这依 ...
- DTO数据传输对象
如果有多个对象需要传输到页面上需要用DTO传输
- Agilent RF fundamentals (3)- TX and RX
1Create carrier:谐振器,如433.92Mhz LC谐振 (频偏控制) 2Add data to carrier 加载数据 3Amplify to broadcast :放大器,如NPN ...
- php调用API支付接口(转自刘68)
首先访问 https://charging.teegon.com/ 注册账号, 找到开发配置 记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击 S ...