欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring2.5注释型 + jpa + 数据库池的配置(4)

程序员文章站 2022-03-02 14:38:13
...
PersonDao.java

package org.jinsheng.dao;

import java.util.List;

import org.jinsheng.jpa.entity.Person;

public interface PersonDao
{
public void addPerson(Person person);

public void deletePerson(long id);

public void updatePerson(Person person);

public Person findPerson(long id);

public long getPersonCount();
}



PersonDaoImpl.java

package org.jinsheng.dao.impl;

import java.util.List;

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

import org.jinsheng.dao.PersonDao;
import org.jinsheng.jpa.entity.Person;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component("personDaoImpl")
@Transactional // 引用spring中配置的事务
public class PersonDaoImpl implements PersonDao
{
@PersistenceContext // 从spring容器中配置的EntityManagerFactory提取EntityManager注入
EntityManager em;

public void addPerson(Person person)
{
em.persist(person);
}

public void deletePerson(long id)
{
Person person = em.getReference(Person.class, id);
em.remove(person);
}

public void updatePerson(Person person)
{
em.merge(person);
}

@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) //设置事务为只读
public Person findPerson(long id)
{
Person person = em.find(Person.class, id);
return person;
}

@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public long getPersonCount()
{
String str = "select count(id) from Person";
return (Long) em.createQuery(str).getSingleResult();
}

}




ServerDao.java 组件文件,用于引出数据库操作的dao类

package org.jinsheng.dao;

public interface ServerDao
{
public PersonDao getPersonDao();
}



ServerDaoImpl.java ServerDao的实现类

package org.jinsheng.dao.impl;

import org.jinsheng.dao.PersonDao;
import org.jinsheng.dao.ServerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("server") // 声名此类在spring容器中的 bean 名称
public class ServerDaoImpl implements ServerDao
{
@Autowired // 自动注入 bean
private PersonDao personDao;

public PersonDao getPersonDao()
{
return personDao;
}
}


junit4的测试

package org.jinsheng.dao.impl;

import java.util.Date;

import org.jinsheng.dao.PersonDao;
import org.jinsheng.dao.ServerDao;
import org.jinsheng.jpa.entity.Person;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class PersonDaoImplTest extends TestCase
{
private PersonDao personDao;

public PersonDaoImplTest()
{
ServerDao serverDao = (ServerDao) new ClassPathXmlApplicationContext(
"applicationContext.xml").getBean("server");
personDao = serverDao.getPersonDao();
}

@Test
public void testAddPerson()
{
Person person = new Person();
person.setName("jinsheng");
person.setUsername("pjsh");
person.setPassword("pjsh");
person.setSex("男");
long sCount = personDao.getPersonCount();
long startTime = new Date().getTime();
for (int i = 0; i <= 100000; i++)
{
person.setAge(i);
personDao.addPerson(person);
}
long endTime = new Date().getTime();
long eCount = personDao.getPersonCount();
System.out.println("分事务调用执行10W次数据插入所用时间为:" + (endTime - startTime) + " 毫秒。");
// 本人在个人的机子上测试10W条插入的时间是8分多钟,好久,不懂优化,呵呵。
System.out.println("更新前后的数据差为:" + (eCount - sCount));
}

@Test
public void testGetPersonCount()
{
long count = personDao.getPersonCount();
System.out.println("总数为:" + count);
}

@Test
public void testDeletePerson()
{
personDao.deletePerson(15);
}

@Test
public void testUpdatePerson()
{
Person person = new Person();
person.setId(4);
person.setName("jinsheng");
person.setUsername("pjsh");
person.setPassword("pjsh");
person.setSex("男");
person.setAge(25);
personDao.updatePerson(person);
}

@Test
public void testFindPerson()
{
Person person = personDao.findPerson(3);
System.out.println(person);
}

}