注解在spring中的使用

PersonAction类

package com.my.action;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import com.my.bean.Person;
import com.my.service.IPersonService;
import com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="json-default")
public class PersonAction extends  ActionSupport {
    private Person person;
    private List<Person>  list;

    @Resource
    private IPersonService  iPersonService;

    @JSON(serialize=false)
    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Person> getList() {
        return list;
    }

    public void setList(List<Person> list) {
        this.list = list;
    }

    @Action(value="/showPerson",
            results={
            @Result(name=SUCCESS, type="json")
           })
    public String showPerson(){
        list = iPersonService.getAllPerson();
        return SUCCESS;
    }

    @Action(value="add",
            results={
             @Result(location="/show.jsp",type="redirect")
            })
    public String add(){
        iPersonService.add(person);
        return SUCCESS;
    } 

}

实体bean–Person

package com.my.bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_person")
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;
    @OneToMany(mappedBy="person",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private Set<Pet> pets = new HashSet<Pet>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Pet> getPets() {
        return pets;
    }

    public void setPets(Set<Pet> pets) {
        this.pets = pets;
    }
}

实体bean–Pet

package com.my.bean;
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.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_pet")
public class Pet {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;
    @ManyToOne
    @JoinColumn(name="p_id")
    private Person person;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

DAO层–IPersonDao

package com.my.dao;

import java.util.List;

import com.my.bean.Person;

public interface IPersonDao {

    public void add(Person  per);

    public List<Person> getAllPerson();

}

DAOImpl–PersonDaoImpl

package com.my.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.my.bean.Person;
@Repository("personDaoImpl")
public class PersonDaoImpl implements IPersonDao {

    //@Autowired
    @Resource
    private SessionFactory sessionFactory;

    @Transactional(readOnly=false,propagation=Propagation.REQUIRED)
    public void add(Person per ) {

        sessionFactory.getCurrentSession().save(per);

    }

    @Transactional(readOnly=true) 
    public List<Person> getAllPerson() {

        return sessionFactory.getCurrentSession().createQuery("from Person").list();
    }

}

PersonServiceImpl

package com.my.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.my.bean.Person;
import com.my.dao.IPersonDao;
@Service("IPersonService")
public class PersonServiceImpl implements IPersonService {

    @Resource
    private IPersonDao  personDaoImpl;
    public void add(Person per) {

        personDaoImpl.add(per);

    }

    public List<Person> getAllPerson() {
        // TODO Auto-generated method stub
        return personDaoImpl.getAllPerson();
    }

}

spring-DaseBean.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

     <context:property-placeholder location="classpath:db.properties"/>  

     <!-- 配置spring要支持注解 -->
     <context:component-scan base-package="com.my.*"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${driverClass}"/>
        <property name="jdbcUrl" value="${jdbcUrl}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>
         <!--当连接池中的连接用完时,C3P0一次性创建新连接的数目 -->
        <property name="acquireIncrement" value="${acquireIncrement}"/>
        <!--初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值  -->
        <property name="initialPoolSize" value="${initialPoolSize}"/>
        <!--minPoolSize连接池 最少连接数  -->
         <property name="minPoolSize" value="${minPoolSize}"/>
        <!--maxPoolSize连接池 最大连接数  -->
         <property name="maxPoolSize" value="${maxPoolSize}"/>
         <!--maxIdleTime最大空闲时间  -->
         <property name="maxIdleTime" value="${maxIdleTime}"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
     <property name="packagesToScan">
      <list>
       <value>com.my.bean</value>
      </list> 
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        hibernate.show_sql =true
       <!--  hibernate.hbm2ddl.auto=create -->
      </value>
    </property>
  </bean>
   <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
   </bean>

   <!--配置使用注解的方式管理事务-->
    <tx:annotation-driven/>
 </beans>    

struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

  <!--把struts生产action的权利交给spring  -->
  <constant name="struts.objectFactory" value="spring" />
  <constant name="struts.devMode" value="true" />
  <constant name="struts.configuration.xml.reload" value="true" />

</struts>

db.properties数据源配置参数

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///pet
user=root
password=root
acquireIncrement=10
initialPoolSize=10
minPoolSize=3
maxPoolSize=500
maxIdleTime=1800
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值