前言

  • 我的所有博客操作均有相应截图,所以只要跟着做,一定是能跑通的。

本篇讲到了在后台实现数据库的操作,涉及的数据的增删改查,以及一些框架自带函数的初步使用。如果没有配置好SSM框架,那就赏脸看一下
https://blog.csdn.net/babybabyup/article/details/79687097 配置篇呗!

工具

  • 本机mysql数据库(database=helloword,table=user)
  • IDEA

具体操作

自动生成数据库函数

  • 打开IDEA左栏中的Maven project->helloworld Maven WebAPP->Plugins->mybatis-generator 双击出现的mybatieos-generator:generator

SSM框架中实现数据库操作-从零学习SSM框架(3)

等待其完成后,控制台输出
SSM框架中实现数据库操作-从零学习SSM框架(3)

并且生成相应的函数以及实体代码,dao层接口

SSM框架中实现数据库操作-从零学习SSM框架(3)
dao 层接口中,mybatis 生成了六个函数接口,分别是

int deleteByPrimaryKey(Integer id);  int insert(User record);  int insertSelective(User record);  User selectByPrimaryKey(Integer id);  int updateByPrimaryKeySelective(User record);  int updateByPrimaryKey(User record);

解释一下各个函数的意义吧

  • deleteByPrimaryKey(Integer id) :通过主键id在数据库中删除一条数据,成功则返回1,失败则返回0;
  • insert(User record) :插入数据,要注意如果数据库中的属性不能为空,那么对象记录record的所有属性一定要全部不为null,否则会插入失败。
  • insertSelective(User record) :
  • selectByPrimaryKey(Integer id) :通过主键id进行查找,查找到了则返回实体User,也就是一个对象,数据库中没有此条记录,则返回null;
  • updateByPrimaryKeySelective(User record):更新某条 User 记录,但是如果record某些属性为空,则不会用空值去代替已存在的属性,即只是更新record设置的属性。
  • updateByPrimaryKey(User record):不考虑更新的数据如何,将User属性全部更新。

任何函数都可以进行单元测试,挑updateByPrimaryKeySelective(User record)updateByPrimaryKey(User record) 比较一下吧。
首先在test数据库user数据库中插入一条数据。如图:
SSM框架中实现数据库操作-从零学习SSM框架(3)
然后生成这两个函数的测试,具体方法可以参考https://blog.csdn.net/babybabyup/article/details/79687097

package com.springmvc.dao;  import com.springmvc.entity.User; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;   public class UserMapperTest {       @Autowired     UserMapper userMapper;     ApplicationContext applicationContext;       @Before     public void setUp() throws Exception {         applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");         userMapper = applicationContext.getBean(UserMapper.class);      }      @After     public void tearDown() throws Exception {     }      @Test     public void updateByPrimaryKeySelective() throws Exception {         User user = new User();         user.setId(1);         user.setName("James");        System.out.println(userMapper.updateByPrimaryKeySelective(user));     }      @Test     public void updateByPrimaryKey() throws Exception {              } }

不去设置sex属性则不会将sex更新,运行后的数据库变为
SSM框架中实现数据库操作-从零学习SSM框架(3)
那么再看一下,另一个函数到底是什么结果,在测试类新增为:

@Test     public void updateByPrimaryKey() throws Exception {         User user = new User();         user.setId(1);         user.setSex("female");         System.out.println(userMapper.updateByPrimaryKey(user));       }

不设置name属性,更新后所有的属性都更新了,包括空属性
SSM框架中实现数据库操作-从零学习SSM框架(3)

从前端进行数据库存储

index.jsp中新建一个form进行数据传输

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %>       Index        这个是index页面
新增代码
id:
姓名:
性别:
 

IndexController.java中,新添接收代码

package com.springmvc.controller;   import com.springmvc.entity.User; import com.springmvc.services.UserServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;    @Controller public class IndexController {      @Autowired     private UserServices userServices;     @RequestMapping(value = "/test2", method = RequestMethod.POST)     public String test2(@ModelAttribute User user, Model model) {         userServices.insert(user);         model.addAttribute("end","插入成功");         return "test1";     }  }

运行成功后,再次刷新数据库,则已经新添了一行数据
SSM框架中实现数据库操作-从零学习SSM框架(3)
这样,基本的操作已经完全OK了,继续后台开发的笔记。