准备一个FactoryBean
/**
* 一个类实现FactoryBean接口,那么它就是一个FactoryBean
* 泛型就是它要操作的那个类型
*/
public class EntityManagerFactoryBean implements FactoryBean<EntityManagerFactory> {
//这个方法返回什么对象,我们拿到的bean就是什么对象
@Override
public EntityManagerFactory getObject() throws Exception {
return Persistence.createEntityManagerFactory();
}
//返回的对象类型
@Override
public Class<?> getObjectType() {
return EntityManagerFactory.class;
}
//这个对象是否是单例的
@Override
public boolean isSingleton() {
return true;
}
}
准备一个静态工厂
public class DaoFactory {
//在这个工厂中有一个静态方法
public static MyDao createMyDao(){
return new MyDao();
}
}
配置这个工厂对应的方法
<bean id="myDao" class="cn.itsource._10_bean.DaoFactory"
factory-method="createMyDao">bean>
准备一个静态工厂
public class DaoFactory {
public MyDao createMyDao(){
return new MyDao();
}
}
配置这个工厂对应的方法
<bean id="daoFactory" class="cn.itsource._10_bean.DaoFactory" />
<bean id="myDao" factory-bean="daoFactory" factory-method="createMyDao" />
导包 -> domain-> db.properties ->datasource -> entityManagerFactory -> tx(事务) -> dao -> service -> controller(springmvc的配置,web.xml) -> easyui
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.itsourcegroupId>
<artifactId>ssjartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>ssj Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>4.3.8.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-entitymanagerartifactId>
<version>4.3.8.Finalversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.6.5version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>ssjfinalName>
build>
project>
db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssj
jdbc.username=root
jdbc.password=123456
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="cn.itsource.ssj.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
bean>
property>
bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager" />
* propagation:事务传播机制
* REQUIRED(默认配置):如果调用我的方法是有事务的,那么我们就使用同一个事务
* REQUIRES_NEW: 不管谁调用我,我都会开一个自己的新事务
* NEVER: 绝对不开事务,看到事务就报错给你看
* SUPPORTS: 支持(调用我的方法有事务,就有,如果没有事务,就算了)
保证一个线程只用一个EntityManager对象
@PersistenceContext
private EntityManager entityManager;