<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" scope="singleton">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
<!-- 以下为可选参数
<property name="initialSize">
<value>10</value>
</property>-->
<property name="minIdle">
<value>10</value>
</property>
<property name="maxActive">
<value>15</value>
</property>
<property name="maxIdle">
<value>15</value>
</property>
<property name="validationQuery">
<value>select count(*) from dual</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
<property name="testOnReturn">
<value>true</value>
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<!--大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证 -->
<property name="minEvictableIdleTimeMillis">
<value>60000</value>
</property>
<!--失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程 -->
<property name="timeBetweenEvictionRunsMillis">
<value>300000</value>
</property>
</bean>
<context:annotation-config />
<!-- 配置实体管理对象 p:persistenceUnitManager-ref="persistenceUnitManager"-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<!-- 选配属性 -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
</bean>
</property>
</bean>
<!--可屏蔽
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"
p:default-data-source-ref="dataSource"
p:persistence-xml-locations="classpath*:META-INF/persistence.xml">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean> -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" lazy-init="true">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception" />
<!--<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />-->
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="businessService" expression="execution(* service.*.*(..))" />
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="businessService" />
</aop:config>
<bean id="orderDao" class="dao.OrderDao">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="orderService" class="service.OrderService">
<property name="orderDao" ref="orderDao"/>
</bean>
<context:load-time-weaver />
<context:annotation-config />
</beans>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="openjpa.InverseManager" value="false"/>
<property name="openjpa.Log" value="log4j"/>
<property name="openjpa.DataCache" value="false"/>
<property name="openjpa.QueryCache" value="false"/>
<property name="openjpa.Compatibility" value="QuotedNumbersInQueries=true"/>
</properties>
</persistence-unit>
<!--<persistence-unit name="MyJPA" transaction-type="RESOURCE_LOCAL">
<!– 描述信息.(可选) –>
<description>MySQL5.1数据库的映射文件</description>
<!–
要注意的是我们使用的JPA事务采用RESOURCE_LOCAL(本地资源),
不能够使用JTA(全局资源,容器管理事务)。
而且使用本地资源的话,
只能够在web容器部署,而不能够部署到EJB容器里面(Glassfish等),
在GlassfishV2里面部署就提示只可以使用JTA事务
–>
<!– 定义jpa的Provider –>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>entity.Animal</class>
<!– J2EE中,不建议在此配置,建议在Spring中的entityManagerFactory处配制
<properties>
<property name="openjpa.Log" value="none" />
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true,PrettyPrintLineLength=72" />
</properties>
–>
<exclude-unlisted-classes/>
</persistence-unit>-->
</persistence>
Order.java
/** * one端 * * 碰到many为末端的加载就是延迟加载,若one为末端则为立即加载,除了one-to-one。 * * @author jiqinlin * */
@SuppressWarnings({"ALL"})
@Entity(name="tb_order")
public class Order implements java.io.Serializable {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;
/* one端
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE})
mappedBy="order": 指明Order类为双向关系维护端,负责外键的更新
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order")
private List<OrderItem> orderItemList;
OrderItem.java
/** * many端 * *
在one-to-many双向关联中,多的一方为关系维护端,关系维护端负责外键记录的更新 *
关系被维护端是没有权力更新外键记录的 * *
* */
@SuppressWarnings({"ALL"})
@Entity(name="tb_orderitem")
public class OrderItem implements java.io.Serializable {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
//@Column(name="order_id")
// private int orderId;
@Column(name="name")
private String name;
// optional=true:可选,表示此对象可以没有,可以为null;false表示必须存在
@ManyToOne(cascade = { CascadeType.REFRESH,CascadeType.PERSIST , CascadeType.MERGE }, optional = true)
@JoinColumn(name = "order_id")
private Order order;
Dao.java
public class OrderDao extends JpaDaoSupport {
public void addOrder(final Object order){
getJpaTemplate().persist(order);
/* getJpaTemplate().execute(new JpaCallback() {
public Object doInJpa(EntityManager entityManager) throws PersistenceException {
entityManager.getTransaction().begin();
entityManager.persist(order);
entityManager.getTransaction().commit();
entityManager.close();
return null;
}
});*/
}
public void updateOrder(final Object order){
getJpaTemplate().merge(order);
}
}
service
public void addOrder(Object order) throws Exception {
orderDao.addOrder(order);
//throw new Exception();
}
public void updateOrder(Object order) throws Exception {
orderDao.updateOrder(order);
//throw new Exception();
}
public static void main(String[]args){
List orderItems = new ArrayList();
Order order = new Order();
order.setName("name333");
// order.setId(1);
OrderItem orderItem = new OrderItem();
orderItem.setOrder(order);
//order.getId();
orderItem.setName("nameNew");
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:applicationContext-common.xml");
OrderService orderService = (OrderService) ctx.getBean("orderService");
try {
//orderService.addOrder(order);
orderService.updateOrder(orderItem);
} catch (Exception e) {
e.printStackTrace();
}
分享到:
相关推荐
spring+springmvc+jpa零配置注解开发 不需要在xml中配置任何东西
Struts+Hibernate+spring+jpa+lucene+jbpm+compass配置文件
Struts2+Spring+JPA整合配置实例,及增删改查实例 (jar包放在另外一个文件中,超过15M了,请从新下载)
spring+jpa的applicationContext.xml配置
myfaces+ajax4jsf+spring+jpa+chartCreate 配置备忘 完整eclipse项目源代码
用户管理 提供用户的相关配置 角色管理 角色菜单分配权限 权限管理 权限细化到接口 菜单管理 已实现动态路由,后端可配置化 系统日志 记录用户访问监控异常信息 系统缓存管理 将redis的操作可视化,提供对redis的...
基于注解的Struts2 + Spring + Jpa的架构配置,并且采用了arid实现对Bean的自动装配。
采用hibernate 4.3.6,spring 4.0.6 ,实现jpa配置,简单易懂明了。压缩文件中包含jpa依赖的hibernate包,如果下载者希望运行,需要自己写实体类。压缩包中含有精简的配置。
spring 3.2+ jpa 1.1+ atomikos3.9 配置mysql+sqlserver2000多数据源 包含所需所有jar包,完整项目
例子 博文链接:https://log-cd.iteye.com/blog/562054
声明:该应用仅仅是技术研究:Spring Data JPA的配置和常见api的使用&maven构建项目,其他技术不在此研究 内涵sql和各种Spring Data JPA测试和案例,导入&创建DB,配置好maven便可运行 maven构建项目&问题:...
Spring Boot+Jpa多数据源配置Demo,可同时支持多种数据库,不同数据库,同时支持不同数据库的JdbcTemplate
spring+struts2.2+JPA环境配置小项目,直接导入运行
NULL 博文链接:https://prowl.iteye.com/blog/519618
技术架构:SpringMVC4+Spring4+Spring Data JPA+hibernate4+Maven 声明:查了好多资料,下载了好多代码,发现没有几个能运行起来的,于是写下此代码,希望能够帮助后来者。此程序导入&创建DB,配置好maven便可运行。...
spring+springmvc+spring data jpa+mysql代码示例。运行前需创建数据库sssp并修改相关数据库配置
struts2+jpa+spring2.5配置基础框架,直接运行即可
在SpringBoot中整合使用SpringDataJPA,完全快速上手开发,不再需要繁琐的文件配置
本系统是基于Thymeleaf+SpringBoot+SpringDataJPA实现的的中小医院信息管理系统。简单实现了挂号收费,门诊管理,划价收费,药房取药,体检管理,药房管理,系统维护等基础功能。就诊卡提供了手动和读卡两种方式录入...