SpringBoot之整合Mybatis(增,改,删)
2018-09-18 06:39:15来源:博客园 阅读 ()
一,在上一篇文章SpringBoot之整合Mybatis中,我们使用spring boot整合了Mybatis,并演示了查询操作。接下来我们将完善这个示例,增加增,删,改的功能。
二,改动代码
1.修改UserMapper.xml,修改之后的代码:
<?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.zcz.dao.UserDao"> <sql id="base_column_list"> id,name </sql> <select id="findAllUser" resultType="com.zcz.entity.User"> select id,name from user </select> <select id="findUserById" resultType="com.zcz.entity.User"> select <include refid="base_column_list"></include> from user where id = #{id} </select> <update id="updateUserById" parameterType="com.zcz.entity.User"> update user set name = #{name} where id = #{id} </update> <insert id="insertUser" parameterType="com.zcz.entity.User"> insert into user(name) values (#{name}) </insert> <delete id="deleteById" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> </mapper>
2.修改UserDao ,修改后的代码:
package com.zcz.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.zcz.entity.User; @Mapper public interface UserDao { public List<User> findAllUser(); public User findUserById(int id); public boolean updateUserById(User user); public boolean insertUser(User user); public boolean deleteById(int id); }
3.修改UserService和UserServiceImpl
UserService代码如下:
package com.zcz.service; import java.util.List; import com.zcz.entity.User; public interface UserService { public List<User> getUsers(); public User findUserById(int id); public boolean updateUserById(User user); public boolean insertUser(User user); public boolean deleteById(int id); }
UserServiceImpl代码如下:
package com.zcz.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zcz.dao.UserDao; import com.zcz.entity.User; import com.zcz.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> getUsers() { // TODO Auto-generated method stub return userDao.findAllUser(); } @Override public User findUserById(int id) { // TODO Auto-generated method stub return userDao.findUserById(id); } @Override public boolean updateUserById(User user) { // TODO Auto-generated method stub userDao.updateUserById(user); return true; } @Override public boolean insertUser(User user) { // TODO Auto-generated method stub userDao.insertUser(user); return true; } @Override public boolean deleteById(int id) { // TODO Auto-generated method stub userDao.deleteById(id); return true; } }
4.修改UserController.代码如下
package com.zcz.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zcz.entity.User; import com.zcz.service.UserService; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") public List<User> getUsers(){ return userService.getUsers(); } @RequestMapping("/addUser") public void addUser(HttpServletRequest request) { String name = request.getParameter("name"); User user = new User(); user.setName(name); userService.insertUser(user); } @RequestMapping("/updateUser") public void updateUser(HttpServletRequest request) { String name = request.getParameter("name"); int id = Integer.parseInt(request.getParameter("id")); User user = new User(); user.setName(name); user.setId(id); userService.updateUserById(user); } @RequestMapping("/getUserOne") public User getUserOne(HttpServletRequest request) { int id = Integer.parseInt(request.getParameter("id")); return userService.findUserById(id); } @RequestMapping("/deleteUser") public void deleteUser(HttpServletRequest request) { int id = Integer.parseInt(request.getParameter("id")); userService.deleteById(id); } }
三,接下来就是测试
首先看一下数据库的数据:
1,测试新增,访问:http://localhost:8080/user/addUser?name=zhangchengzi
成功
2,测试更新,访问:http://localhost:8080/user/updateUser?id=6&name=zhangchengzilala
成功
3.测试更具id查询,访问:http://localhost:8080/user/getUserOne?id=6
成功
4,测试删除,访问:http://localhost:8080/user/deleteUser?id=6
成功
5.我把源代码上传到了github仓库:https://github.com/ZCC1/learnSpringBootWithMybatis2.git。可以clone下来参考。欢迎star。
好了,这样以来,Spring Boot整合Mybatis,常用的操作,增,删,改,查全部都实现了。
原创不易,转载请注明出处:https://www.cnblogs.com/zhangchengzi/p/9662639.html
更多文章,请查看目录
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash