SSH框架查詢方法(struts2 Spring 3.1 Hibernate 4.1)

zpz_326發表於2017-07-17

持久層程式碼

package com.jkxy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jkxy.dao.ICatalogDao;
import com.jkxy.entity.Catalog;

/**
 * @author 
 * @category 鮮花分類持久層實現類
 */
public class CatalogDaoImpl implements ICatalogDao {
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Catalog> findAllCatalogs() {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Catalog");
		List<Catalog> catalogs = query.list();
		session.close();
		return catalogs;
	}
}
業務邏輯層程式碼

package com.jkxy.service.impl;

import java.util.List;

import com.jkxy.dao.ICatalogDao;
import com.jkxy.entity.Catalog;
import com.jkxy.service.ICatalogService;

public class CatalogServiceImpl implements ICatalogService {
	private ICatalogDao catalogdao;
	
	public void setCatalogdao(ICatalogDao catalogdao) {
		this.catalogdao = catalogdao;
	}

	@Override
	public List<Catalog> findAllCatalogs() {
		
		return catalogdao.findAllCatalogs();
	}
}
控制層程式碼

package com.jkxy.action;

import java.util.List;
import java.util.Map;

import com.jkxy.entity.Catalog;
import com.jkxy.service.ICatalogService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 
 * @category 鮮花分類的控制層
 */
public class flowerAction extends ActionSupport {
	private ICatalogService catalogservice;

	public void setCatalogservice(ICatalogService catalogservice) {
		this.catalogservice = catalogservice;
	}
	public String browseCatalog() throws Exception{
		List<Catalog> catalogs= catalogservice.findAllCatalogs();
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("catalogs", catalogs);
		return SUCCESS;
	} 
}
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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd" 
	xmlns:tx="http://www.springframework.org/schema/tx">

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/jkxyflower">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/jkxy/entity/Flower.hbm.xml</value>
				<value>com/jkxy/entity/Userdetail.hbm.xml</value>
				<value>com/jkxy/entity/Catalog.hbm.xml</value>
				<value>com/jkxy/entity/User.hbm.xml</value>
				<value>com/jkxy/entity/Guashi.hbm.xml</value>
				<value>com/jkxy/entity/Orders.hbm.xml</value>
				<value>com/jkxy/entity/Orderitem.hbm.xml</value></list>
		</property>
	</bean>
	<!-- dao層 -->
	<bean id="catalogdao" class="com.jkxy.dao.impl.CatalogDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- service層 -->
	<bean id="catalogservice" class="com.jkxy.service.impl.CatalogServiceImpl">
		<property name="catalogdao" ref="catalogdao"></property>
	</bean>
	<!-- action層 -->
	<bean id="flowerAction" class="com.jkxy.action.flowerAction">
		<property name="catalogservice" ref="catalogservice"></property>
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default"> 
		<action name="browseCatalog" class="flowerAction" method="browseCatalog">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>    


相關文章