前言
- 我的所有博客操作均有相应截图,所以只要跟着做,一定是能跑通的。
本篇讲到了在后台实现数据库的操作,涉及的数据的增删改查,以及一些框架自带函数的初步使用。如果没有配置好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
等待其完成后,控制台输出
并且生成相应的函数以及实体代码,dao层接口
在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;
有时间更新。。。