mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上使用@Insert注解插入一条数据
作者:互联网
POJO类不变
使用@Insert注解后,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); }
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); }
实现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; } }
新增一个控制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 InsertController { @Autowired private UserService userService; @GetMapping(value = "/7") public int home7(MyUser user) { int rowax = userService.insertUser(user); return rowax; } }
http://localhost:8080/7?id=1001&name=www&age=999
标签:Insert,name,MyUser,id,user,spring,mybatis,import,public 来源: https://www.cnblogs.com/xiaobaibailongma/p/16298169.html