数据库表结构(user表)
项目结构
public class UserEntity {
private Integer userId; //用户Id
private String userName; //用户姓名
private String userAddress; //用户家庭住址
public UserEntity() {
}
public UserEntity(Integer userId, String userName, String userAddress) {
this.userId = userId;
this.userName = userName;
this.userAddress = userAddress;
}
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 String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
@Override
public String toString() {
return "UserEntity{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userAddress='" + userAddress + '\'' +
'}';
}
}
@Mapper
public interface UserMapper {
/**
* 查询所有用户信息
* @return
*/
List<UserEntity> selectAllUser();
/**
* 添加用户
* @param user
* @return
*/
Integer addUser(UserEntity user);
/**
* 修改用户
* @param user
* @return
*/
Integer updateUser(UserEntity user);
/**
* 删除用户
* @param user
* @return
*/
Integer deleteUser(UserEntity user);
/**
* 根据id查询用户信息
* @param id
* @return
*/
UserEntity getUser(Integer id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:命名空间必须要对应mapper(或dao)接口的完全限定名(包名+类名)-->
<mapper namespace="com.example.ytgs_curd.mapper.UserMapper">
<!-- 结果集映射:
id是resultMap的唯一标识,在一个mapper标签中,可以定义多个resultMap标签,但他们的id不能重复;
type对应要把列转换为哪个类的对象,写哪个类的完全限定名(包名+类名)-->
<!-- 认为映射的主键一个resultMap标签最多只能有一个id标签
property属性对应实体类的属性名,严格区分大小写;
column列对应sql语句返回的列的名字,不区分大小写-->
<!-- 实体类中其他简单类型的属性,使用result标签去映射-->
<resultMap id="BaseResultMap" type="com.example.ytgs_curd.entity.UserEntity">
<id column="userId" property="userId"/>
<result column="userName" property="userName"/>
<result column="userAddress" property="userAddress"/>
</resultMap>
<!-- mapper能写的基本的子标签目前先掌握四个
insert:增标签
delete:删除标签
update:修改标签
select:查询标签-->
<!-- id属性必须要和mapper(或dao)接口中的方法名完全一致,区分大小写
resultMap属性说明当前select标签使用哪个resultMap标签映射
如果要映射的实体类的属性名和列名完全一致,也可不使用resultMap属性
使用resultType属性写要转换的实体类的完全限定名(包名+类名)-->
<select id="selectAllUser" resultMap="BaseResultMap">
select * from user;
</select>
<!-- 插入用户信息-->
<insert id="addUser" parameterType="com.example.ytgs_curd.entity.UserEntity">
insert into user(userName,userAddress) values(#{userName},#{userAddress});
</insert>
<!--修改用户信息-->
<update id="updateUser" parameterType="com.example.ytgs_curd.entity.UserEntity">
update user set userName=#{userName},userAddress=#{userAddress} where userId=#{userId};
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="com.example.ytgs_curd.entity.UserEntity">
delete from user where userId=#{userId};
</delete>
<!--查询用户信息-->
<select id="getUser" resultType="com.example.ytgs_curd.entity.UserEntity">
select * from user where userId = #{userId}
</select>
</mapper>
文件结构
public interface UserService {
/**
* 查询所有用户信息
* @return
*/
List<UserEntity> showAllUser();
/**
* 添加用户信息
* @param user
* @return
*/
Integer addUserInfo(UserEntity user);
/**
* 修改用户信息
* @param user
* @return
*/
Integer updateUserInfo(UserEntity user);
/**
* 删除用户信息
* @param user
* @return
*/
Integer deleteUserInfo(UserEntity user);
/**
* 根据用户查询用户信息
* @param id
* @return
*/
UserEntity getUserInfo(Integer id);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<UserEntity> showAllUser() {
return userMapper.selectAllUser();
}
@Override
public Integer addUserInfo(UserEntity user) {
return userMapper.addUser(user);
}
@Override
public Integer updateUserInfo(UserEntity user) {
return userMapper.updateUser(user);
}
@Override
public Integer deleteUserInfo(UserEntity user) {
return userMapper.deleteUser(user);
}
@Override
public UserEntity getUserInfo(Integer id) {
return userMapper.getUser(id);
}
}
@RestController //注意
@RequestMapping
public class UserController {
@Autowired
private UserService userService;
String message=""; //用于返回操作状态
/**
* 查询所有用户
* @return
*/
@GetMapping("/showAllUser")
public R<List<UserEntity>> showAllUser(){
return R.ok(userService.showAllUser());
}
/**
* 添加用户
* @param user
* @return
*/
@PostMapping("/addUser")
public String addUer(@RequestBody UserEntity user){
//用Mybatis执行insert语句的时候,插入成功会返回1,不成功则会抛出异常,捕获一下异常就好
try {
userService.addUserInfo(user);
message = "增加用户成功";
}catch (Exception exception){
message = "增加用户异常";
}
return message;
}
/**
* 修改用户信息
* @param user
* @return
*/
@PutMapping("/updateUser")
public String updateUser(@RequestBody UserEntity user){
//Mybatis的更新操作成功返回1,用户不存在返回0,失败则抛异常
try {
message = userService.updateUserInfo(user) == 1?"更新用户成功":"用户不存在,更新失败";
}catch (Exception exception){
message = "更新异常";
}
return message;
}
/**
* 删除用户
* @param user
* @return
*/
@DeleteMapping("/deleteUser")
public String deleteUser(@RequestBody UserEntity user){
//Mybatis的删除操作和更新返回值一样,成功返回删除成功,用户不存在返回删除失败,失败则抛异常
try {
message = userService.deleteUserInfo(user) == 1?"删除用户成功":"用户不存在,删除失败";
}catch (Exception exception){
message = "删除异常";
}
return message;
}
/**
* 根据id查询用户信息
* @param id
* @return
*/
@GetMapping("getUser/{id}")
public String GetUser(@PathVariable int id) {
return userService.getUserInfo(id).toString();
}
}