其他分享
首页 > 其他分享> > mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上修改某个记录

mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上修改某个记录

作者:互联网

POJO类不变

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

mybatis映射文件:

 

<?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">
<mapper namespace="com.test.mapper.UserMapper">
    <select id="findAll" resultType="com.test.entity.MyUser">
        select * from users
    </select>
    <select id="getUser" resultType="com.test.entity.MyUser" parameterType="int">
        select * from users where id = #{id}
    </select>
    <update id="updateUser" parameterType="com.test.entity.MyUser" >
        update users set id = #{id} , name = #{name} , age = #{age} where id = #{id}
    </update>
</mapper>

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

修改

 

UserMapper

 

 

package com.test.mapper;


import com.test.entity.MyUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    public List<MyUser> findAll();

    @Select("select * from users limit 10")
    public List<MyUser> findShi();


    public MyUser getUser( int id );


    public boolean updateUser( MyUser user);

}

  

 

 

 

 

 

 

 

 

 

 

 

 

修改服务接口:

 

UserService

 

package com.test.service;


import com.test.entity.MyUser;

import java.util.List;

public interface UserService
{
    List<MyUser> findAll();

    List<MyUser> findShi();

    MyUser getUserForId(int id);

    boolean updateUserById(MyUser user);

}

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

修改实现服务接口类:

UserServiceImpl

 

package com.test.service.impl;


import com.test.entity.MyUser;
import com.test.mapper.UserMapper;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    UserMapper userMapper;
    @Override
    public List<MyUser> findAll()
    {
        return userMapper.findAll();
    }

    @Override
    public List<MyUser> findShi()
    {
        return userMapper.findShi();
    }

    @Override
    public MyUser getUserForId(int id)
    {
       MyUser user =   userMapper.getUser(id);

       return user;
    }

    @Override
    public boolean updateUserById(MyUser user)
    {
        boolean flag = false;

        flag = userMapper.updateUser(user);

        return flag;

    }

}

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

新建一个controller类

 

package com.test.controller;

import com.test.entity.MyUser;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UpdateController
{
    @Autowired
    private UserService userService;


    @GetMapping(value = "/4")
    public String home4(MyUser user)
    {
        boolean flagax = userService.updateUserById(user);

        if (flagax == true)
        {
            return  "cheng----gong";
        }
        else
        {
            return  "cuo---wu";
        }

    }


}

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

http://localhost:8080/4?id=1&name=uuuuuuu&age=222

 

 

 

 

 

修改前:

 

 

 

 

 

 

 

 

修改后:

 

标签:spring,boot,id,test,MyUser,mybatis,import,com,public
来源: https://www.cnblogs.com/xiaobaibailongma/p/16297804.html