标签:
@Entity //声明实体
@Table(name="tbl_user") //对应表
public class UserModel {
//定义主键
@Id
private Integer uuid;
private String name;
private Integer age;
//省略getter/setter
}
nDAO的接口
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/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
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
<!--定义服务层代码包存放路径,包下自动找@service注释的类-->
<context:component-scan base-package="com.....">
<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"/>
<!--定义repository接口存放目录-->
<!--定义接口实现的后缀,通常用Impl-->
<!--定义实体工厂的引用-->
<!--定义事务管理器的引用-->
<jpa:repositories
base-package="com......repository"
repository-impl-postfix="Impl"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager">
</jpa:repositories>
<!--定义实体工厂bean-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="......" ref="....."/>
<property name="......" value="......"/>
<property name=".......">
<bean class="........"/>
</property>
</bean>
<!--事务管理器配置-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
标签:
原文地址:http://www.cnblogs.com/wzz1020/p/4761685.html