基於註解的SpringMVC整合JPA – JAVA編程語言程序開發技術文章

實體類
Department


package com.sj.bean;


import java.util.Set;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name=”department”,catalog=”sj”)
public class Department {
   
    private int id;
    private String name;
    private Set<Employee> sets;
    @Id
    @Column(name=”id”)
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name=”name”)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToMany(mappedBy=”department”,cascade=CascadeType.ALL)
    public Set<Employee> getSets() {
        return sets;
    }
    public void setSets(Set<Employee> sets) {
        this.sets = sets;
    }
   
}
Employee
package com.sj.bean;


import java.io.Serializable;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;



@SuppressWarnings(“serial”)
@Entity
@Table(name=”employee”,catalog=”sj”)
public class Employee implements Serializable{
   
    private int id;
    private String name;
    private Department department;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name=”id”)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name=”name”)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name=”deptid”)
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
   
}
BaseDAO
package com.sj.dao;


import java.util.List;


public interface BaseDAO<T> {
   
    List<T> listAll();
    Object findById(Class<T> c,int id);
    boolean save(Object object);
    boolean update(Object object);
    boolean delete(Object object);
   
}   
BaseDAOImpl
package com.sj.dao;


import java.util.List;


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


import org.springframework.stereotype.Component;


@Component(“baseDAO”)
public class BaseDAOImpl<T> implements BaseDAO<T> {


    @PersistenceContext(unitName=”sjPU”)
    private EntityManager entityManager;
   
   
    public boolean delete(Object object) {
        try {
            entityManager.remove(object);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public Object findById(Class<T> c,int id) {
        try {
            return entityManager.find(c, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public boolean save(Object object) {
        try {
            entityManager.persist(object);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public boolean update(Object object) {
        try {
            entityManager.merge(object);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    @SuppressWarnings(“unchecked”)
    public List<T> listAll() {
        try {
            Query query=entityManager.createQuery(” from Employee “);
            return query.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
BaseService
package com.sj.service;


import java.util.List;


public interface BaseService<T> {
   
    List<T> listAll();
    Object findById(Class<T> c,int id);
    boolean save(Object object);
    boolean update(Object object);
   

發佈留言