随笔-112  评论-73  文章-0  trackbacks-0
使用maven工程,pom.xml文件如下
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.8.Final</version>
        </dependency>

        <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.8.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
       <!-- <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
        </dependency>
-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

dao接口如下:
package cn.test.dao;

/**
 * Created by libo on 13-12-19.
 
*/
public interface IDao {

    void save(Object entity);
}

dao实现类如下:
package cn.test.dao;

import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Created by libo on 13-12-19.
 
*/
@Component
public class DaoJpaImpl implements IDao {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void save(Object entity) {
        entityManager.persist(entity);
    }
}

service 接口:
package cn.test.service;

import cn.test.entity.User;

/**
 * Created by libo on 13-12-19.
 
*/
public interface UserService {
    void save(User user);
}

service 实现类:
package cn.test.service;

import cn.test.dao.IDao;
import cn.test.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by libo on 13-12-19.
 
*/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private IDao iDao;

    @Override
    //@Transactional(propagation= Propagation.REQUIRED)
    @Transactional(readOnly = false)
    public void save(User user) {
        iDao.save(user);
    }
}
实体类:
package cn.test.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * Created by libo on 13-12-19.
 
*/
@Entity
@Table(name = "myUser")
public class User implements Serializable{

    @Id
    private String id;

    private String name;

    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

spring-config.xml 其中
task相关内容可以删除
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx
="http://www.springframework.org/schema/tx"
       xmlns:task
="http://www.springframework.org/schema/task"
       xmlns:context
="http://www.springframework.org/schema/context"
       xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
>
    <context:annotation-config/>
    <context:component-scan base-package="cn.test"/>
    <task:annotation-driven/>

    <bean id="em" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="cn.test.entity"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_jpa"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="em"/>
    </bean>
    <!-- 可以使用@Transactional配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="persistenceExceptionTranslationPostProcessor"
          class
="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>

测试类:
package cn.test.service;

import cn.test.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by libo on 13-12-19.
 
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class UserServiceImplTest {

    @Autowired
    private UserService userService;

    @Test
    public void testSave() throws Exception {
        User user = new User();
        user.setId("u_id");
        user.setName("u_name");
        user.setAge(23);
        userService.save(user);
    }
}

运行Junit测试就可以了。连接的是MySQL数据库

end.
posted on 2013-12-19 16:17 Libo 阅读(572) 评论(0)  编辑  收藏 所属分类: Spring数据库

只有注册用户登录后才能发表评论。


网站导航: