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> <insert id="addUser" parameterType="com.test.entity.MyUser"> insert into users(id,name,age) values ( #{id},#{name},#{age} ) </insert> <delete id="delUser" parameterType="String"> delete from users where name = #{name} </delete> </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); public boolean addUser( MyUser user); public boolean delUser ( String name); }
service接口:
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); boolean addUserAdd(MyUser user); boolean delUserByName(String name); }
实现service接口:
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; } @Override public boolean addUserAdd(MyUser user) { boolean flag = false; flag = userMapper.addUser(user); System.out.println(flag); return flag; } @Override public boolean delUserByName(String name) { boolean flag = false; flag = userMapper.delUser(name); 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.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class DelUserController { @Autowired private UserService userService; @GetMapping(value = "/6") public String home6(@RequestParam("name") String name) { boolean flagax = userService.delUserByName(name); if (flagax == true) { return "cheng----gong----deluser"; } else { return "cuo---wu----deluser"; } } }
http://localhost:8080/6?name=uuuuuuu
标签:name,spring,boot,id,boolean,MyUser,mybatis,import,public 来源: https://www.cnblogs.com/xiaobaibailongma/p/16298064.html