springboot整合mybatis(2)

1.因为已经生成了dao层的配置文件和接口 只需要写service层和controller层即可

2.controller层

package com.mc_74120.springbootmybatis.controller;

import com.mc_74120.springbootmybatis.pojo.User;
import com.mc_74120.springbootmybatis.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@PostMapping("/addUser")
public String addUser(User user){
try {
userService.addUser(user);
}
catch (Exception e){
e.printStackTrace();
return "error";
}
return "redirect:/ok";
}
@GetMapping("findAllUser")
public String findAllUser(Model model){
try {
List<User> users= userService.findAllUser();
model.addAttribute("userList",users);
}
catch (Exception e){
e.printStackTrace();
return "error";
}
return "selectUser";
}
@GetMapping("/preUpdate")
public String preUpdate(@RequestParam Integer id,Model model){
try {
User user= userService.preUpdate(id);
model.addAttribute("user",user);
}
catch (Exception e){
e.printStackTrace();
return "error";
}
return "updateUser";
}
@PostMapping("/updateUser")
public String updateUser(User user){
try {
userService.updateUser(user);
}
catch (Exception e){
e.printStackTrace();
return "error";
}
return "redirect:/ok";
}
@GetMapping("/deleteUser")
public String deleteUser(Integer id){
try {
userService.deleteUser(id);
}
catch (Exception e){
e.printStackTrace();
return "error";
}
return "ok";
}
@ExceptionHandler(value = {java.lang.NullPointerException.class})
public ModelAndView nullPointExceptionHandler(Exception e,ModelAndView ModelAndView){
ModelAndView.addObject("err",e.toString());
ModelAndView.setViewName("");
return ModelAndView;
}
}
3.service层
package com.mc_74120.springbootmybatis.service;

import com.mc_74120.springbootmybatis.mapper.UserMapper;
import com.mc_74120.springbootmybatis.pojo.User;
import com.mc_74120.springbootmybatis.pojo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@Transactional
public void addUser(User user) {
userMapper.insert(user);

}

@Override
@Transactional
public List<User> findAllUser() {
UserExample userExample=new UserExample();
return userMapper.selectByExample(userExample);
}

@Override
public User preUpdate(Integer id) {
return userMapper.selectByPrimaryKey(id);
}

@Override
@Transactional
public void updateUser(User user) {
userMapper.updateByPrimaryKeySelective(user);
}

@Override
@Transactional
public void deleteUser(Integer id) {
userMapper.deleteByPrimaryKey(id);
}
}
package com.mc_74120.springbootmybatis.service;

import com.mc_74120.springbootmybatis.pojo.User;

import java.util.List;

public interface UserService {
void addUser(User user);
List<User> findAllUser();
User preUpdate(Integer id);
void updateUser(User user);
void deleteUser(Integer id);
}

猜你喜欢

转载自www.cnblogs.com/mc-74120/p/12741814.html