package com.allqj.calculator.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
/**
* spec多表尝试电影表实体类
*
* @author Xing
* @CREATE 2018-03-21
*/
@Entity
@Setter
@Getter
@Table(name = "movie")
public class MovieBean implements Serializable {
@Id
@GenericGenerator(name = "bb", strategy = "guid")
@GeneratedValue(generator = "bb")
@Column(name = "mid", nullable = false)
private String mid; @Column(name = "mname")
private String mname; @Column(name = "mtype")
private String mtype; @Column(name = "mfeel")
private String mfeel;
@JsonIgnoreProperties(value = { "movies" })
//@JsonIgnore
@ManyToMany(mappedBy = "movies")
private List<SuperStarBean> supers;
}
//@OneToMany(mappedBy="movies",/*targetEntity=SuperstarToMovie.class,(如果使用泛型来定义集合以指定元素类型,则不需要指定关联的目标实体类型; 否则必须指定目标实体类)*/fetch=FetchType.LAZY)
package com.allqj.calculator.entity;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import com.allqj.calculator.entity.MovieBean;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* spec多表尝试演员表实体类
*
* @author Xing
* @CREATE 2018-03-12-18:31
*/ @Entity
@Getter
@Setter
@Table(name = "superstar")
public class SuperStarBean implements Serializable {
@Id
@GenericGenerator(name = "aa", strategy = "guid")
@GeneratedValue(generator = "aa")
@Column(name = "sid", nullable = false)
private String sid; @Column(name = "name")
private String name; @Column(name = "sex")
private String sex;
@JsonIgnoreProperties(value = { "supers" })
@ManyToMany(fetch = FetchType.EAGER, cascade ={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
@JoinTable(name = "movie_superstar",
joinColumns = {
@JoinColumn(name = "sid")},
inverseJoinColumns = {
@JoinColumn(name = "mid")}) private List<MovieBean> movies; }
package com.allqj.calculator.controller;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import com.allqj.calculator.service.ZmanyService;
import com.allqj.qjf.common.controller.BaseController;
import com.allqj.qjf.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; /**
* spec多表尝试Controller层
*
* @author Xing
* @CREATE 2018-03-12-18:30
*/
@RestController
@RequestMapping("/tomany")
public class ZmanyController extends BaseController {
@Autowired
private ZmanyService s; @ResponseBody
//根据演员名查询信息
@RequestMapping("/superlist")
public ResultVO list(@RequestBody SuperStarBean stom) {
List<SuperStarBean> list = s.findAllbySuper(stom);
return ok(list);
} //根据电影名查询信息
@RequestMapping("/movielist")
public ResultVO list1(@RequestBody MovieBean mb) {
List<MovieBean> list1 = s.findAllbyMovie(mb);
return ok(list1);
} //删除演员信息中的某部电影關聯
@RequestMapping("/delmovie")
public ResultVO delmovie(@RequestBody String sid) {
s.delmovie(sid);
return ok();
} //刪除電影信息中的某位演員關聯
@RequestMapping("/delsuper")
public ResultVO delsuper(@RequestBody String mid) {
s.delsuper(mid);
return ok();
} //修改或添加演员
@RequestMapping("/superadd")
public ResultVO add(@RequestBody SuperStarBean superStar) {
SuperStarBean add1 = s.add(superStar);
return ok(add1);
} //修改或添加电影
@RequestMapping("/movieadd")
public ResultVO addmovie(@RequestBody MovieBean movie) {
MovieBean addm = s.addm(movie);
return ok();
} /*public boolean register(SuperStarBean supers){
MovieBean role=new MovieBean();
role.setMid((long) 2);
Set<MovieBean> roles=new HashSet<MovieBean>();
roles.add(role);
supers.setMovies(movies); ;
return userService.insert(supers);
}*/ //删除演员
@RequestMapping("/superdelAll")
public ResultVO superdelall(@RequestBody SuperStarBean superbean) {
s.superdelall(superbean);
return ok();
}
//删除电影
@RequestMapping("/moviedelAll")
public ResultVO moviedelall(@RequestBody MovieBean movieBean){
s.moviedelall(movieBean);
return ok();
}
}
package com.allqj.calculator.service;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean; import java.util.List; public interface ZmanyService { List<MovieBean> findAllbyMovie(MovieBean mb); List<SuperStarBean> findAllbySuper(SuperStarBean stom); void delmovie(String sid); void delsuper(String mid); SuperStarBean add(SuperStarBean superStar); MovieBean addm(MovieBean movie); SuperStarBean superdelall(SuperStarBean superbean); void moviedelall(MovieBean movieBean);
}
package com.allqj.calculator.service;
import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import com.allqj.calculator.repository.SuperRepository;
import com.allqj.calculator.repository.ZmanyRepository;
import com.allqj.calculator.repository.ZmanyRepositoryImpl;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects; /**
* spec多表尝试Service层实现类
*
* @author Xing
* @CREATE 2018-03-12-18:32
*/
@Service
public class ZmanyServiceImpl implements ZmanyService{
@Autowired
private ZmanyRepository movieDao;
@Autowired
private SuperRepository superDao;
@Autowired
private ZmanyRepositoryImpl zmanyRepositoryImpl;
@Override
@Modifying
public List<MovieBean> findAllbyMovie(MovieBean mb) {
Specification<MovieBean> specification = new Specifications<MovieBean>()
//.between("age", new Range<>(10, 35))
//.eq(Objects.nonNull(stom.getMovies()),"movie.mname", "666")
.build();
return movieDao.findAll();
}
@Override
@Modifying
public List<SuperStarBean> findAllbySuper(SuperStarBean stom) {
Specification<SuperStarBean> specification = new Specifications<SuperStarBean>()
.between("age", new Range<>(10, 35))
.eq(Objects.nonNull(stom.getMovies()),"movie.mname", "666")
.build();
return superDao.findAll();
} @Override
public void delmovie(String sid) {
zmanyRepositoryImpl.delmovie(sid);
} @Override
public void delsuper(String mid) {
zmanyRepositoryImpl.delsuper(mid);
} @Override
public SuperStarBean add(SuperStarBean superStar) {
return superDao.save(superStar);
} @Override
public MovieBean addm(MovieBean movie) {
return movieDao.save(movie);
} @Override
public SuperStarBean superdelall(SuperStarBean superbean) {
superDao.delete(superbean);
return null;
} @Override
public void moviedelall(MovieBean movieBean) {
// movieDao.deleteByMid("1");
// zmanyRepositoryImpl.delete(movieBean);
}
}
package com.allqj.calculator.repository;

import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import javax.transaction.Transactional; public interface ZmanyRepository extends JpaRepository<MovieBean, Long>, JpaSpecificationExecutor<MovieBean> { }
package com.allqj.calculator.repository;
import com.allqj.calculator.entity.MovieBean;
import com.allqj.calculator.entity.SuperStarBean;
import org.springframework.stereotype.Repository;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.transaction.Transactional; /**
* @author Xing
* @CREATE 2018-03-17-15:33
*/
@Repository
public class ZmanyRepositoryImpl {
@PersistenceContext
private EntityManager em;
/**
* 解除关联关系调用集合对象的remove方法
* 集合的remove方法会删除的是关联关系,也就是删除中间表的某条记录
* 但是,它不会删除实体类对应数据表中的记录
*/
//解除演員信息中與某電影的關係
@Transactional
public void delmovie(String sid){
SuperStarBean superstar = em.find(SuperStarBean.class, sid);
MovieBean item = superstar.getMovies().iterator().next();
superstar.getMovies().remove(item);
}
//解除電影信息中與某演員的關係
@Transactional
public void delsuper(String mid) {
MovieBean movie = em.find(MovieBean.class, mid);
SuperStarBean item = movie.getSupers().iterator().next();
movie.getSupers().remove(item);
}
}
05-08 15:10