注解在spring中的使用
2016-12-18 19:21
267 查看
PersonAction类
实体bean–Person
实体bean–Pet
DAO层–IPersonDao
DAOImpl–PersonDaoImpl
PersonServiceImpl
spring-DaseBean.xml配置文件
struts.xml配置文件
db.properties数据源配置参数
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; impo dfca rt 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
相关文章推荐
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(一):消除hibernate中<mapping resouce 的xxxx. hbm.xml文件
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- struts2+Spring2.0+Hibernate3使用注解的登录验证
- 使用Spring的注解方式注入Struts所管理的action
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- 使用spring-modules-0.9实现注解缓存
- [注解的力量]Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(二):进一步消除 hibernate.cfg.xml
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- 配置整合DWR3.0和Spring2.5使用annotation注解
- Spring2.5 @Resource注解的使用
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 使用 Spring 2.5 基于注解驱动的 Spring MVC