使用ssh注解查询
来源:互联网 发布:网络歌手阿刚歌曲 编辑:程序博客网 时间:2024/04/30 04:54
今天学习ssh注解,发现用注解比配置bean要简单得多。在此想记录下来!
首先这是我的代码层次图:
1.首先导入必要的包,我这里大概引用51个包
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
3.配置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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config></context:annotation-config><!-- 扫包 --><context:component-scan base-package="com.wow.*"></context:component-scan><!-- 数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"><value>com.mysql.jdbc.Driver</value></property><property name="jdbcUrl"><value>jdbc:mysql://127.0.0.1:3306/user</value></property><property name="user"><value>root</value></property><property name="password"><value>root</value></property></bean><!-- sessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop></props></property><property name="packagesToScan"><list><value>com.wow.entity</value></list></property></bean><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean></beans>
4.在struts.xml中把对象交给spring管理
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 将action的对象实例化交给 spring管理 --><constant name="struts.objectFactory" value="spring"></constant> </struts>
5.在GoodsEntity中
package com.wow.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name="goods")public class GoodsEntity {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private int goodsId;@Column(name="goodsName")private String goodsName;@OneToOne@JoinColumn(name="goodsId") // 自己表的idprivate OrderEntity orderEntity;public OrderEntity getOrderEntity() {return orderEntity;}public void setOrderEntity(OrderEntity orderEntity) {this.orderEntity = orderEntity;}public int getGoodsId() {return goodsId;}public void setGoodsId(int goodsId) {this.goodsId = goodsId;}public String getGoodsName() {return goodsName;}public void setGoodsName(String goodsName) {this.goodsName = goodsName;}}
在OrderEntity中:
package com.wow.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name="orders")public class OrderEntity {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private int oid;@Column(name="oname")private String oname;@OneToOne(mappedBy="orderEntity") //在对方表里面的自己(的属性名)private GoodsEntity goodsEntity;public int getOid() {return oid;}public void setOid(int oid) {this.oid = oid;}public String getOname() {return oname;}public void setOname(String oname) {this.oname = oname;}public GoodsEntity getGoodsEntity() {return goodsEntity;}public void setGoodsEntity(GoodsEntity goodsEntity) {this.goodsEntity = goodsEntity;}}在IGoodsService中:
package com.wow.service;import java.util.List;import com.wow.entity.GoodsEntity;public interface IGoodsService {/** * 查询商品列表 * @return */public List<GoodsEntity> list();}
在GoodsServiceImpl中:
package com.wow.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.wow.dao.IGoodsDao;import com.wow.entity.GoodsEntity;import com.wow.service.IGoodsService;@Servicepublic class GoodsServiceImpl implements IGoodsService {//注入@Resourceprivate IGoodsDao goodsDaoImpl;public void setGoodsDaoImpl(IGoodsDao goodsDaoImpl) {this.goodsDaoImpl = goodsDaoImpl;}public List<GoodsEntity> list() {return goodsDaoImpl.list();}}
对应的dao,在IGoodsDao中package com.wow.dao;import java.util.List;import com.wow.entity.GoodsEntity;public interface IGoodsDao {/** * 查询商品列表 * @return */public List<GoodsEntity> list();}
在GoodsDaoImpl中:package com.wow.dao.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;import com.wow.dao.IGoodsDao;import com.wow.entity.GoodsEntity;@Repositorypublic class GoodsDaoImpl implements IGoodsDao{@Resourceprivate SessionFactory sessionFactory;public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}/** * 查询了goods 列表 */public List<GoodsEntity> list() {Session openSession = sessionFactory.openSession();Query query = openSession.createQuery("from GoodsEntity");return query.list();}}
最后在action中执行:package com.wow.action;import java.util.List;import javax.annotation.Resource;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.Namespaces;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionSupport;import com.wow.entity.GoodsEntity;import com.wow.service.IGoodsService;@Namespace("/")@ParentPackage("struts-default")@Controllerpublic class GoodsAction extends ActionSupport{@Resourceprivate IGoodsService goodsServiceImpl;public void setGoodsServiceImpl(IGoodsService goodsServiceImpl) {this.goodsServiceImpl = goodsServiceImpl;}/** * 查询商品列表 * @return */@Action(value="list",results={@Result(location="/success.jsp",name="success"),@Result(location="/fail.jsp",name="fail")})public String goodsList(){List<GoodsEntity> list = goodsServiceImpl.list();System.out.println(list);return "success";}}
最后给自己说声 fighting!!!1 0
- 使用ssh注解查询
- ssh使用注解自动装配
- 使用注解简化SSH框架
- ssh项目使用注解搭建
- 使用注解写SSH整合
- SSH在实体类中使用注解实现级联排序查询
- 玩转SSH之Spring(一)---关于Spring框架中使用@query注解实现复杂查询
- Maven使用注解联合查询
- 【SSH】使用HQL查询数据
- 简化ssh框架(使用注解)
- 关于Hibernate注解的使用(命名查询 本地查询)
- Hibernate注解的使用(命名查询 本地查询)
- SSH下使用Spring注解自动注入bean
- SSH下使用Spring注解自动注入bean
- ssh使用Java注解的方式声明架构
- SSH中使用jpa注解方式配置实体类
- Spring Annotation(注解)的使用步骤(SSH)
- SSH-使用注解整合实现简单的登录功能
- [BZOJ 1566][NOI2009]管道取珠(DP)
- WPF
- Arduino float/double 转 char
- NDIS笔记---(1)
- 科锐课堂笔记:2017/3/7 数组续篇
- 使用ssh注解查询
- Linux分区、格式化、自动挂载磁盘
- CSS-2
- android 抓log总结
- 169. Majority Element LeetCode
- 欢迎使用CSDN-markdown编辑器
- HTML`CSS_页面居中小技巧
- 页面加载完成后自动执行(js代码)
- XP清除“我最近的文件列表“与”使用的程序记录“