(9) ebj学习: Jpa的增删查改,CURD方法监听和命名查询
生活随笔
收集整理的這篇文章主要介紹了
(9) ebj学习: Jpa的增删查改,CURD方法监听和命名查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 整體結(jié)構(gòu)
client文件存放 ejb客戶端的一些jar包
Animal.java 中配置命名查詢
Person.java 中實(shí)現(xiàn)了方法監(jiān)聽和類監(jiān)聽兩種監(jiān)聽方式( 在增刪查改方法執(zhí)行之前后之后調(diào)用監(jiān)聽方法)
JpaCurdTest.java 為 Junit測試增刪查改
2 代碼
Animal.java
Person.java
package leaning.entity;import java.io.Serializable;import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.PrePersist;@Entity @EntityListeners(PersonListener.class) // 監(jiān)聽方法2 ,自定義在一個(gè)類中 public class Person implements Serializable{@Id@GeneratedValueprivate int id;private String name;private String address;public int getId() {return id;}/*** 監(jiān)聽方法1* 實(shí)體對象的回調(diào)方法,通過PrePersist注解,可以將這個(gè)方法變?yōu)? 當(dāng)實(shí)體對象被保存之前自動(dòng)調(diào)用的方法* 可用注解有 : PrePersist,PostPersist,PreRemove,PostRemove* PreUpdate,PostUpdate,PostLoad* **/@PrePersistpublic void doSomeThing(){System.out.println("@PrePersist 實(shí)體對象即將保存 : name = " + name);}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
PersonListener.java
package leaning.entity;import javax.persistence.PostPersist;public class PersonListener {/*** 自動(dòng)調(diào)用該方法* */@PostPersistpublic void doSomething(Person person){System.out.println("@PostPersist 實(shí)體對象已經(jīng)被加載, name = " + person.getName()); }}
PersonManager.java
package leaning.entity;import java.util.List;public interface PersonManager {public void addPerson(String name);public void removePerson(int id);public void removePerson1(int id);public void updatePerson(int id,String updateName);public Person findPerson(int id);public Person findPerson2(int id);public List searchPerson1();public List searchPerson2();public List searchPerson3();public List searchAnimals();public List searchAllAnimals();}
PersonManagerBean.java
package leaning.entity;import java.util.List;import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;@Stateless(name="personManager") @Remote public class PersonManagerBean implements PersonManager{@PersistenceContext(unitName="curdUnit")private EntityManager em;public void addPerson(String name) {Person p = new Person();p.setName(name);em.persist(p);}public void removePerson(int id) {Person p = em.find(Person.class, id);em.remove(p);}//這種方法不能刪除public void removePerson1(int id) {Person p = new Person();p.setId(id);em.remove(p);}public void updatePerson(int id, String updateName) {Person p = new Person();p.setId(id);p.setName(updateName);em.merge(p);}public Person findPerson(int id) {//相當(dāng)于hibernate中的get操作return em.find(Person.class, id);}public Person findPerson2(int id) {//相當(dāng)于hibernate中的Load操作,加載的是一個(gè)代理類return em.getReference(Person.class, id);}//利用EJB-QL 查詢所有的人員public List searchPerson1() {return em.createQuery("from Person").getResultList();}public List searchPerson2() {return em.createQuery("select p from Person p where p.name like ?").setParameter(1, "%李%") //索引值從1開始{與hibernate中從0開始不同}.getResultList();}public List searchPerson3() {return em.createQuery("select p from Person p where p.name like :name").setParameter("name", "%李%") .getResultList();}/*** 測試命名查詢和多臺(tái)查詢* */public List searchAnimals() {return em.createNamedQuery("searchAnimals").setParameter("name", "%鴨%").getResultList();}public List searchAllAnimals() {return em.createNamedQuery("searchAllAnimals").getResultList();}}
測試類?JpaCurdTest.java package leaning.test;import static org.junit.Assert.*;import java.util.List;import javax.naming.InitialContext; import javax.naming.NamingException;import leaning.entity.Animal; import leaning.entity.Person; import leaning.entity.PersonManager;import org.junit.Test;/*** JPA增刪查改測試* **/ public class JpaCurdTest {@Testpublic void test() throws NamingException {}@Testpublic void testAddPerson() {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");personManager.addPerson("張三");} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testRemovePerson() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");personManager.removePerson(3);} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testRemovePerson1() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");personManager.removePerson1(3);} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testUpdatePerson() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");personManager.updatePerson(3, "updateName");} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testFindPerson() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");Person p = personManager.findPerson(4);System.out.println("name = " + p.getName());} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testFindPerson2() {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");Person p = personManager.findPerson2(4);System.out.println("name = " + p.getName());} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testSearchPerson1() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");List list = personManager.searchPerson1();showList(list);} catch (NamingException e) {e.printStackTrace();}}private void showList(List<Person> list){if(list==null) return ;for(int i = 0 ; i < list.size() ; i++){Person p = list.get(i);System.out.println("name = " + p.getName());}}private void showAnimalList(List<Animal> list){if(list==null) return ;for(int i = 0 ; i < list.size() ; i++){Animal animal = list.get(i);System.out.println("name = " + animal.getName() );}}@Testpublic void testSearchPerson2() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");List list = personManager.searchPerson2();showList(list);} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testSearchPerson3() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");List list = personManager.searchPerson3();showList(list);} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testSearchAnimals() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");List list = personManager.searchAnimals();showAnimalList(list);} catch (NamingException e) {e.printStackTrace();}}@Testpublic void testSearchAllAnimals() throws NamingException {InitialContext context;try {context = new InitialContext();PersonManager personManager = (PersonManager)context.lookup("personManager/remote");List list = personManager.searchAllAnimals();showAnimalList(list);} catch (NamingException e) {e.printStackTrace();}}}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence 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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"><persistence-unit name="curdUnit" transaction-type="JTA"><jta-data-source>java:/MySqlDS</jta-data-source><properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /><property name="hibernate.hbm2ddl.auto" value="update"/></properties></persistence-unit></persistence>
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=localhost java.naming.factory.url.pkgs=org.jboss.naming\:org.jnp.interfaces
總結(jié)
以上是生活随笔為你收集整理的(9) ebj学习: Jpa的增删查改,CURD方法监听和命名查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js时间工具 MyTimeUtil.js
- 下一篇: (10) ejb学习: Jpa的JTA事