Hi, It's Me! Irina Lu

wenjia lu

Wenjia's Tech Blog

GET IN TOUCH

Contact Info description

数据库的那些事儿

pojo 实体类

mapper。xml有问题:

Q:Dict_id 自动顺序增长,而不是自己设的值。

A:

为啥发出请求加东西,实际却没有加到数据库呢?因为我adddic 函数还没写昂。

接下来,做删除功能。

遇到过的错误 Q & A:

There is no getter for property named ‘dict_uuid’ in ‘class com.jj.red.packet.user.mybatis.domain.RoundDict’

There is no getter for property named ‘dict_key’ in ‘class com.jj.red.packet.user.mybatis.domain.RoundDict’

DEBUG [com.jj.red.packet.user.mybatis.persistence.RoundDictMapper.insertSelective] - ==> Preparing: insert into t_round_dict ( dictUuid, dictKey, dictDetail ) values ( ?, ?, ? )

Q:增和删,mapper都返回1,能改吗?

[com.jj.red.packet.user.mybatis.persistence.RoundDictMapper.insert] - <== Updates: 1

A: resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型 –>

<select id="selectPersonCount" resultType="java.lang.Integer">

Q: list 为啥没有返回items;

<select id="selectAll" parameterType="com.jj.red.packet.user.mybatis.domain.RoundDictExample" resultMap="BaseResultMap">

RoundDictExample example

验证dict_id不能为null,不然抛出异常

java.lang.RuntimeException: Value for dict_id cannot be null
at com.jj.red.packet.user.mybatis.domain.RoundDictExample$GeneratedCriteria.addCriterion(RoundDictExample.java:95)
at com.jj.red.packet.user.mybatis.domain.RoundDictExample$GeneratedCriteria.andDictIdEqualTo(RoundDictExample.java:118)
at com.jj.red.packet.user.mybatis.domain.RoundDictExample$Criteria.andDictIdEqualTo(RoundDictExample.java:763)
at com.jj.red.packet.user.service.impl.RoundDictServiceImpl.getDicts(RoundDictServiceImpl.java:55)

Q:Unknown column ‘xxx’ in ‘field list’

A:数据表中字段与实体类中声明的字段对象没有用注解连接。

或者 将两个名字改成一致的

Q:不知道哪个是对应数据库的键,哪个对应实体类的键

A:

<!--  ADD -->
<insert id="insertSelective" parameterType="com.jj.red.packet.user.mybatis.domain.RoundDict">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_round_dict
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="dictId != null">
dictId,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dictId != null">
#{dictId,jdbcType=VARCHAR},
<!-- 取值方式1:#{value jdbcType = valuetype}:jdbcType 表示该属性的数据类型在数据库中对应的类型,如 #{user jdbcType=varchar} 等价于 String username;-->
</if>
</trim>
</insert>

tips;UUID : VARCHAR(36)类型

终端打印出来的顺序;

ApplicationContext –> ContextLoader –> ServletREgistrationBean –> dispatcherServlet –> RequestMappingHandlerMapping –> Mapped (/m/add method=[POST]) to. ResponseMessage –> xxController.add ; url path [/v2/api] to SwaggerController.getDocument –> … —> tomcat started on port xxx

LIST

Controller:
Page<RoundDict> roundDicts = roundDictService.getDicts(roundDictRequest.getDictId(),roundDictRequest.getDictUuid());

Controller: roundDictService .getDicts –> roundDictServiceImpl .getDicts( roundDictMapper.selectByExample(example) ) –> mappler. selectByexample–> mapper.xml id = selectByexample

快看原来返回的是什么模型就已经告诉我了

list :

==list

Page<RoundDict> roundDicts = roundDictService.getDicts(roundDictRequest.getDictId(),roundDictRequest.getDictUuid());

return this.setSysData(roundDicts);

roundDictService.getDicts( ) –>

Page roundDicts –> Page?? –> setSysData -> coreController.setSysData.setItem. –> page data.getResult –> PageData.setItems –> ResponseMessage.setPageDate

setSysData(Page data形参) == setSysData(Page<RoundDict> roundDicts实参)
page.getResult return this
PageData.setItems( page data.getResult) == PageData.setItems( Page<RoundDict> roundDicts.getResult )

page类 :setStart

SysPageBean : setItems

pageBean: setItems

Corecontroller: roundDicts 需要有一个 getItems()方法

protected ResponseMessage<SysPageBean> setSysData(PageBean **data**) {
ResponseMessage<SysPageBean> responseMessage = new ResponseMessage<>();
SysPageBean pageData= new SysPageBean();
pageData.setTotal(Long.parseLong(data.getTotalNum() + ""));

pageData.setItems(**data**.getItems());

responseMessage.setData(pageData);
return responseMessage;
}

注解 for swagger

@RequestMapping(value = AdminRestUrl.ADD , method = RequestMethod.POST)
// 返回被@ApiModel标注的类对象。 啊哈哈这个会显示到swagger页面的这个接口最顶上。
@ApiOperation(value = "实体类响应",notes = "返回数据为实体类的接口")
//这个可能只对map生效哦,不过不确定
@ApiImplicitParam(name = "user", value = "用户详细信息User", required = true,dataType = "User")
public ResponseMessage<Integer> add(@RequestBody RoundDict roundDict) {

常用注解:https://github.com/Mshuyan/swagger

https://blog.csdn.net/weixin_44906271/article/details/105792809

=====add

为什么insert、delete、update语句的返回值类型是int?

有JDBC操作经验的朋友可能会有印象,增删改操作实际上返回的是操作的条数。而Mybatis框架本身是基于JDBC的,所以此处也沿袭这种返回值类型。

返回模型:ResponseMessage«Map» {

resultStatus (code:1000,message:成功) :getStatus,getcode/Message

自定义(getDictId)

return map(code,message)

–>

ResponseMessage( ) :ResponseMessage( resultStatus.自定义(getDictId) ) getData()

return map( code,message, dictId, dictum ….)

——》

Controller ()

Return ResponseMessage

responsebody?

\

发现我 “roomId”: 0, “userId”: 0 已经成功删掉了,唯有action还很坚挺,可能在父类里面定义了。

资料:

MyBatis

mapper.xml中常用的标签详解:https://blog.csdn.net/qq_29233973/article/details/51433924

https://www.cnblogs.com/aichiboluo/p/8962529.html

深入解析mapper.xml: https://lanlan2017.github.io/JavaReadingNotes/f097c266/

Mapper.xml 中的sql写法:https://blog.csdn.net/weixin_43925626/article/details/84940475?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control

Mapper.xml 中的sql写法:https://blog.csdn.net/weixin_30892037/article/details/97550213?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

sql 返回数据:https://www.cnblogs.com/ysySelf/p/11223971.html