在进行删除操作中发现的一个很奇怪的问题,希望有大佬能帮忙解惑
以下是涉及到问题的代码:
- Controll层 :
@GetMapping("/deleteByUserName/{userName}")
public ModelAndView deleteByUserName(@PathVariable("userName") String userName){
System.out.println("username="+userName);
entityService.deleteByUserName(userName);
return new ModelAndView("redirect:/updateUserInfoFirst");
}
@RequestMapping("/updateUserInfoFirst")
public String updateUserInfo(@PathParam("userId") Integer userId,Model model){
if(userId == null){
List<User> findUser = userRepository.findAll();
model.addAttribute("findUser", findUser);
}else{List<User> findUser =userRepository.findAllByUserId(userId);
model.addAttribute("findUser",findUser);
}
return "/updateUserInfoFirst";
}
- Service层:
@Transactional
public void deleteByUserName(String userName) {
List<User> users = userRepository.findAllByUserName(userName);
//如果删除维护端数据,只是把维护端的List清空
for(User user : users) {
user.getRoles().clear();
userRepository.save(user); //执行save()之后才会保存到数据库中
}
userRepository.deleteByUserName(userName);
}
- Dao层:
public interface UserDao extends JpaRepository<User, Integer> {
void deleteByUserName(String userName);
}
- Entity层:
@Entity
@Table(name = "user_table")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Integer userId;
@Column(name = "user_name")
private String userName;
//关键点
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(
name = "user_role", //name是表名
//joinColumns设置的是entity中属性到关系表的映射名称,name是映射表中的字段名
joinColumns = {@JoinColumn(name = "user_id")},
//inverseJoinColumns,name是关系实体Role的id在关系表中的名称
inverseJoinColumns = {@JoinColumn(name = "role_id")}
)
private List<Role> roles;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
}
- html:
<tr th:each="show:${findUser}" >
<td th:text="${show.userId}" ></td>
<td th:text="${show.userName}" ></td>
<td th:text="${show.roles.size() == 0 ? '无' : show.roles[0].roleName}" id="roleName"></td>
<td><a href="#" th:onclick="toPageAsk('/updateUserInfoThird/[[${show.userId}]]')">修改</a></td>
<td><a href="#" th:onclick="toPageAsk('/deleteByUserName/[[${show.userName}]]')">删除</a></td>
</tr>
- js :
function toPageAsk(curl){
//curl = curl.replace('"','').replace('"','');
alert(curl)
$("#contentdiv").load(curl,null,function(){/*alert("加载成功")*/});
}
//contentdiv是页面布局的一个属性
插入图片显示一下问题所在:
点击删除按钮后,toPageAsk方法执行失败,但是js alert了结果:
Debug断点测试也是这样:
所以问题就是为什么在传参数的时候参数会自己带上一个双引号呢?是我传参数的格式哪里有问题吗?而且比较奇怪的是,传ID参数不会出现这样的问题,但是传Name参数(包括名字是数字的参数)就会出现这样的问题。望指教