springboot+jpa实现底层dao基本增删改查

1.基本底层dao封装

import org.hibernate.validator.constraints.EAN;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.Logger;

@Transactional
public class BaseDao {

    private Class entityClass;

    @PersistenceContext
    protected EntityManager entityManager;

    //很重要,映射entityManager
    protected BaseDao(){
        Type genType = getClass().getGenericSuperclass();//通过反射获取当前类表示的实体(类,接口,基本类型或void)的直接父类的Type
        Type[] params = ((ParameterizedType)genType).getActualTypeArguments();//返回参数数组
        entityClass=(Class)params[0];
    }

    public Class getEntityClass() {
        return entityClass;
    }

    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void save(T entity){
        this.entityManager.persist(entity);
        this.entityManager.flush();
    }

    public void  update(ID id){
        T entity = findByID(id);
        this.entityManager.merge(entity);
        this.entityManager.flush();
    }

    public void delete(ID id){
        T entity = findByID(id);
        this.entityManager.remove(entity);
    }

    public T findByID(ID id){
        T pojo = (T) this.entityManager.find(entityClass,id);
        return pojo;
    }

    public List findAll(String qlString){
        Query query = this.entityManager.createQuery(qlString);
        return query.getResultList();
    }

}

2.创建user实体类进行测试

@Entity
@Table(name = "user")
public class User {
    @Id
    private Long id;
    private String name;
    private String password;

    //省略get、set方法
}

3.创建userDao继承底层BaseDao

@Repository
public class UserDao extends BaseDao {
}

4.创建service调用userDao中的增删改

import com.systop.demo.moudle.dao.UserDao;
import com.systop.demo.moudle.entity.User;
import com.systop.demo.moudle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
@Transactional(readOnly = true)
public class UserService{

    @Autowired
    private UserDao userDao;

    @Transactional(readOnly = false)
    public void save(User user){
        userDao.save(user);
    }

    public User findById(Long id){
        User user = userDao.findByID(id);
        return user;
    }

    public List findAll(){
        String hql = "from User";
        return userDao.findAll(hql);
    }

    @Transactional(readOnly = false)
    public void update(Long id){
        userDao.update(id);
    }

    @Transactional(readOnly = false)
    public void delete(Long id){
        userDao.delete(id);
    }
}

5.在userController中进行测试

import com.systop.demo.moudle.entity.User;
import com.systop.demo.moudle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/save")
    public String save(){
        User user  = new User();
        user.setId(7L);
        user.setName("aa");
        user.setPassword("aaa");
        userService.save(user);
        return "success";
    }

    @RequestMapping("/find/{id}")
    public User findById(@PathVariable("id") Long id){
       return userService.findById(id);
    }

    @RequestMapping("/find")
    public List find(){
        return userService.findAll();
    }

    @RequestMapping("/update")
    public String update(){
        User user  = new User();
        user.setId(2L);
        user.setName("abcde111");
        user.setPassword("aaa");
        userService.update(2L);
        return "success";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        userService.delete(id);
        return "删除成功";
    }
}

 

你可能感兴趣的:(java)