巴巴运动网学习笔记(16-20)
生活随笔
收集整理的這篇文章主要介紹了
巴巴运动网学习笔记(16-20)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.ProductType的JPA映射
View Code 1 @Entity2 public class ProductType {
3 private int id;
4 private String name;
5 private String note;
6 private boolean visible = true;
7 private Set<ProductType> child;
8 private ProductType parent;
9
10 @Id @GeneratedValue(strategy = GenerationType.AUTO)
11 public int getId() {
12 return id;
13 }
14
15 public void setId(int id) {
16 this.id = id;
17 }
18 @Column(length=30,nullable=false)
19 public String getName() {
20 return name;
21 }
22
23 public void setName(String name) {
24 this.name = name;
25 }
26 @Column(length=100)
27 public String getNote() {
28 return note;
29 }
30
31 public void setNote(String note) {
32 this.note = note;
33 }
34 @Column(nullable=false)
35 public boolean isVisible() {
36 return visible;
37 }
38
39 public void setVisible(boolean visible) {
40 this.visible = visible;
41 }
42 @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
43 public Set<ProductType> getChild() {
44 return child;
45 }
46
47 public void setChild(Set<ProductType> child) {
48 this.child = child;
49 }
50 @ManyToOne(cascade=CascadeType.REFRESH) @JoinColumn(name="parentid")
51 public ProductType getParent() {
52 return parent;
53 }
54
55 public void setParent(ProductType parent) {
56 this.parent = parent;
57 }
58
59 }
2.實(shí)現(xiàn)ProductType的無限級(jí)分類
View Code 1 @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")2 public Set<ProductType> getChild() {
3 return child;
4 }
5
6 public void setChild(Set<ProductType> child) {
7 this.child = child;
8 }
9 @ManyToOne(cascade=CascadeType.REFRESH) @JoinColumn(name="parentid")
10 public ProductType getParent() {
11 return parent;
12 }
13
14 public void setParent(ProductType parent) {
15 this.parent = parent;
16 }
3.實(shí)現(xiàn)ProductTypeService和ProductTypeServiceImpl
a.創(chuàng)建DAO接口,實(shí)現(xiàn)增刪改查代碼的服用
View Code 1 package cnblogs.xiaoqiu.service.base;2
3 public interface DAO {
4
5 /**
6 * 保存實(shí)體
7 * @param entity
8 */
9 public void save(Object entity);
10 /**
11 * 刪除實(shí)體
12 * @param entity
13 */
14 public <T> void delete(Class<T> classType,Object entityId);
15 /**
16 *
17 * @param entitys 要?jiǎng)h除的實(shí)體的ID數(shù)組
18 */
19 public <T> void delete(Class<T> classType,Object[] entitysId);
20 /**
21 * 更新實(shí)體
22 * @param entity
23 */
24 public void update(Object entity);
25 /**
26 * 查找實(shí)體
27 * @param <T>
28 * @param classType 實(shí)體的類型
29 * @param id 實(shí)體的ID
30 * @return
31 */
32 public <T> T find(Class<T> classType,Object id);
33 }
b.創(chuàng)建DAOSupport抽象類,為以后類實(shí)現(xiàn)DAO接口提供方便
View Code 1 package cnblogs.xiaoqiu.service.base;2
3 import javax.persistence.EntityManager;
4 import javax.persistence.PersistenceContext;
5
6 import org.springframework.stereotype.Service;
7 import org.springframework.transaction.annotation.Propagation;
8 import org.springframework.transaction.annotation.Transactional;
9
10 @Service @Transactional
11 public abstract class DAOSupport implements DAO {
12 @PersistenceContext protected EntityManager entityManager;
13
14 public <T> void delete(Class<T> classType,Object entityId) {
15 entityManager.remove(entityManager.getReference(classType, entityId));
16
17 }
18
19 public <T> void delete(Class<T> classType,Object[] entitysId) {
20 for(int i=0;i<entitysId.length;i++){
21 delete(classType, entitysId[i]);
22 }
23 }
24 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
25 public <T> T find(Class<T> classType, Object id) {
26 return entityManager.find(classType, id);
27 }
28
29 public void save(Object entity) {
30 entityManager.persist(entity);
31 }
32
33 public void update(Object entity) {
34 entityManager.merge(entity);
35 }
36
37 }
c.創(chuàng)建ProductTypeService接口
View Code 1 package cnblogs.xiaoqiu.service.product;2
3 import cnblogs.xiaoqiu.service.base.DAO;
4
5 public interface ProductTypeService extends DAO{
6
7 }
d.創(chuàng)建ProductTypeServiceImpl類
package cnblogs.xiaoqiu.service.product.impl;import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cnblogs.xiaoqiu.service.base.DAOSupport;
import cnblogs.xiaoqiu.service.product.ProductTypeService;
@Service @Transactional
public class ProductTypeServiceImpl extends DAOSupport implements ProductTypeService {
}
4.覆蓋DAOSupport的delete方法
View Code 1 package cnblogs.xiaoqiu.service.product.impl;2
3 import org.springframework.stereotype.Service;
4 import org.springframework.transaction.annotation.Transactional;
5 import cnblogs.xiaoqiu.service.base.DAOSupport;
6 import cnblogs.xiaoqiu.service.product.ProductTypeService;
7
8 @Service @Transactional
9 public class ProductTypeServiceImpl extends DAOSupport implements ProductTypeService {
10
11 @Override
12 public <T> void delete(Class<T> classType, Object entityId) {
13 String jpql = "update ProductType p set p.visible=?1";
14 entityManager.createQuery(jpql).setParameter(1, false).executeUpdate();
15 }
16
17 }
5.測(cè)試ProductTypeServiceImpl
轉(zhuǎn)載于:https://www.cnblogs.com/xiaoqv/archive/2012/03/25/2416831.html
總結(jié)
以上是生活随笔為你收集整理的巴巴运动网学习笔记(16-20)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 游戏运营杂谈之-----IB推荐算法
- 下一篇: 在Marketplace上销售应用【WP