1. 用户地址簿功能
1.1 需求分析
地址簿,指的是移动端消费者用户的地址信息,用户登录成功后可以维护自己的地址信息。同一个用户可以有多个地址信息,但是只能有一个默认地址。
对于地址簿管理,我们需要实现以下几个功能:
- 新增地址
- 地址列表查询
- 设置默认地址
- 编辑地址
- 删除地址
1.2 数据模型
用户的地址信息会存储在address_book表,即地址簿表中。具体表结构如下:
这里面有一个字段is_default,实际上我们在设置默认地址时,只需要更新这个字段就可以了。
1.3 导入功能代码
对于这一类的单表的增删改查,我们已经写过很多了,基本的开发思路都是一样的,那么本小节的用户地址簿管理的增删改查功能,我们就不再一一实现了,基本的代码我们都已经提供了,直接导入进来,做一个测试即可。
对于下面的地址管理的代码,我们可以直接从资料拷贝,也可以直接从下面的讲义中复制。
1). 实体类 AddressBook(直接从课程资料中导入即可)
所属包: com.itheima.reggie.entity
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">annotation</span>.<spanstyle="color:#000000">FieldFill</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">annotation</span>.<spanstyle="color:#000000">TableField</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">lombok</span>.<spanstyle="color:#000000">Data</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">java</span>.<spanstyle="color:#000000">io</span>.<spanstyle="color:#000000">Serializable</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">java</span>.<spanstyle="color:#000000">time</span>.<spanstyle="color:#000000">LocalDateTime</span>; <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 地址簿</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span><spanstyle="color:#770088">public</span><spanstyle="color:#770088">class</span><spanstyle="color:#0000ff">AddressBook</span><spanstyle="color:#770088">implements</span><spanstyle="color:#000000">Serializable</span> { <spanstyle="color:#770088">private</span><spanstyle="color:#770088">static</span><spanstyle="color:#770088">final</span><spanstyle="color:#008855">long</span><spanstyle="color:#000000">serialVersionUID</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#116644">1L</span>; <spanstyle="color:#770088">private</span><spanstyle="color:#008855">Long</span><spanstyle="color:#000000">id</span>; <spanstyle="color:#aa5500">//用户id</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">Long</span><spanstyle="color:#000000">userId</span>; <spanstyle="color:#aa5500">//收货人</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">consignee</span>; <spanstyle="color:#aa5500">//手机号</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">phone</span>; <spanstyle="color:#aa5500">//性别 0 女 1 男</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">sex</span>; <spanstyle="color:#aa5500">//省级区划编号</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">provinceCode</span>; <spanstyle="color:#aa5500">//省级名称</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">provinceName</span>; <spanstyle="color:#aa5500">//市级区划编号</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">cityCode</span>; <spanstyle="color:#aa5500">//市级名称</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">cityName</span>; <spanstyle="color:#aa5500">//区级区划编号</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">districtCode</span>; <spanstyle="color:#aa5500">//区级名称</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">districtName</span>; <spanstyle="color:#aa5500">//详细地址</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">detail</span>; <spanstyle="color:#aa5500">//标签</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">String</span><spanstyle="color:#000000">label</span>; <spanstyle="color:#aa5500">//是否默认 0 否 1是</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">Integer</span><spanstyle="color:#000000">isDefault</span>; <spanstyle="color:#aa5500">//创建时间</span><spanstyle="color:#555555"></span>(<spanstyle="color:#000000">fill</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">FieldFill</span>.<spanstyle="color:#000000">INSERT</span>) <spanstyle="color:#770088">private</span><spanstyle="color:#000000">LocalDateTime</span><spanstyle="color:#000000">createTime</span>; <spanstyle="color:#aa5500">//更新时间</span><spanstyle="color:#555555"></span>(<spanstyle="color:#000000">fill</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">FieldFill</span>.<spanstyle="color:#000000">INSERT_UPDATE</span>) <spanstyle="color:#770088">private</span><spanstyle="color:#000000">LocalDateTime</span><spanstyle="color:#000000">updateTime</span>; <spanstyle="color:#aa5500">//创建人</span><spanstyle="color:#555555"></span>(<spanstyle="color:#000000">fill</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">FieldFill</span>.<spanstyle="color:#000000">INSERT</span>) <spanstyle="color:#770088">private</span><spanstyle="color:#008855">Long</span><spanstyle="color:#000000">createUser</span>; <spanstyle="color:#aa5500">//修改人</span><spanstyle="color:#555555"></span>(<spanstyle="color:#000000">fill</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">FieldFill</span>.<spanstyle="color:#000000">INSERT_UPDATE</span>) <spanstyle="color:#770088">private</span><spanstyle="color:#008855">Long</span><spanstyle="color:#000000">updateUser</span>; <spanstyle="color:#aa5500">//是否删除</span><spanstyle="color:#770088">private</span><spanstyle="color:#008855">Integer</span><spanstyle="color:#000000">isDeleted</span>; }</span></span>
2). Mapper接口 AddressBookMapper(直接从课程资料中导入即可)
所属包: com.itheima.reggie.mapper
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">core</span>.<spanstyle="color:#000000">mapper</span>.<spanstyle="color:#000000">BaseMapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">entity</span>.<spanstyle="color:#000000">AddressBook</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">apache</span>.<spanstyle="color:#000000">ibatis</span>.<spanstyle="color:#000000">annotations</span>.<spanstyle="color:#000000">Mapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">apache</span>.<spanstyle="color:#000000">ibatis</span>.<spanstyle="color:#000000">annotations</span>.<spanstyle="color:#000000">Select</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">java</span>.<spanstyle="color:#000000">util</span>.<spanstyle="color:#000000">List</span>; <spanstyle="color:#555555"></span><spanstyle="color:#770088">public</span><spanstyle="color:#770088">interface</span><spanstyle="color:#0000ff">AddressBookMapper</span><spanstyle="color:#770088">extends</span><spanstyle="color:#000000">BaseMapper</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span> { }</span></span>
2). Mapper接口 AddressBookMapper(直接从课程资料中导入即可)
所属包: com.itheima.reggie.mapper
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">core</span>.<spanstyle="color:#000000">mapper</span>.<spanstyle="color:#000000">BaseMapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">entity</span>.<spanstyle="color:#000000">AddressBook</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">apache</span>.<spanstyle="color:#000000">ibatis</span>.<spanstyle="color:#000000">annotations</span>.<spanstyle="color:#000000">Mapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">apache</span>.<spanstyle="color:#000000">ibatis</span>.<spanstyle="color:#000000">annotations</span>.<spanstyle="color:#000000">Select</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">java</span>.<spanstyle="color:#000000">util</span>.<spanstyle="color:#000000">List</span>; <spanstyle="color:#555555"></span><spanstyle="color:#770088">public</span><spanstyle="color:#770088">interface</span><spanstyle="color:#0000ff">AddressBookMapper</span><spanstyle="color:#770088">extends</span><spanstyle="color:#000000">BaseMapper</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span> { }</span></span>
3). 业务层接口 AddressBookService(直接从课程资料中导入即可)
所属包: com.itheima.reggie.service
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">extension</span>.<spanstyle="color:#000000">service</span>.<spanstyle="color:#000000">IService</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">entity</span>.<spanstyle="color:#000000">AddressBook</span>; <spanstyle="color:#770088">public</span><spanstyle="color:#770088">interface</span><spanstyle="color:#0000ff">AddressBookService</span><spanstyle="color:#770088">extends</span><spanstyle="color:#000000">IService</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span> { }</span></span>
4). 业务层实现类 AddressBookServiceImpl(直接从课程资料中导入即可)
所属包: com.itheima.reggie.service.impl
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">extension</span>.<spanstyle="color:#000000">service</span>.<spanstyle="color:#000000">impl</span>.<spanstyle="color:#000000">ServiceImpl</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">entity</span>.<spanstyle="color:#000000">AddressBook</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">mapper</span>.<spanstyle="color:#000000">AddressBookMapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">service</span>.<spanstyle="color:#000000">AddressBookService</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">springframework</span>.<spanstyle="color:#000000">stereotype</span>.<spanstyle="color:#000000">Service</span>; <spanstyle="color:#555555"></span><spanstyle="color:#770088">public</span><spanstyle="color:#770088">class</span><spanstyle="color:#0000ff">AddressBookServiceImpl</span><spanstyle="color:#770088">extends</span><spanstyle="color:#000000">ServiceImpl</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBookMapper</span>, <spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#770088">implements</span><spanstyle="color:#000000">AddressBookService</span> { }</span></span>
5). 控制层 AddressBookController(直接从课程资料中导入即可)
所属包: com.itheima.reggie.controller
controller主要开发的功能:
A. 新增地址逻辑说明:
- 需要记录当前是哪个用户的地址(关联当前登录用户)
B. 设置默认地址
- 每个用户可以有很多地址,但是默认地址只能有一个 ;
- 先将该用户所有地址的is_default更新为0 , 然后将当前的设置的默认地址的is_default设置为1
C. 根据ID查询地址
D. 查询默认地址
- 根据当前登录用户ID 以及 is_default进行查询,查询当前登录用户is_default为1的地址信息
E. 查询指定用户的全部地址
- 根据当前登录用户ID,查询所有的地址列表
代码实现如下:
<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">core</span>.<spanstyle="color:#000000">conditions</span>.<spanstyle="color:#000000">query</span>.<spanstyle="color:#000000">LambdaQueryWrapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">baomidou</span>.<spanstyle="color:#000000">mybatisplus</span>.<spanstyle="color:#000000">core</span>.<spanstyle="color:#000000">conditions</span>.<spanstyle="color:#000000">update</span>.<spanstyle="color:#000000">LambdaUpdateWrapper</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">common</span>.<spanstyle="color:#000000">BaseContext</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">common</span>.<spanstyle="color:#000000">R</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">entity</span>.<spanstyle="color:#000000">AddressBook</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">com</span>.<spanstyle="color:#000000">itheima</span>.<spanstyle="color:#000000">reggie</span>.<spanstyle="color:#000000">service</span>.<spanstyle="color:#000000">AddressBookService</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">lombok</span>.<spanstyle="color:#000000">extern</span>.<spanstyle="color:#000000">slf4j</span>.<spanstyle="color:#000000">Slf4j</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">springframework</span>.<spanstyle="color:#000000">beans</span>.<spanstyle="color:#000000">factory</span>.<spanstyle="color:#000000">annotation</span>.<spanstyle="color:#000000">Autowired</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">org</span>.<spanstyle="color:#000000">springframework</span>.<spanstyle="color:#000000">web</span>.<spanstyle="color:#000000">bind</span>.<spanstyle="color:#000000">annotation</span>.<spanstyle="color:#981a1a">*</span>; <spanstyle="color:#770088">import</span><spanstyle="color:#000000">java</span>.<spanstyle="color:#000000">util</span>.<spanstyle="color:#000000">List</span>; <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 地址簿管理</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span><spanstyle="color:#555555"></span><spanstyle="color:#555555"></span>(<spanstyle="color:#aa1111">"/addressBook"</span>) <spanstyle="color:#770088">public</span><spanstyle="color:#770088">class</span><spanstyle="color:#0000ff">AddressBookController</span> { <spanstyle="color:#555555"></span><spanstyle="color:#770088">private</span><spanstyle="color:#000000">AddressBookService</span><spanstyle="color:#000000">addressBookService</span>; <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 新增</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span><spanstyle="color:#770088">public</span><spanstyle="color:#000000">R</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">save</span>(<spanstyle="color:#555555"></span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#000000">addressBook</span>) { <spanstyle="color:#000000">addressBook</span>.<spanstyle="color:#000000">setUserId</span>(<spanstyle="color:#000000">BaseContext</span>.<spanstyle="color:#000000">getCurrentId</span>()); <spanstyle="color:#000000">log</span>.<spanstyle="color:#000000">info</span>(<spanstyle="color:#aa1111">"addressBook:{}"</span>, <spanstyle="color:#000000">addressBook</span>); <spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">save</span>(<spanstyle="color:#000000">addressBook</span>); <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">success</span>(<spanstyle="color:#000000">addressBook</span>); } <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 设置默认地址</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span>(<spanstyle="color:#aa1111">"default"</span>) <spanstyle="color:#770088">public</span><spanstyle="color:#000000">R</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">setDefault</span>(<spanstyle="color:#555555"></span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#000000">addressBook</span>) { <spanstyle="color:#000000">log</span>.<spanstyle="color:#000000">info</span>(<spanstyle="color:#aa1111">"addressBook:{}"</span>, <spanstyle="color:#000000">addressBook</span>); <spanstyle="color:#000000">LambdaUpdateWrapper</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">wrapper</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#770088">new</span><spanstyle="color:#000000">LambdaUpdateWrapper</span><spanstyle="color:#981a1a"><></span>(); <spanstyle="color:#000000">wrapper</span>.<spanstyle="color:#000000">eq</span>(<spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getUserId</span>, <spanstyle="color:#000000">BaseContext</span>.<spanstyle="color:#000000">getCurrentId</span>()); <spanstyle="color:#000000">wrapper</span>.<spanstyle="color:#000000">set</span>(<spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getIsDefault</span>, <spanstyle="color:#116644">0</span>); <spanstyle="color:#aa5500">//SQL:update address_book set is_default = 0 where user_id = ?</span><spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">update</span>(<spanstyle="color:#000000">wrapper</span>); <spanstyle="color:#000000">addressBook</span>.<spanstyle="color:#000000">setIsDefault</span>(<spanstyle="color:#116644">1</span>); <spanstyle="color:#aa5500">//SQL:update address_book set is_default = 1 where id = ?</span><spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">updateById</span>(<spanstyle="color:#000000">addressBook</span>); <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">success</span>(<spanstyle="color:#000000">addressBook</span>); } <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 根据id查询地址</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span>(<spanstyle="color:#aa1111">"/{id}"</span>) <spanstyle="color:#770088">public</span><spanstyle="color:#000000">R</span><spanstyle="color:#000000">get</span>(<spanstyle="color:#555555"></span><spanstyle="color:#008855">Long</span><spanstyle="color:#000000">id</span>) { <spanstyle="color:#000000">AddressBook</span><spanstyle="color:#000000">addressBook</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">getById</span>(<spanstyle="color:#000000">id</span>); <spanstyle="color:#770088">if</span> (<spanstyle="color:#000000">addressBook</span><spanstyle="color:#981a1a">!=</span><spanstyle="color:#221199">null</span>) { <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">success</span>(<spanstyle="color:#000000">addressBook</span>); } <spanstyle="color:#770088">else</span> { <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">error</span>(<spanstyle="color:#aa1111">"没有找到该对象"</span>); } } <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 查询默认地址</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span>(<spanstyle="color:#aa1111">"default"</span>) <spanstyle="color:#770088">public</span><spanstyle="color:#000000">R</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">getDefault</span>() { <spanstyle="color:#000000">LambdaQueryWrapper</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">queryWrapper</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#770088">new</span><spanstyle="color:#000000">LambdaQueryWrapper</span><spanstyle="color:#981a1a"><></span>(); <spanstyle="color:#000000">queryWrapper</span>.<spanstyle="color:#000000">eq</span>(<spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getUserId</span>, <spanstyle="color:#000000">BaseContext</span>.<spanstyle="color:#000000">getCurrentId</span>()); <spanstyle="color:#000000">queryWrapper</span>.<spanstyle="color:#000000">eq</span>(<spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getIsDefault</span>, <spanstyle="color:#116644">1</span>); <spanstyle="color:#aa5500">//SQL:select * from address_book where user_id = ? and is_default = 1</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#000000">addressBook</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">getOne</span>(<spanstyle="color:#000000">queryWrapper</span>); <spanstyle="color:#770088">if</span> (<spanstyle="color:#221199">null</span><spanstyle="color:#981a1a">==</span><spanstyle="color:#000000">addressBook</span>) { <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">error</span>(<spanstyle="color:#aa1111">"没有找到该对象"</span>); } <spanstyle="color:#770088">else</span> { <spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">success</span>(<spanstyle="color:#000000">addressBook</span>); } } <spanstyle="color:#aa5500">/**</span><span style="color:#aa5500">* 查询指定用户的全部地址</span><span style="color:#aa5500">*/</span><spanstyle="color:#555555"></span>(<spanstyle="color:#aa1111">"/list"</span>) <spanstyle="color:#770088">public</span><spanstyle="color:#000000">R</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">List</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">>></span><spanstyle="color:#000000">list</span>(<spanstyle="color:#000000">AddressBook</span><spanstyle="color:#000000">addressBook</span>) { <spanstyle="color:#000000">addressBook</span>.<spanstyle="color:#000000">setUserId</span>(<spanstyle="color:#000000">BaseContext</span>.<spanstyle="color:#000000">getCurrentId</span>()); <spanstyle="color:#000000">log</span>.<spanstyle="color:#000000">info</span>(<spanstyle="color:#aa1111">"addressBook:{}"</span>, <spanstyle="color:#000000">addressBook</span>); <spanstyle="color:#aa5500">//条件构造器</span><spanstyle="color:#000000">LambdaQueryWrapper</span><spanstyle="color:#981a1a"><</span><spanstyle="color:#000000">AddressBook</span><spanstyle="color:#981a1a">></span><spanstyle="color:#000000">queryWrapper</span><spanstyle="color:#981a1a">=</span><spanstyle="color:#770088">new</span><spanstyle="color:#000000">LambdaQueryWrapper</span><spanstyle="color:#981a1a"><></span>(); <spanstyle="color:#000000">queryWrapper</span>.<spanstyle="color:#000000">eq</span>(<spanstyle="color:#221199">null</span><spanstyle="color:#981a1a">!=</span><spanstyle="color:#000000">addressBook</span>.<spanstyle="color:#000000">getUserId</span>(), <spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getUserId</span>, <spanstyle="color:#000000">addressBook</span>.<spanstyle="color:#000000">getUserId</span>()); <spanstyle="color:#000000">queryWrapper</span>.<spanstyle="color:#000000">orderByDesc</span>(<spanstyle="color:#000000">AddressBook</span>::<spanstyle="color:#000000">getUpdateTime</span>); <spanstyle="color:#aa5500">//SQL:select * from address_book where user_id = ? order by update_time desc</span><spanstyle="color:#770088">return</span><spanstyle="color:#000000">R</span>.<spanstyle="color:#000000">success</span>(<spanstyle="color:#000000">addressBookService</span>.<spanstyle="color:#000000">list</span>(<spanstyle="color:#000000">queryWrapper</span>)); } }</span></span>
1.4 功能测试
代码导入进来,并且去阅读了一下地址管理各个功能的逻辑实现,接下来,我们就可以启动项目,进行一个测试。测试过程中,通过debug断点调试观察服务端程序的执行过程,在浏览器中使用调试工具查看页面和服务端的交互过程和请求响应数据。
1). 新增
填写表单数据,点击保存地址,查看网络请求。
2). 列表查询
当新增地址完成后,页面会再次发送一个请求,来查询该用户的所有地址列表,在界面进行展示。
3). 设置默认
在地址列表页面中,勾选 "设为默认地址" ,此时会发送PUT请求,来设置默认地址。
2. 菜品展示
2.1 需求分析
用户登录成功后跳转到系统首页,在首页需要根据分类来展示菜品和套餐。如果菜品设置了口味信息,需要展示
按钮,否则显示按钮。
2.2 前端页面分析
在开发代码之前,需要梳理一下前端页面和服务端的交互过程:
1). 页面(front/index.html)发送ajax请求,获取分类数据(菜品分类和套餐分类)
该功能在之前的业务开发中,我们都已经实现了。通过请求响应的数据,我们也可以看到数据是可以正确获取到的。
注意:首页加载时,不仅发送请求获取分类列表,还发送了一次ajax请求用于加载购物车数据,而这两次请求必须全部成功,页面才可以正常渲染,而当前购物车列表查询功能还未实现(报404),所以列表目前并未渲染。此处可以将这次请求的地址暂时修改一下,从静态json文件获取数据,等后续开发购物车功能时再修改回来,如下:
目前该部分的功能我们已经调通,左侧的分类菜单,和右侧的菜品信息我们都可以看到,后续我们只需要将购物车列表的数据改成调用服务端接口查询即可。