使用ssh注解查询

今天学习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")  //  自己表的id
	private 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; @Service public class GoodsServiceImpl implements IGoodsService { //注入 @Resource private 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;

@Repository
public class GoodsDaoImpl implements IGoodsDao{

	@Resource
	private 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")
@Controller

public class GoodsAction extends ActionSupport{

	@Resource
	private 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!!!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值