spring + jpa

applicationContext.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:p="http://www.springframework.org/schema/p "
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd " xmlns:tx="http://www.springframework.org/schema/tx ">
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
       
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
         <property name="persistenceUnitName" value="people"/>
   </bean>   
 
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
 
    <tx:annotation-driven transaction-manager="transactionManager" />
 <bean id="cmservice" class="com..cms.service.CmsServiceImpl" />
</beans>

 

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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 ">
 <persistence-unit name="people">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>  
        <properties>  
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
               
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />  
            <property name="hibernate.connection.username" value="root" />  
            <property name="hibernate.connection.password" value="root" />  
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testjpa" />  
            <property name="hibernate.max_fetch_depth" value="5" />  
            <property name="hibernate.jdbc.fetch_size" value="20" />  
            <property name="hibernate.jdbc.batch_size" value="10" />  
          
            <property name="hibernate.hbm2ddl.auto" value="update" />  
            <property name="hibernate.show_sql" value="true" />  
            <property name="hibernate.format_sql" value="false" />  
        </properties> 
 </persistence-unit>
</persistence>

 

对象:

package com.cms.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "person")
public class Person implements Serializable{
 
 @Id
 @GeneratedValue
 private Integer id;
 @Column(name = "NAME")
 private String name;
 
 public Person() {
  // TODO Auto-generated constructor stub
 }
 
 public Person(Integer id) {
  this.id = id;
 }
 
 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
}

 

Service:

public interface ICmsService {

   public void save(Person o);


 
}

 

@Transactional
public class CmsServiceImpl  implements ICmsService {
 private EntityManager em;
 @PersistenceContext
 public void setEntityManager(EntityManager em){
         this.em = em;
 }
 
 public void save(Person entity) {
  Assert.notNull(entity);
  em.persist(entity);
  return entity;
 }
 
}

 

需要的lib包http://download.youkuaiyun.com/source/2560951

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值