ssh框架整合详解(jar包+代码+小案例)

1.导入相关jar包(基础jar包42个):

如果需要其他的jar包自行导入即可

在这里插入图片描述

2.书写Struts配置文件和web.xml文件 进行Struts2和spring整合

在这里插入图片描述在这里插入图片描述

3.书写beans.xml文件 hibernate与spring整合的第一种方式(不需要hibernate.cfg.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">
	
	<!-- 开启注解自动代理 -->
	<context:annotation-config></context:annotation-config>
	<!-- 扫描的包 -->
	<context:component-scan base-package="controller,dao,service"></context:component-scan>
	
	
	
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- c3p0配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- hibernate与spring整合的第一种方式(不需要hibernate.cfg.xml) -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	
		<property name="dataSource" ref="dataSource"></property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		
		<property name="mappingLocations">
			<list>
				<value>classpath:pojo/*.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		 <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置事务增强  固定的 -->	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="*" read-only="false"/>
		</tx:attributes>
	</tx:advice>

	<!-- 配置aop -->
	<aop:config>
		<aop:pointcut expression="execution(* service..*(..))" id="poi1"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="poi1"/>
	</aop:config>
	
	
	
	</beans>

4.实体类的配置文件(一对多)

ps:我是单表维护表关系
student.hbm.xml(多方)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="pojo.Student" table="ssh_student">
    		<id name="id" column="id" type="int">
    			<generator class="native"></generator>
    		</id>
    	
    		<property name="name" column="name"></property>
    		
    		
    		<!-- 配置多对一 -->
    		
    		<many-to-one name="teacher" class="pojo.Teacher" column="tid" lazy="false" ></many-to-one>
    		
    	</class>
    
    
    </hibernate-mapping>

teacher.hbm.xml(一方)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="pojo.Teacher"  table="ssh_teacher">
    	
    		<id name="tid" column="tid" type="int">
    			<generator class="native"></generator>
    		</id>
    		
    		<property name="tname" column="tname"></property>
    	</class>
    
    </hibernate-mapping>

5.Action的配置(注解)

package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import pojo.Student;
import pojo.Teacher;
import service.StudentServiceI;
import service.TeacherServiceI;
@Controller
@Scope(value="prototype")
public class StudentAction extends ActionSupport implements ModelDriven<Student> {

	@Autowired
	private StudentServiceI StudentServiceImpl;
	@Autowired
	private TeacherServiceI TeacherServiceImpl;
	
	//获取tid
	public Integer tid;

	public Integer getTid() {
		return tid;
	}

	public void setTid(Integer tid) {
		this.tid = tid;
	}

	//获取Student对象
	private Student student=new Student();
	

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	//模型驱动创建get方法
	public Student getModel() {
		// TODO Auto-generated method stub
		return student;
	}

	//查询列表
	
	public String list() {
		
		System.out.println("测试查询列表。。。。。。");
		List<Student> list = StudentServiceImpl.findStudentList();
		
		//放入值栈
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		valueStack.push(list);
		
		return "list";
	}
	
	//更新前准备(回显  根据id查询的学生信息  老师列表 选中的老师)
	public String toupdate() {
		
		System.out.println("更新前准备--------");
		//根据id 查询学生信息
		Student student2 = StudentServiceImpl.findStudentById(student.getId());
		
		//老师列表
		List<Teacher> teacherList = TeacherServiceImpl.findTeacherList();
		
		//放入值栈
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		valueStack.push(student2);
		valueStack.set("teacherList",teacherList);
		//valueStack.push(teacherList);
		
		return "toupdate";
	}
	
	//更新 (接收传回来的 id name tid)
	public String update() {
		
		
		StudentServiceImpl.update(student, tid);
		
		return "update";
	}
	
	//添加前准备(回显老师列表)
	
	public String toadd() {
		
		System.out.println("添加前准备=====");
		
		//老师列表
		List<Teacher> teacherList = TeacherServiceImpl.findTeacherList();
		
		//把老师列表传入值栈
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		
		valueStack.set("teacherList",teacherList);
		
		return "toadd";	
	}
	
	//添加("获取传来的 name did")
	public String add() {
		
		StudentServiceImpl.add(student, tid);
		
		
		return "add";
	}
	//删除
	public String delete() {
		
		StudentServiceImpl.delete(student.getId());
		
		System.out.println("删除。。。");
		
		return "delete";
	}
	
}

6.项目结构

在这里插入图片描述

数据库查询用的HQL 其他的都是注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值