提交 95634872 编写于 作者: C Calvin

quikstart删除用户时需要删除Task,演示Spring Data JPA下的delete/update语句的写法

上级 37883671
...@@ -2,9 +2,16 @@ package org.springside.examples.quickstart.repository; ...@@ -2,9 +2,16 @@ package org.springside.examples.quickstart.repository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import org.springside.examples.quickstart.entity.Task; import org.springside.examples.quickstart.entity.Task;
public interface TaskDao extends PagingAndSortingRepository<Task, Long> { public interface TaskDao extends PagingAndSortingRepository<Task, Long> {
Page<Task> findByUserId(Long id, Pageable pageRequest); Page<Task> findByUserId(Long id, Pageable pageRequest);
@Modifying
@Query("delete from Task task where task.user.id=?1")
void deleteByUserId(Long id);
} }
...@@ -12,6 +12,7 @@ import org.springframework.data.jpa.domain.support.DateTimeProvider; ...@@ -12,6 +12,7 @@ import org.springframework.data.jpa.domain.support.DateTimeProvider;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.quickstart.entity.User; import org.springside.examples.quickstart.entity.User;
import org.springside.examples.quickstart.repository.TaskDao;
import org.springside.examples.quickstart.repository.UserDao; import org.springside.examples.quickstart.repository.UserDao;
import org.springside.examples.quickstart.service.ServiceException; import org.springside.examples.quickstart.service.ServiceException;
import org.springside.examples.quickstart.service.account.ShiroDbRealm.ShiroUser; import org.springside.examples.quickstart.service.account.ShiroDbRealm.ShiroUser;
...@@ -35,6 +36,7 @@ public class AccountService { ...@@ -35,6 +36,7 @@ public class AccountService {
private static Logger logger = LoggerFactory.getLogger(AccountService.class); private static Logger logger = LoggerFactory.getLogger(AccountService.class);
private UserDao userDao; private UserDao userDao;
private TaskDao taskDao;
private DateTimeProvider dateTimeprovider = CurrentDateTimeProvider.INSTANCE; private DateTimeProvider dateTimeprovider = CurrentDateTimeProvider.INSTANCE;
public List<User> getAllUser() { public List<User> getAllUser() {
...@@ -73,6 +75,8 @@ public class AccountService { ...@@ -73,6 +75,8 @@ public class AccountService {
throw new ServiceException("不能删除超级管理员用户"); throw new ServiceException("不能删除超级管理员用户");
} }
userDao.delete(id); userDao.delete(id);
taskDao.deleteByUserId(id);
} }
/** /**
...@@ -106,6 +110,11 @@ public class AccountService { ...@@ -106,6 +110,11 @@ public class AccountService {
this.userDao = userDao; this.userDao = userDao;
} }
@Autowired
public void setTaskDao(TaskDao taskDao) {
this.taskDao = taskDao;
}
public void setDateTimeProvider(DateTimeProvider dateTimeProvider) { public void setDateTimeProvider(DateTimeProvider dateTimeProvider) {
this.dateTimeprovider = dateTimeProvider; this.dateTimeprovider = dateTimeProvider;
} }
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
<property name="cacheManager" ref="shiroEhcacheManager" /> <property name="cacheManager" ref="shiroEhcacheManager" />
</bean> </bean>
<!-- 項目自定义的Realm --> <!-- 項目自定义的Realm, 所有accountService依赖的dao都需要用depends-on声明 -->
<bean id="shiroDbRealm" class="org.springside.examples.quickstart.service.account.ShiroDbRealm" depends-on="userDao"> <bean id="shiroDbRealm" class="org.springside.examples.quickstart.service.account.ShiroDbRealm" depends-on="userDao,taskDao">
<property name="accountService" ref="accountService"/> <property name="accountService" ref="accountService"/>
</bean> </bean>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册