使用SSM实现一个简单的CRUD

x33g5p2x  于2022-03-11 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(560)

写在前面的话

​ SSM框架整合开发会大幅度的提高我们的开发效率。我这里准备写一套SSM整合之后实现的单表的CRUD的案例,希望给刚刚学习SSM框架的小伙伴一些帮助。

​ 也欢迎给位大神给出指点 _。

tips:后面有源码下载地址。

欧耶!直接开打吧

你需要准备的武器

  • 学习过Spring,SpringMVC和MyBatis框架
  • 会整合SSM框架,不会的可以参考我的这一篇文章:https://juejin.cn/post/7068889782637559816
  • 要有一份对技术不断追求的心
  • 要热爱我的民族,热爱我们的祖国_

酒就不开了,直接开整吧

搭建一个SSM框架整合的web项目。

在resources中添加数据源配置db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/bank?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
  3. jdbc.username=root
  4. jdbc.password=

[1]数据表实体类

准备一个数据表:

  1. -- 商品表 --
  2. create table item(
  3. item_id varchar(20) not null primary key,
  4. item_title varchar(300) not null,
  5. item_desc varchar(500) ,
  6. category_id int,
  7. create_time datetime not null,
  8. modify_time datetime not null,
  9. status int default '1'
  10. );
  11. -- 来个几条数据吧 --
  12. insert into item values('100010428315','Redmi K40 骁龙870 三星AMOLED 120Hz高刷直屏 4800万高清三摄 8GB+128GB 亮黑','【品质好物】4800万高清三摄相机,骁龙870【note10pro火热抢购中】查看>',1,'2022-02-26 15:26:23','2022-02-26 15:26:23',1);
  13. insert into item values('100013102509','荣耀Play5T Pro 6400万双摄 6.6英寸全视屏 22.5W超级快充 4000mAh大电池','【限时优惠400元】八核处理器,6400万超清双摄,配备22.5W超级快充~荣耀60SE新品上市查看>',1,'2022-02-26 15:26:23','2022-02-26 15:26:23',1);
  14. insert into item values('10031528454992','小米 红米 Redmi Note10 5G 游戏智能5G手机 新品Redmi手机 4G+128G月影银 官方标配','【小米直供|现货当天发】【低至949起】赠:90天碎屏险+钢化膜+品质音乐耳机+运费险!【Note11系列新品上市】查看>',1,'2022-02-26 15:26:23','2022-02-26 15:26:23',1);

根据数据库表,添加实体类:

  1. package com.qidian.demo.pojo;
  2. import java.util.Date;
  3. import java.io.Serializable;
  4. /**
  5. * @author 戴着假发的程序员
  6. */
  7. public class Item implements Serializable {
  8. private static final long serialVersionUID = -67170620160284587L;
  9. private String itemId;
  10. private String itemTitle;
  11. private String itemDesc;
  12. private Integer categoryId;
  13. private Date createTime;
  14. private Date modifyTime;
  15. private Integer status;
  16. // setter和getter我就省略了,太占空间了
  17. }

[2]Mapper开发

在你的mapper包中添加ItemMapper接口和ItemMaper.xml文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xSAtD0CO-1646889283988)(assets/1645863272197.png)]

ItemMapper接口内容:

  1. package com.qidian.demo.mapper;
  2. import com.qidian.demo.pojo.Item;
  3. import java.util.List;
  4. /**
  5. * @author 戴着假发的程序员
  6. */
  7. public interface ItemMapper {
  8. /**
  9. * 通过ID查询单条数据
  10. * @param itemId 主键
  11. * @return 实例对象
  12. */
  13. Item selectById(String itemId);
  14. /**
  15. * 查询全部
  16. * @return 对象列表
  17. */
  18. List<Item> selectAll();
  19. /**
  20. * 通过实体作为筛选条件查询
  21. * @param item 实例对象
  22. * @return 对象列表
  23. */
  24. List<Item> selectList(Item item);
  25. /**
  26. * 新增数据
  27. * @param item 实例对象
  28. * @return 影响行数
  29. */
  30. int insert(Item item);
  31. /**
  32. * 修改数据
  33. * @param item 实例对象
  34. * @return 影响行数
  35. */
  36. int update(Item item);
  37. /**
  38. * 通过主键删除数据
  39. * @param itemId 主键
  40. * @return 影响行数
  41. */
  42. int deleteById(String itemId);
  43. }

ItemMapper.xml文件内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.qidian.demo.mapper.ItemMapper">
  4. <!-- 结果集 -->
  5. <resultMap type="com.qidian.demo.pojo.Item" id="ItemMap">
  6. <result property="itemId" column="item_id" jdbcType="VARCHAR"/>
  7. <result property="itemTitle" column="item_title" jdbcType="VARCHAR"/>
  8. <result property="itemDesc" column="item_desc" jdbcType="VARCHAR"/>
  9. <result property="categoryId" column="category_id" jdbcType="INTEGER"/>
  10. <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
  11. <result property="modifyTime" column="modify_time" jdbcType="TIMESTAMP"/>
  12. <result property="status" column="status" jdbcType="INTEGER"/>
  13. </resultMap>
  14. <!-- 基本字段 -->
  15. <sql id="Base_Column_List">
  16. item_id, item_title, item_desc, category_id, create_time, modify_time, status </sql>
  17. <!-- 查询单个 -->
  18. <select id="selectById" resultMap="ItemMap">
  19. select
  20. <include refid="Base_Column_List" />
  21. from item
  22. where item_id = #{itemId}
  23. </select>
  24. <!-- 查询全部 -->
  25. <select id="selectAll" resultMap="ItemMap">
  26. select
  27. <include refid="Base_Column_List" />
  28. from item
  29. </select>
  30. <!--通过实体作为筛选条件查询-->
  31. <select id="selectList" resultMap="ItemMap">
  32. select
  33. <include refid="Base_Column_List" />
  34. from item
  35. <where>
  36. <if test="itemId != null and itemId != ''">
  37. and item_id = #{itemId}
  38. </if>
  39. <if test="itemTitle != null and itemTitle != ''">
  40. and item_title = #{itemTitle}
  41. </if>
  42. <if test="itemDesc != null and itemDesc != ''">
  43. and item_desc = #{itemDesc}
  44. </if>
  45. <if test="categoryId != null">
  46. and category_id = #{categoryId}
  47. </if>
  48. <if test="createTime != null">
  49. and create_time = #{createTime}
  50. </if>
  51. <if test="modifyTime != null">
  52. and modify_time = #{modifyTime}
  53. </if>
  54. <if test="status != null">
  55. and status = #{status}
  56. </if>
  57. </where>
  58. </select>
  59. <!-- 新增所有列 -->
  60. <insert id="insert" keyProperty="itemId" useGeneratedKeys="true">
  61. insert into item(item_id, item_title, item_desc, category_id, create_time, modify_time, status)
  62. values ( #{itemId}, #{itemTitle}, #{itemDesc}, #{categoryId}, #{createTime}, #{modifyTime}, #{status})
  63. </insert>
  64. <!-- 通过主键修改数据 -->
  65. <update id="update">
  66. update item
  67. <set>
  68. <if test="itemTitle != null and itemTitle != ''">
  69. item_title = #{itemTitle},
  70. </if>
  71. <if test="itemDesc != null and itemDesc != ''">
  72. item_desc = #{itemDesc},
  73. </if>
  74. <if test="categoryId != null">
  75. category_id = #{categoryId},
  76. </if>
  77. <if test="createTime != null">
  78. create_time = #{createTime},
  79. </if>
  80. <if test="modifyTime != null">
  81. modify_time = #{modifyTime},
  82. </if>
  83. <if test="status != null">
  84. status = #{status},
  85. </if>
  86. </set>
  87. where item_id = #{itemId}
  88. </update>
  89. <!--通过主键删除-->
  90. <delete id="deleteById">
  91. delete from item where item_id = #{itemId}
  92. </delete>
  93. </mapper>

[3]service开发

在你的service包中添加ItemService接口和ItemServicImpl实现类:

ItemService接口内容:

  1. package com.qidian.demo.service;
  2. import com.qidian.demo.pojo.Item;
  3. import java.util.List;
  4. /**
  5. * @author 戴着假发的程序员
  6. */
  7. public interface ItemService {
  8. /**
  9. * 通过ID查询单条数据
  10. * @param itemId 主键
  11. * @return 实例对象
  12. */
  13. Item selectById(String itemId);
  14. /**
  15. * 查询全部
  16. * @return 对象列表
  17. */
  18. List<Item> selectAll();
  19. /**
  20. * 通过实体作为筛选条件查询
  21. * @param item 实例对象
  22. * @return 对象列表
  23. */
  24. List<Item> selectList(Item item);
  25. /**
  26. * 新增数据
  27. * @param item 实例对象
  28. * @return 影响行数
  29. */
  30. int insert(Item item);
  31. /**
  32. * 修改数据
  33. * @param item 实例对象
  34. * @return 修改
  35. */
  36. Item update(Item item);
  37. /**
  38. * 通过主键删除数据
  39. * @param itemId 主键
  40. * @return 影响行数
  41. */
  42. int deleteById(String itemId);
  43. }

ItemServicImpl实现类内容:

  1. package com.qidian.demo.service.impl;
  2. import com.qidian.demo.pojo.Item;
  3. import com.qidian.demo.mapper.ItemMapper;
  4. import com.qidian.demo.service.ItemService;
  5. import org.springframework.stereotype.Service;
  6. import javax.annotation.Resource;
  7. import java.util.List;
  8. /**
  9. * @author 戴着假发的程序员
  10. */
  11. @Service("itemService")
  12. public class ItemServiceImpl implements ItemService {
  13. @Resource
  14. private ItemMapper itemMapper;
  15. /**
  16. * 通过ID查询单条数据
  17. * @param itemId 主键
  18. * @return 实例对象
  19. */
  20. @Override
  21. public Item selectById(String itemId) {
  22. return this.itemMapper.selectById(itemId);
  23. }
  24. /**
  25. * 查询所有
  26. * @return 实例对象的集合
  27. */
  28. @Override
  29. public List<Item> selectAll() {
  30. return this.itemMapper.selectAll();
  31. }
  32. /**
  33. * 根据条件查询
  34. * @return 实例对象的集合
  35. */
  36. @Override
  37. public List<Item> selectList(Item item) {
  38. return this.itemMapper.selectList(item);
  39. }
  40. /**
  41. * 新增数据
  42. * @param item 实例对象
  43. * @return 实例对象
  44. */
  45. @Override
  46. public int insert(Item item) {
  47. return this.itemMapper.insert(item);
  48. }
  49. /**
  50. * 修改数据
  51. * @param item 实例对象
  52. * @return 实例对象
  53. */
  54. @Override
  55. public Item update(Item item) {
  56. this.itemMapper.update(item);
  57. return this.selectById(item.getItemId());
  58. }
  59. /**
  60. * 通过主键删除数据
  61. * @param itemId 主键
  62. * @return 是否成功
  63. */
  64. @Override
  65. public int deleteById(String itemId) {
  66. return this.itemMapper.deleteById(itemId);
  67. }
  68. }

[4]controller开发

在你的controller包中添加ItemController

ItemController内容如下:

  1. package com.qidian.demo.controller;
  2. import com.qidian.demo.pojo.Item;
  3. import com.qidian.demo.service.ItemService;
  4. import org.springframework.web.bind.annotation.*;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.HashMap;
  8. import javax.annotation.Resource;
  9. /**
  10. * @author 戴着假发的程序员
  11. */
  12. @RestController
  13. @RequestMapping("/item")
  14. public class ItemController {
  15. @Resource
  16. private ItemService itemService;
  17. /**
  18. * @param item 参数对象
  19. * @return 单条数据
  20. */
  21. @RequestMapping(value = "get", method = RequestMethod.GET)
  22. public Item selectOne(Item item) {
  23. Item result = itemService.selectById(item.getItemId());
  24. if(result != null){
  25. return result;
  26. }
  27. return null;
  28. }
  29. /**
  30. * 新增一条数据
  31. * @param item 实体类
  32. * @return Map
  33. */
  34. @RequestMapping(value = "insert", method = RequestMethod.POST)
  35. public Map insert(Item item) {
  36. Map map = new HashMap();
  37. map.put("code",200);
  38. int result = itemService.insert(item);
  39. if (result <= 0) {
  40. map.put("code",500);
  41. }
  42. return map;
  43. }
  44. /**
  45. * 修改一条数据
  46. * @param item 实体类
  47. * @return Map
  48. */
  49. @RequestMapping(value = "update")
  50. public Map update(Item item) {
  51. Map map = new HashMap();
  52. map.put("code",200);
  53. Item result = itemService.update(item);
  54. if (result != null) {
  55. map.put("data",result);
  56. }else{
  57. map.put("code",500);
  58. }
  59. return map;
  60. }
  61. /**
  62. * 删除一条数据
  63. * @param item 参数对象
  64. * @return Map
  65. */
  66. @RequestMapping(value = "delete")
  67. public Map delete(Item item) {
  68. Map map = new HashMap();
  69. map.put("code",200);
  70. int result = itemService.deleteById(item.getItemId());
  71. if (result <= 0) {
  72. map.put("code",500);
  73. }
  74. return map;
  75. }
  76. /**
  77. * 查询全部
  78. * @return Response对象
  79. */
  80. @RequestMapping(value = "selectAll")
  81. public List<Item> selectAll() {
  82. return itemService.selectAll();
  83. }
  84. }

OK可以测试接口了:

接口参数返回值说明
/item/selectAllitem列表返回所有item列表
/item/insertitem的所有列{code:xx,msg:xx}添加item
/item/deleteitem_id{code:xx,msg:xx}根据id删除item
/item/updateitem所有列{code:xx,msg:xx}编辑item信息
/item/getitem_id一个item对象根据id查询item信息

当然上面的案例只是一个简单的单表的CRUD,还有太多的细节没有处理,所以这个DEMO就是为了熟练SSM框架整合的基本操作。

后面我会更新完整的项目开发教程。多谢关注。

源码下载: https://gitee.com/win-dk/ssm-crud

相关文章