spring框架。SSH整合半xml半注解配置

注解的配置版本(一半注解一半xml文件)   
            

applicationContext:
                        dao
                        service
                        action

                        事务配置

都用注解来实现
            struts2:
                 1 导包:struts2-convention-plugin-2.3.24.jar

                    @Scope()
                    @ParentPackage()
                    @Namespace()
                    @Action()

                    
                    需要注意:struts2-convention-plugin-2.3.24.jar这个包下面配置的扫描注解只去 action,actions,struts,struts2
                            <init-param>
                                    <param-name>struts.convention.package.locators</param-name>
                                    <param-value>web</param-value>
                              </init-param>
            
            xxx.hbm.xml
                    在持久化类上用注解方式
                    在sessionFactory 不在加载配置文件了 而是扫描包下的注解
                    <property name="packagesToScan">
                            <list>
                                <value>com.it</value>
                            </list>
                    </property>

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/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">
	<!-- Spring整合hibernate -->
	<!-- 配置c3p0 -->
	<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value="123"></property>
	</bean>
	<!-- 引入Hibernate的配置的信息=============== -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
			 <property name="dataSource" ref="c3p0"></property>
			 <property name="hibernateProperties">
			  	<props>
			  		<prop key="hibernate.show_sql">true</prop>
			  		<prop key="hibernate.format_sql">true</prop>
			  		<prop key="hibernate.hbm2ddl.auto">update</prop>
			  		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			  	</props>
		  	</property>
		  	<!--name属性 ,如果是使用xml配置是:mappingResources 
		  					       使用注解的配置是:packagesToScan
		  	 -->
		  	<property name="packagesToScan">
		   		<list>
		   			<!--使用注解写的持久化类的包名-->
		   			<value>com.it.bean</value>
		   		</list>
		   </property>
	</bean>
	
	<!-- 配置扫描 -->
	<context:component-scan base-package="com.it"></context:component-scan>
	
	
	<!-- 配置AOP事务管理 -->
	<!-- 切面对象 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 
				切面对象从配置的sessionFactory中获取session
				将获取的该session开启事务
				并且将开启了事务的该session绑定到了当前线程中
		 -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 开启事务的注解方式 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

CustomerAction.java

package com.it.action;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.it.bean.Customer;
import com.it.service.CustomerService;
import com.it.serviceimpl.CustomerServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller("customerAction")//一般用于表现层对象(Action)
@Scope("prototype")//默认是单例模式,即scope="singleton"。scope="prototype"多例
@ParentPackage(value="struts-default")//对应xml配置文件中的package的父包,一般需要继承struts-default
@Namespace(value="/")//对应配置文件中的nameSpace,命名空间
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	// 模型封装
	private Customer custmoer = new Customer();

	@Override
	public Customer getModel() {
		// TODO Auto-generated method stub
		return custmoer;
	}

	// 注入CustomerService:
	@Autowired
	private CustomerService customerService;
	
	// 保存客户的方法
	public String save() {
		System.out.println("action执行了");
		System.out.println(custmoer);
		customerService.save(custmoer);
		return null;
	}

}

CustomerServiceImpl.java

package com.it.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.it.bean.Customer;
import com.it.dao.CustomerDao;
import com.it.daoimpl.CustomerDaoImpl;
import com.it.service.CustomerService;
@Service("customerService")//一般用于业务层对象(service)
public class CustomerServiceImpl implements CustomerService{
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	@Transactional
	public void save(Customer custmoer) {
		// TODO Auto-generated method stub
		System.out.println("service save");
		customerDao.save(custmoer);
	}
	
}

CustomerDaoImpl.java

package com.it.daoimpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.it.bean.Customer;
import com.it.dao.CustomerDao;
//使用注解注入dao
@Repository("customerDao")//一般用于持久对象(dao)
public class CustomerDaoImpl  implements CustomerDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;
	

	@Override
	public void save(Customer custmoer) {
		// TODO Auto-generated method stub
		System.out.println("dao save");
		this.hibernateTemplate.save(custmoer);
	}

}

javabean,,,Customer.java

package com.it.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//hibernate持久化类
@Entity
//表名
@Table(name="cst_customer")
public class Customer {
	//设置id
	@Id
	//这个属性在表中的名称
	@Column(name="cust_id")
	//设置自增
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int cust_id;// '客户编号(主键)',
	@Column(name="cust_name")
	private String cust_name;//'客户名称(公司名称)',
	@Column(name="cust_source")
	private String cust_source;// '客户信息来源',
	@Column(name="cust_industry")
	private String cust_industry;// '客户所属行业',
	@Column(name="cust_level")
	private String cust_level;// '客户级别',
	@Column(name="cust_phone")
	private String cust_phone;//'固定电话',
	@Column(name="cust_mobile")
	private String cust_mobile;//'移动电话',
	
	public int getCust_id() {
		return cust_id;
	}

	public void setCust_id(int cust_id) {
		this.cust_id = cust_id;
	}

	public String getCust_name() {
		return cust_name;
	}

	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}

	public String getCust_source() {
		return cust_source;
	}

	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}

	public String getCust_industry() {
		return cust_industry;
	}

	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}

	public String getCust_level() {
		return cust_level;
	}

	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}

	public String getCust_phone() {
		return cust_phone;
	}

	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}

	public String getCust_mobile() {
		return cust_mobile;
	}

	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}

	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + "]";
	}
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值