SSH框架查詢方法(struts2 Spring 3.1 Hibernate 4.1)
持久層程式碼
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>
相關文章
- 【SSH框架】系列之 Spring 整合 Hibernate 框架框架Spring
- 搭建SSH三大框架WEB專案過程(Struts2.3+Hibernate4.3+Spring4.1)框架WebSpring
- Hibernate 框架的查詢方式框架
- SSH框架的多表查詢和增刪查改 (方法一)上框架
- 框架(Spring、Struts2和Hibernate三者)整合框架Spring
- SSH框架之Hibernate的查詢詳解(1)、debug斷點除錯框架斷點除錯
- SSH框架下的分頁查詢框架
- ssh框架中的分頁查詢框架
- SSH 專案過程中遇到的問題和解決方法彙總 struts2 spring hibernateSpring
- SSH_hibernate---六種查詢資料庫方式資料庫
- Struts1、Struts2、Hibernate、Spring框架工作原理介紹Spring框架
- ssh框架搭建Struts2 06+spring2 5+hibernate3 2整合例項程式碼教程步驟框架Spring
- SSH:查詢
- 【SSH框架】之Hibernate系列(一)框架
- Hibernate 查詢
- Struts2,Hibernate,Spring 環境配置Spring
- 使用Spring 3.1和Hibernate做持久層Spring
- 在SSH框架下按條件分頁查詢框架
- 【Hibernate框架開發之八】Hibernate 查詢語言Query Language(HQL)框架
- Hibernate查詢方式
- Hibernate——Query查詢
- 深入淺出的理解框架(Struts2、Hibernate、Spring)與 MVC 設計模式框架SpringMVC設計模式
- ssh 分頁查詢
- 391、Java框架46 -【Hibernate - 查詢HQL、查詢Criteria、查詢標準SQL】 2020.10.19Java框架SQL
- hibernate中hql查詢
- Hibernate hql 多表查詢
- Hibernate 查詢語句
- Spring JPA 定義查詢方法Spring
- ssh查詢及其安裝
- Hibernate查詢自動更新
- Hibernate連線查詢join
- hibernate 動態查詢(DetachedCriteria )
- hibernate批量查詢問題
- hibernate(七) hibernate中查詢方式詳解
- Hibernate通常是三種:hql查詢,QBC查詢和QBE查詢:
- [問題]利用spring & hibernate ,每次查詢,都會初始化Spring
- 多條件查詢---ssh版本
- SSH:hiberate實現資料的查詢(單查詢和全查詢)