<bean />
   
  </bean>
首先是jar准备
Spring3.0 + JPA(Hibernate3.6)
结构预览
Spring3.0 + JPA(Hibernate3.6)
jdbc:properties
driverClassName =org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:8899/ssh2?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initialSize =1
maxActive=10
maxIdle=2
minIdle=1
----------------------------------------------------------------------------------------------------------------
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="ssh2"> <!-- 实体bean集合名字 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- JPA驱动提供者 -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
----------------------------------------------------------------------------------------------------------------
beans.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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:component-scan base-package="ben.s3h3.demo" />
<context:property-placeholder location="classpath:jdbc.properties" />

<bean /><!-- 事务声明方式是注解 -->
</beans>
----------------------------------------------------------------------------------------------------------------
IDType.java
package ben.s3h3.demo.bean.id;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class IDType {
private Integer id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}

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

}
----------------------------------------------------------------------------------------------------------------
IDService.java
package ben.s3h3.demo.service.id;

import ben.s3h3.demo.bean.id.IDType;

public interface IDService
{
void save(IDType type);
}
----------------------------------------------------------------------------------------------------------------
IDServiceBean.java
package ben.s3h3.demo.service.id.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ben.s3h3.demo.bean.id.IDType;
import ben.s3h3.demo.service.id.IDService;

@Service //标注为Spring的Servicebean,供自动扫描机制加载该bean,就不用再beans.xml中声明了
@Transactional
public class IDServiceBean implements IDService {

@PersistenceContext
EntityManager em;

public void save(IDType type) {
em.persist(type);
}
}
----------------------------------------------------------------------------------------------------------------
没用JUnit 写了个main方法
package main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ben.s3h3.demo.bean.id.IDType;
import ben.s3h3.demo.service.id.IDService;

public class DemoRun
{
public static void main(String[] args)
{
System.out.println("测试Spring3.05+JPA(Hibernate3.6)环境");
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
IDService idService = (IDService) cxt
.getBean("IDServiceBean");
idService.save(new IDType());
System.out.println("测试完毕 ---> 查看数据库去 :-)");
}
}

相关文章:

  • 2021-10-28
  • 2022-12-23
  • 2021-12-11
  • 2021-12-10
  • 2021-11-03
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2021-11-07
  • 2021-07-10
  • 2021-07-16
  • 2021-05-29
相关资源
相似解决方案