@Transactional
@Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null.");
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
* Must be {@link Transient} in order to ensure that no JPA provider complains because of a missing setter.
* @see org.springframework.data.domain.Persistable#isNew()
@Transient // DATAJPA-622
@Override
public boolean isNew() {
return null == getId();
@Transactional
public User save(User user) {
return userDao.save(user);
control层
* 单条数据保存
* @param id 自增的,可以不填
* @param name
* @param age
* @return
@GetMapping("/save")
public User save(Integer id,String name, Integer age){
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return userService.save(user);
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#save(java.lang.Iterable)
@Transactional
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "Entities must not be null!");
List<S> result = new ArrayList<>();
for (S entity : entities) {
result.add(save(entity));
return result;