【JavaEE】SSH全注解
来源:互联网 发布:java明日潘多拉 编辑:程序博客网 时间:2024/06/07 18:45
1.下载必要核心jar包,并引入到项目
2.需要注意的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: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/context http://www.springframework.org/schema/context/spring-context-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"> <!-- 让spring关注注解,并扫描com.lagersoft.*包下的所有类 --> <context:component-scan base-package="com.lagersoft.*"></context:component-scan> <context:annotation-config></context:annotation-config> <!-- 数据源配置项 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"> </property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <!-- SessionFactory配置项 这个bean相当于Hibernate的配置项 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="annotatedClasses"> <list> <value>com.lagersoft.po.Dept</value> <value>com.lagersoft.po.Emp</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 使用声明式事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getAll*" read-only="true" propagation="SUPPORTS" /> <tx:method name="find*" read-only="true" propagation="SUPPORTS" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" /> <tx:method name="del*" read-only="false" propagation="REQUIRED" /> <tx:method name="add*" read-only="false" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 切面的配置项 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lagersoft.service.*.*(..))" /> </aop:config></beans>
3.数据访问层的实现类 使用@org.springframework.stereotype.Repository注解,为了更方便的注入sessionFactory对象可以让所有DAO实现类继承DAO实现类的基类
package com.lagersoft.base;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.hibernate3.HibernateTemplate;/** * DAO层实现类基类:实现sessionFactory的注入 * @author xiaoping * */public class BaseDAOImpl{ private SessionFactory sessionFactory; private HibernateTemplate hibernateTemplate; public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } @Autowired public void setHibernateTemplate(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); }}
4.业务逻辑层的实现类使用 @org.springframework.stereotype.Service注解,
使用 @org.springframework.beans.factory.annotation.Autowired 来注入需要使用到的DAO
@Servicepublic class EmpServiceImpl implements EmpService { @Autowired private EmpDAO empDAO; ...}
5.xxAction类 使用 @org.springframework.stereotype.Controller注解
使用 @org.apache.struts2.convention.annotation.Action来注解具体的需要访问的action方法
使用@org.apache.struts2.convention.annotation.Result注解action返回的结果
@Controller@Scope("prototype")public class EmpAction extends ActionSupport{ //spring注入的使用@Autowired @Autowired EmpService empService; @Autowired DeptService deptService; //这里访问/emp_list 这个Action的时候访问 list() 返回SUCCESS 则跳转到/emp_list.jsp页面 @Action(value= "/emp_list",results={ @Result(name="success",location="/emp_list.jsp") }) public String list(){ list = empService.getAll(); return SUCCESS; } //这里跳转到/emp_list 这个Action @Action(value = "/emp_del",results={ @Result(name = "queryAll",type ="redirectAction", location = "/emp_list") }) public String del(){ empService.del(emp); return "queryAll"; } ...}
6.DEMO例子下载
0 0
- 【JavaEE】SSH全注解
- 全注解SSH
- 全注解SSH
- 全注解SSH
- 全注解SSH
- 全注解SSH
- 全注解SSH
- 全注解整合ssh
- ssh全注解开发
- 零配置,SSH全注解
- SSH全注解代码片段
- SSH全注解开发笔记
- SSH全注解式开发
- ssh全注解框架整合
- 基于全注解的SSH简单框架
- 基于全注解的SSH简单框架
- 基于全注解方式的SSH框架
- eclipse 配置 SSH 无配置文件 全注解
- 这些年,我身边的那些人和事
- Web前端攻击方式及防御措施
- python 栈 ~
- Fragment使用getActivity的Crash问题
- 制作水平导航栏
- 【JavaEE】SSH全注解
- java的lambda表达式、匿名类,Predicate接口,Consumer接口的应用
- leetcode D1 Hamming Distance
- C++之常对象、常成员变量与常成员函数详解
- 媒体查询_REM
- POJ2549【hash分离链接法】
- 乌班图安装步骤
- 基于Android Studio的OpenCV开发环境搭建笔记
- 浅谈jdk的安装与环境变量设置的原理