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.Insert; 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); @Insert("insert into users(id,name,age) values(#{id},#{name},#{age})") public int insert(MyUser user); @Select("select * from users where id = #{id}") public MyUser queryAll(int id); }
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); int insertUser(MyUser user); MyUser queryFindAll(int id); }
实现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; } @Override public int insertUser(MyUser user) { int row = userMapper.insert(user); return row; } @Override public MyUser queryFindAll(int id) { return userMapper.queryAll(id); } }
新增一个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; import java.util.List; @RestController public class FindALLController { @Autowired private UserService userService; @GetMapping(value = "/8") public MyUser home8(@RequestParam("id") int id) { MyUser user = userService.queryFindAll(id); return user; } }
http://localhost:8080/8?id=1001