使用注解实现ssh整合
1.搭建項目:
????項目名稱:ss1
2.添加jar包
????1).struts 2.3.7
?????????--基礎
?? ???? asm-3.3.jar
????????asm-commons-3.3.jar
?? ???? asm-tree-3.3.jar
?? ???? commons-fileupload-1.2.2.jar
?? ???? commons-io-2.0.1.jar
?? ???? commons-lang3-3.1.jar
?? ???? freemarker-2.3.19.jar
?? ???? javassist-3.11.0.GA.jar
?? ???? ognl-3.0.5.jar
?? ???? struts2-core-2.3.7.jar
?? ???? xwork-core-2.3.7.jar
?? ???? --整合spring
????????struts2-spring-plugin-2.3.7.jar
?? ???? --使用注解
?? ???? struts2-convention-plugin-2.3.7.jar
??? 2).spring 3.2
????? ? --AOP
????????com.springsource.org.aopalliance-1.0.0.jar
????????com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
????????spring-aop-3.2.0.RELEASE.jar
????????spring-aspects-3.2.0.RELEASE.jar
????????--日志
????????commons-logging.jar
????????log4j.jar
????????--核心
????????spring-beans-3.2.0.RELEASE.jar
????????spring-context-3.2.0.RELEASE.jar
????????spring-core-3.2.0.RELEASE.jar
????????spring-expression-3.2.0.RELEASE.jar
????????--持久層
????????spring-jdbc-3.2.0.RELEASE.jar
????????spring-orm-3.2.0.RELEASE.jar
????????spring-tx-3.2.0.RELEASE.jar
????????--整合junit4
????????spring-test-3.2.0.RELEASE.jar
????????--使用監聽
????????spring-web-3.2.0.RELEASE.jar
??? 3).hibernate 3.6.10
????????--基礎
??????? \lib\hibernate3.jar
????????\lib\required\*.jar
????????--整合log4j
????????slf4j-log4j12-1.6.1.jar
????????--數據庫驅動
?????????mysql-connector-java-5.1.10-bin.jar
????????--二級緩存
????????ehcache-1.5.0.jar
????????backport-util-concurrent.jar
3.添加配置文件
?? ??? ???? a.struts.xml
?? ??? ??? ??? ?<?xml version="1.0" encoding="UTF-8" ?>
?? ??? ??? ??? ?<!DOCTYPE struts PUBLIC
?? ??? ??? ??? ???? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
?? ??? ??? ??? ???? "http://struts.apache.org/dtds/struts-2.0.dtd">
?? ??? ??? ??? ?<struts>
?? ??? ??? ??? ??? ?<!-- 開發模式 -->
?? ??? ??? ??? ??? ?<constant name="struts.devMode" value="true"/>
?? ??? ??? ??? ??? ?<!-- 整合spring -->
?? ??? ??? ??? ??? ?<constant name="struts.objectFactory" value="spring"></constant>
?? ??? ??? ??? ?</struts>
?? ??? ??? ?b.applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
?? ??? ??? ??? ?</beans>
?? ??? ??? ?c.過濾器配置
?? ??? ??? ??? ?修改web.xml文件添加struts核心過濾器
?? ??? ??? ??? ?<!-- struts核心控制器配置 -->
?? ??? ??? ??? ?<filter>
?? ??? ??? ??? ??? ?<filter-name>struts</filter-name>
?? ??? ??? ??? ??? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
?? ??? ??? ??? ?</filter>
?? ??? ??? ??? ?<filter-mapping>
?? ??? ??? ??? ??? ?<filter-name>struts</filter-name>
?? ??? ??? ??? ??? ?<url-pattern>/*</url-pattern>
?? ??? ??? ??? ?</filter-mapping>
?? ??? ??? ??? ?修改web.xml文件添加Session生命周期開發到web層
?? ??? ??? ??? ?<filter>
?? ??? ??? ??? ??? ?<filter-name>openSessionFilter</filter-name>
?? ??? ??? ??? ??? ?<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
?? ??? ??? ??? ?</filter>
?? ??? ??? ??? ?<filter-mapping>
?? ??? ??? ??? ??? ?<filter-name>openSessionFilter</filter-name>
?? ??? ??? ??? ??? ?<url-pattern>/*</url-pattern>
?? ??? ??? ??? ?</filter-mapping>
?? ??? ??? ?d.修改web.xml文件添加spring監聽器
?? ??? ??? ??? ?<!-- spring 監聽配置 -->
?? ??? ??? ??? ?<listener>
?? ??? ??? ??? ??? ?<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
?? ??? ??? ??? ?</listener>
?? ??? ??? ??? ?<context-param>
?? ??? ??? ??? ??? ?<param-name>contextConfigLocation</param-name>
?? ??? ??? ??? ??? ?<param-value>classpath:applicationContext.xml</param-value>
?? ??? ??? ??? ?</context-param>
?? ??? ??? ?e.實體類的創建
?? ??? ??? ??? ?@Entity
?? ??? ??? ??? ?@Table(name ="d_book")
?? ??? ??? ??? ?public class Book implements Serializable {
?? ??? ??? ??? ??? ?private static final long serialVersionUID = 1L;
?? ??? ??? ??? ??? ?@Id
?? ??? ??? ??? ??? ?@GeneratedValue(strategy=GenerationType.AUTO)
?? ??? ??? ??? ??? ?private Integer id;
?? ??? ??? ??? ??? ?private String name;
?? ??? ??? ??? ??? ?private Double price;
?? ??? ??? ??? ??? ?private String author;
?? ??? ??? ??? ??? ?//省略getter and setter
?? ??? ??? ??? ?}
?? ??? ??? ?f.在spring核心配置文件applicationContext.xml中添加Hibernate配置信息
?? ??? ??? ??? ?<!-- 1.加載屬性文件 -->
?? ??? ??? ??? ?<context:property-placeholder location="classpath:jdbc.properties"/>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 2.自動描述注解 -->
?? ??? ??? ??? ?<context:annotation-config/>
?? ??? ??? ??? ?<context:component-scan base-package="cn.jbit"></context:component-scan>
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 3.配置數據庫連接池 -->
?? ??? ??? ??? ?<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
?? ??? ??? ??? ??? ?<property name="driverClass" value="${jdbc.driver}"></property>
?? ??? ??? ??? ??? ?<property name="jdbcUrl" value="${jdbc.url}"></property>
?? ??? ??? ??? ??? ?<property name="user" value="${jdbc.user}"></property>
?? ??? ??? ??? ??? ?<property name="password" value="${jdbc.password}"></property>
?? ??? ??? ??? ?</bean>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 4.配置SessionFactory -->
?? ??? ??? ??? ?<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
?? ??? ??? ??? ??? ?<!-- 加載連接池 -->
?? ??? ??? ??? ??? ?<property name="dataSource" ref="dataSource"></property>
?? ??? ??? ??? ??? ?<!-- hibernate屬性 -->
?? ??? ??? ??? ??? ?<property name="hibernateProperties">
?? ??? ??? ??? ??? ??? ?<props>
?? ??? ??? ??? ??? ??? ??? ?<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
?? ??? ??? ??? ??? ??? ??? ?<prop key="hibernate.show_sql">true</prop>
?? ??? ??? ??? ??? ??? ??? ?<prop key="hibernate.format_sql">true</prop>
?? ??? ??? ??? ??? ??? ??? ?<prop key="hibernate.current_session_context_class">thread</prop>
?? ??? ??? ??? ??? ??? ?</props>
?? ??? ??? ??? ??? ?</property>
?? ??? ??? ??? ??? ?<!-- 掃描注解po類所在的包 -->
?? ??? ??? ??? ??? ?<property name="packagesToScan">
?? ??? ??? ??? ??? ??? ?<list>
?? ??? ??? ??? ??? ??? ??? ?<value>cn.jbit.spring9.domain</value>
?? ??? ??? ??? ??? ??? ?</list>
?? ??? ??? ??? ??? ?</property>
?? ??? ??? ??? ?</bean>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 5.配置Hibernater模板 -->
?? ??? ??? ??? ?<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
?? ??? ??? ??? ??? ?<property name="sessionFactory" ref="sessionFactory"></property>
?? ??? ??? ??? ?</bean>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 6.聲明事務管理器 -->
?? ??? ??? ??? ?<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
?? ??? ??? ??? ??? ?<property name="sessionFactory" ref="sessionFactory"></property>
?? ??? ??? ??? ?</bean>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<!-- 7.事務驅動 -->
?? ??? ??? ??? ?<tx:annotation-driven transaction-manager="transactionManager"/>
4.持久層
?? ??? ?1).接口
?? ??? ??? ?public interface IBookDao {
?? ??? ??? ??? ?public void insert(Book book);
?? ??? ??? ??? ?public Book select(int id);
?? ??? ??? ?}
?? ??? ?2).實現
?? ??? ??? ?@Repository
?? ??? ??? ?public class BookDaoImpl implements IBookDao {
?? ??? ??? ??? ?@Autowired
?? ??? ??? ??? ?private HibernateTemplate hibernateTemplate;
?? ??? ??? ??? ?public void insert(Book book) {
?? ??? ??? ??? ??? ?System.out.println("dao save");
?? ??? ??? ??? ??? ?this.hibernateTemplate.save(book);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?public Book select(int id) {
?? ??? ??? ??? ?return this.hibernateTemplate.load(Book.class, id);
?? ??? ??? ?}
5.業務層
?? ??? ?1).接口
?? ??? ??? ?public interface IBookService {
?? ??? ??? ??? ?public void save(Book book);
?? ??? ??? ??? ?public Book find(int id);
?? ??? ??? ?}
?? ??? ?2).實現
?? ??? ??? ?@Service
?? ??? ??? ?@Transactional
?? ??? ??? ?public class BookServiceImpl implements IBookService {
?? ??? ??? ??? ?@Autowired
?? ??? ??? ??? ?private IBookDao bookDao;
?? ??? ??? ??? ?public void save(Book book) {
?? ??? ??? ??? ??? ?bookDao.insert(book);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?public Book find(int id) {
?? ??? ??? ??? ??? ?return bookDao.select(id);
?? ??? ??? ??? ?}
?? ??? ??? ?}
6.控制器
?? ??? ?1).創建BookAction類繼承ActionSupport
?? ??? ??? ?@Namespace("/")
?? ??? ??? ?@Controller
?? ??? ??? ?public class BookAction extends ActionSupport implements ModelDriven<Book>{
?? ??? ??? ??? ?private Book book = new Book();
?? ??? ??? ??? ?//業務邏輯層
?? ??? ??? ??? ?private IBookService bookService;
?? ??? ??? ??? ?@Action(value="bookAction_add",results={@Result(name="add",location="/index.jsp")})
?? ??? ??? ??? ?public String add(){
?? ??? ??? ??? ??? ?System.out.println("添加信息");
?? ??? ??? ??? ??? ?//bookService.save(book);
?? ??? ??? ??? ??? ?return "add";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?public Book getModel() {
?? ??? ??? ??? ??? ?return book;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?public void setBookService(IBookService bookService) {
?? ??? ??? ??? ??? ?this.bookService = bookService;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?@Action(value="bookAction_load",results={@Result(name="load",location="/index.jsp")})
?? ??? ??? ??? ?public String load(){
?? ??? ??? ??? ??? ?book = bookService.find(1);
?? ??? ??? ??? ??? ?System.out.println(book);
?? ??? ??? ??? ??? ?return "load";
?? ??? ??? ??? ?}
?? ??? ??? ?}
7.表示層
????以表單方式提交數據,此處省略!
本文轉自 ?素顏豬 ?51CTO博客,原文鏈接:http://blog.51cto.com/suyanzhu/1437411
總結
以上是生活随笔為你收集整理的使用注解实现ssh整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux常用命令和选项
- 下一篇: 编程修养(四)