不上图了,太麻烦,直接说具体的步骤.
1.首先挑选好对应的Hibernate jar包.下面我项目里的jar包是可以对应使用的.包没几个,就不再说了,
2.OK之后各种配置,伤脑筋,不想再说了.下面配置文件都有,方便就去copy改,基本上不需要改动什么了.想认知的,里面也有注释.
3.OK之后,推荐使用ui文件弄好模板文件,各种减少代码量.
4.写好基类,推荐使用泛型,代码不要太少了,看着service层空荡荡的都有点不习惯,基本操作类基本不需要再写什么代码了.
5.各种调试.
总结:这个框架,主体上是spring掌管一切.
A:ManagedBean全部交给Spring,使用@Controller @Scope("request,session,view等")替代@ManagedBean和@ViewScope等注解. 后续做AOP也方便.
B:Service层需要使用spring的 @Transactional @Service注解.以供IOC操作.
遇到一个很头疼的异常,事务回滚,jpa无法添加或者编辑数据库数据,在service层加入 @Transactional 后解决.异常2个如下:
javax.persistence.TransactionRequiredException: Executing an update/delete query at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) at $Proxy112.executeUpdate(Unknown Source) at org.credo.common.service.LoginService.updateUserinfo(LoginService.java:40) at org.credo.common.controller.LoginBean.modifyAboutMe(LoginBean.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:264) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:293) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) at $Proxy22.flush(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) at $Proxy22.flush(Unknown Source) at org.credo.common.service.LoginService.updateUserinfo(LoginService.java:38) at org.credo.common.controller.LoginBean.modifyAboutMe(LoginBean.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:264) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) at javax.faces.component.UICommand.broadcast(UICommand.java:300) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662)
POM文件如下,这是第一步,POM是最后项目完善使用的:
<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.0</modelVersion> <groupId>org.credo</groupId> <artifactId>base</artifactId> <version>0.0.1</version> <packaging>war</packaging> <name>base</name> <properties> <theme.version>1.0.6</theme.version> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- repositories --> <repositories> <repository> <id>prime-repo</id> <name>PrimeFaces Maven Repository</name> <url>http://repository.primefaces.org</url> <layout>default</layout> </repository> </repositories> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <!-- jsf --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- primefaces --> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>3.2</version> </dependency> <!-- dataSource --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- proxy spring used--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.1_3</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- aop spring used--> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> <scope>compile</scope> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> <scope>compile</scope> </dependency> <!-- springframework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-dao</artifactId> <version>2.0.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.0.6.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Themes --> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>afterdark</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>afternoon</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>afterwork</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>black-tie</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>blitzer</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>bluesky</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>casablanca</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>cruze</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>cupertino</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>dark-hive</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>dot-luv</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>eggplant</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>excite-bike</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>flick</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>glass-x</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>home</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>hot-sneaks</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>humanity</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>le-frog</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>midnight</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>mint-choc</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>overcast</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>pepper-grinder</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>redmond</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>rocket</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>sam</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>smoothness</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>south-street</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>start</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>sunny</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>swanky-purse</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>trontastic</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>ui-darkness</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>ui-lightness</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>vader</artifactId> <version>${theme.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.20</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.3</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.3</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.2.1.ga</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>3.2.0.Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.2.1.ga</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>ejb3-persistence</artifactId> <version>3.3.2.Beta1</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>base</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>6.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
<?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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-lazy-init="true"> <context:component-scan base-package="org.credo"/> <!-- datasource,context:property-placeholder让spring解析到属性文件 --> <context:property-placeholder location="classpath:jdbc.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}"/> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> </bean> <!-- entity manager factory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="persistenceUnitName" value="punit"/> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaPropertyMap"> <map> <!-- <entry key="dialect" value="tool.HibernateForMySQLDialect"/> --> <entry key="hibernate.hbm2ddl.auto" value="update"/> <entry key="hibernate.show_sql" value="true" /> </map> </property> </bean> <!-- transaction --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- support jsf view scope --> <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="view"> <bean class="org.credo.base.util.ViewScope"></bean> </entry> </map> </property> </bean> <!-- 事务传播行为类型 REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。 MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。 REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。 NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。 NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。 --> </beans>
第三步是JPA的配置文件:persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence 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" version="1.0"> <persistence-unit name="punit" transaction-type="RESOURCE_LOCAL"> <class>org.credo.base.entity.IdEntity</class> <class>org.credo.base.entity.BaseEntity</class> <class>org.credo.model.Userinfo</class> </persistence-unit> </persistence>
这三个配置文件是最重要的,其中配置起来也是很麻烦的,我是费劲啊日.
jdbc.properties
#derby configure jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/base jdbc.username=root jdbc.password=root initialSize=10 maxActive=500 maxIdle=5 minIdle=2 #\u5D4C\u5165\u5F0FDerby\u9A71\u52A8 #jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver #jdbc.url=jdbc\:derby\:D\:/workspace/Derby/derbydb;create\=true #####################################log4j.properties
log4j.rootLogger = INFO,CONSOLE, FILE #log4j.rootLogger = INFO, CONSOLE,R log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold = INFO log4j.appender.CONSOLE.Target = System.out log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = [%-5p] [%l] %m%n #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ##################### # File Appender:\u8F93\u51FA\u5230\u6587\u4EF6 ##################### log4j.appender.FILE = org.apache.log4j.FileAppender log4j.appender.FILE.File = log4j.log log4j.appender.FILE.Append = true log4j.appender.FILE.layout = org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern = [%-5p] %-d{yyyy-MM-dd HH:mm:ss, SSS} - %c -%-4r [%t] %c %x - %m%n ######################## # Rolling File:\u8F93\u51FA\u5230\u6587\u4EF6,\u6587\u4EF6\u5230\u6307\u5B9A\u5927\u5C0F\u540E\u4F1A\u81EA\u52A8\u521B\u5EFA\u65B0\u7684\u6587\u4EF6. ######################## #log4j.appender.ROLLING_FILE = org.apache.log4j.RollingFileAppender #log4j.appender.ROLLING_FILE.Threshold = ERROR #log4j.appender.ROLLING_FILE.File = rolling.txt #log4j.appender.ROLLING_FILE.Append = true #log4j.appender.ROLLING_FILE.MaxFileSize = 100KB #log4j.appender.ROLLING_FILE.MaxBackupIndex = 1 #log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout #log4j.appender.ROLLING_FILE.layout.ConversionPattern = [framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n # File \u8F93\u51FA \u4E00\u5929\u4E00\u4E2A\u6587\u4EF6,\u8F93\u51FA\u8DEF\u5F84\u53EF\u4EE5\u5B9A\u5236,\u4E00\u822C\u5728\u6839\u8DEF\u5F84\u4E0B #log4j.appender.Daily = org.apache.log4j.DailyRollingFileAppender #log4j.appender.Daily.File = daily_log.txt #log4j.appender.Daily.MaxFileSize = 500KB #log4j.appender.Daily.MaxBackupIndex = 10 #log4j.appender.D.Append = true #log4j.appender.D.Threshold = ERROR #log4j.appender.Daily.layout = org.apache.log4j.PatternLayout #log4j.appender.Daily.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss, SSS} [%t] [%c] [%p] - %m%n