ssh注解整合

CustomerDao.java

package com.cherry.ssh.dao;

import com.cherry.ssh.domain.Customer;

/**
 * 客户管理DAO的接口
 * @author zhang
 *
 */
public interface CustomerDao {

	void save(Customer customer);

}

CustomerDaoImpl.java

package com.cherry.ssh.dao.impl;

import javax.annotation.Resource;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.cherry.ssh.dao.CustomerDao;
import com.cherry.ssh.domain.Customer;
/**
 * 客户管理DAO的实现类
 * @author zhang
 * <bean id="customerDao" class="com.cherry.ssh.dao.impl.CustomerDaoImpl">
 * 
 * </bean>
 */
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	@Override
	public void save(Customer customer) {
		System.out.println("CustomerDaoImpl中的save方法执行了...");
		this.hibernateTemplate.save(customer);
	}

}

CustomerService.java

package com.cherry.ssh.service;

import com.cherry.ssh.domain.Customer;

/**
 * 客户管理的Service的接口
 * @author zhang
 *
 */
public interface CustomerService {

	void save(Customer customer);

}

CustomerServiceImpl.java

package com.cherry.ssh.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cherry.ssh.dao.CustomerDao;
import com.cherry.ssh.domain.Customer;
import com.cherry.ssh.service.CustomerService;
/**
 * 客户管理的Service的实现类
 * @author zhang
 *<bean id="customerService" class="com.cherry.ssh.service.impl.CustomerServiceImpl">
 *
 *</bean>
 */
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

	//注入DAO:
	@Resource(name="customerDao")
	private CustomerDao customerDao;
	
	@Override
	public void save(Customer customer) {
		System.out.println("CustomerServiceImpl的save方法执行了...");
		customerDao.save(customer);
	}

}

CustomerAction.java

package com.cherry.ssh.web.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cherry.ssh.domain.Customer;
import com.cherry.ssh.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 
 * @author zhang
 *  **将Action交给Spring管理
 *  <bean id="customerAction" class="类的全路径" scope="prototype">
 *  </bean>
 *  * 配置Action的访问
 *  <package name="demo" extends="struts-default" namespace="/">
 *  	<action name="customer_*" class="customerAction" method="{1}">
 *  	</action>
 *  </package>
 */
@Controller("customerAction")
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	//模型驱动使用的对象
	private Customer customer =new Customer();
	@Override
	public Customer getModel() {
		return customer;
	}
	
	//注入Service
	@Resource(name="customerService")
	private CustomerService customerService;
	
	//保存客户的方法:save
	@Action(value="customer_save",results= {@Result(name="success",location="/login.jsp")})
	public String save() {
		System.out.println("CustomerAction中的save方法执行了...");
		customerService.save(customer);
		return SUCCESS;
	}
}

applicationContext.xml(src下)

<?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.xsd 
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"> 

	<!-- 引入外部属性文件=============================== -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置C3P0连接池=============================== -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 开启组件扫描(将类交给Spring管理)===== -->
	<context:component-scan base-package="com.cherry.ssh"/>
	
	<!-- Spring整合Hibernate========= -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		
		<!-- 配置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.hbm2ddl.auto">update</prop>
			</props>
		</property>
		
		<!-- 加载映射 -->
		<property name="packagesToScan" value="com.cherry.ssh.domain"/>
	</bean>
	
	<!-- 定义Hibernate模板 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

jdbc.properties(src下)

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssh
jdbc.username=root
jdbc.password=password

log4j.properties(src下--日志记录)

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssh3</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置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>
  
  <!-- Struts2的核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

需要的jar包

一共51个

纯注解开发方便,觉得维护不便

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值