【济南中心】JavaEE框架:spring(三)——spring4新特性
antlr-2.7.7.jar aopalliance-1.0.jar asm-3.2.jar aspectjrt-1.7.1.jar aspectjweaver-1.7.1.jar commons-beanutils-1.8.3.jar commons-codec-1.7.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar commons-fileupload-1.2.2.jar commons-io-2.4.jar commons-lang3-3.1.jar commons-logging-1.1.1.jar commons-pool-1.6.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.2.0.Final.jar hibernate-entitymanager-4.2.0.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.0.Final.jar mysql-connector-java-5.1.9.jar slf4j-api-1.7.3.jar |
@Entity @Table(name="tbl_user") public class UserModel { @Id private Integer uuid; private String name; private Integer age; //省略getter/setter } |
public interface UserRepository extends JpaRepository<UserModel, Integer>{ //空的,可以什么都不用写 } |
@Service @Transactional public class Client { @Autowired private UserRepository ur; public void testAdd(UserModel um){ ur.save(um); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Client c = (Client)ctx.getBean("client"); UserModel um = new UserModel(); um.setAge(1); um.setName("张三"); um.setUuid(1); c.testAdd(um); } } |
<?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" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/s ... ing-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 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> <context:component-scan base-package="cn.javass"> <context:exclude-filter type="annotation“ expression="org.springframework.stereotype.Controller"/> </context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 开启注解事务 只对当前配置文件有效 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <jpa:repositories base-package="cn.javass" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"> </jpa:repositories> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="cn.javass"/> <property name="persistenceProvider"> <bean class="org.hibernate.ejb.HibernatePersistence"/> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false"/> <property name="database" value="MYSQL"/> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> <property name="showSql" value="true"/> </bean> </property> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/> </property> <property name="jpaPropertyMap"> <map> <entry key="hibernate.query.substitutions" value="true 1, false 0"/> <entry key="hibernate.default_batch_fetch_size" value="16"/> <entry key="hibernate.max_fetch_depth" value="2"/> <entry key="hibernate.generate_statistics" value="true"/> <entry key="hibernate.bytecode.use_reflection_optimizer" value="true"/> <entry key="hibernate.cache.use_second_level_cache" value="false"/> <entry key="hibernate.cache.use_query_cache" value="false"/> </map> </property> </bean> <!--事务管理器配置--> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property> <property name="url"><value>jdbc:mysql://localhost:3306/cc?useUnicode=true&characterEncoding=UTF-8</value></property> <property name="username"> <value>root</value> </property> <property name="password" value="cc"/> </bean> </beans> |
<repositories base-package="com.acme.repositories"> <context:exclude-filter type="regex" expression=".*SomeRepository" /> </repositories> |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |