springboot restful jpa

82次阅读

共计 3259 个字符,预计需要花费 9 分钟才能阅读完成。

1 实体类
public class User {
/** 编号 */
private int id;
/** 姓名 */
private String name;
/** 年龄 */
private int age;

public User(){
}
// getter 和 setter 略
}
2 DAO 层编写
@Mapper
public interface UserDao {

/**
* 用户数据新增
*/
@Insert(“insert into t_user(id,name,age) values (#{id},#{name},#{age})”)
void addUser(User user);

/**
* 用户数据修改
*/
@Update(“update t_user set name=#{name},age=#{age} where id=#{id}”)
void updateUser(User user);

/**
* 用户数据删除
*/
@Delete(“delete from t_user where id=#{id}”)
void deleteUser(int id);

/**
* 根据用户名称查询用户信息
*
*/
@Select(“SELECT id,name,age FROM t_user where name=#{userName}”)
User findByName(@Param(“userName”) String userName);

/**
* 根据用户 ID 查询用户信息
*
*/
@Select(“SELECT id,name,age FROM t_user where id=#{userId}”)
User findById(@Param(“userId”) int userId);

/**
* 根据用户 age 查询用户信息
*/
@Select(“SELECT id,name,age FROM t_user where age = #{userAge}”)
User findByAge(@Param(“userAge”) int userAge);

}
3 service 业务层
public interface UserService {

/**
* 新增用户
* @param user
* @return
*/
boolean addUser(User user);

/**
* 修改用户
* @param user
* @return
*/
boolean updateUser(User user);

/**
* 删除用户
* @param id
* @return
*/
boolean deleteUser(int id);

/**
* 根据用户名字查询用户信息
* @param userName
*/
User findUserByName(String userName);

/**
* 根据用户 ID 查询用户信息
* @param userId
*/
User findUserById(int userId);

/**
* 根据用户 ID 查询用户信息
* @param userAge
*/
User findUserByAge(int userAge);
}
4 serviceImpl 实现层
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

@Override
public boolean addUser(User user) {
boolean flag=false;
try{
userDao.addUser(user);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}

@Override
public boolean updateUser(User user) {
boolean flag=false;
try{
userDao.updateUser(user);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}

@Override
public boolean deleteUser(int id) {
boolean flag=false;
try{
userDao.deleteUser(id);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}

@Override
public User findUserByName(String userName) {
return userDao.findByName(userName);
}

@Override
public User findUserById(int userId) {
return userDao.findById(userId);
}

@Override
public User findUserByAge(int userAge) {
return userDao.findByAge(userAge);
}
}
4 controller 层编写
@RestController
@RequestMapping(value = “/api/user”)
public class UserRestController {
@Autowired
private UserService userService;

@RequestMapping(value = “/addUser”, method = RequestMethod.POST)
public boolean addUser(User user) {
System.out.println(“ 开始新增 …”);
return userService.addUser(user);
}

@RequestMapping(value = “/updateUser”, method = RequestMethod.PUT)
public boolean updateUser(User user) {
System.out.println(“ 开始更新 …”);
return userService.updateUser(user);
}

@RequestMapping(value = “/deleteUser”, method = RequestMethod.DELETE)
public boolean delete(@RequestParam(value = “userName”, required = true) int userId) {
System.out.println(“ 开始删除 …”);
return userService.deleteUser(userId);
}

@RequestMapping(value = “/userName”, method = RequestMethod.GET)
public User findByUserName(@RequestParam(value = “userName”, required = true) String userName) {
System.out.println(“ 开始查询 …”);
return userService.findUserByName(userName);
}

@RequestMapping(value = “/userId”, method = RequestMethod.GET)
public User findByUserId(@RequestParam(value = “userId”, required = true) int userId) {
System.out.println(“ 开始查询 …”);
return userService.findUserById(userId);
}

@RequestMapping(value = “/userAge”, method = RequestMethod.GET)
public User findByUserAge(@RequestParam(value = “userAge”, required = true) int userAge) {
System.out.println(“ 开始查询 …”);
return userService.findUserByAge(userAge);
}
}

正文完
 0