当前位置: 首页 > news >正文

企业交易平台的网站制作多少钱如何做网站地图视频

企业交易平台的网站制作多少钱,如何做网站地图视频,怎么做卖保险的网站,黄骅市属于沧州市吗接上篇 使用MyBatis配置Mapper实现增删改查 1.Service的基本作用 Service在代码中的的作用是调用Mapper、被Controller调用。是后端项目中非常重要的组件。 用于设计业务流程、业务逻辑,以保障数据的完整性、有效性、安全性。 2. Service使用举例——“添加相册”…

接上篇 使用MyBatis配置Mapper实现增删改查

1.Service的基本作用

Service在代码中的的作用是调用Mapper、被Controller调用。是后端项目中非常重要的组件。
用于设计业务流程、业务逻辑,以保障数据的完整性、有效性、安全性。

2. Service使用举例——“添加相册”

在项目的根包下创建pojo.dto.AlbumAddNewDTO类,用于封装业务方法所需的参数:

@Data
public class AlbumAddNewDTO implements Serializable {private String name;private String description;private Integer sort;
}

再在项目的根包下创建service.IAlbumService接口,并在接口添加“添加相册”的抽象方法:

public interface IAlbumService {void addNew(AlbumAddNewDTO albumAddNewDTO);
}

再在项目的根包下创建service.impl.AlbumServiceImpl类,实现以上接口,重写接口中声明的抽象方法,具体的实现,应该是:

package com.luoyang.small.service.impl;import com.luoyang.small.mapper.AlbumMapper;
import com.luoyang.small.pojo.dto.AlbumAddNewDTO;
import com.luoyang.small.pojo.entity.Album;
import com.luoyang.small.service.IAlbumService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** 接口实现** @author luoyang* @Date 2023/12/12*/
// 添加在类上,标记当前类是业务逻辑组件类,用法同@Component
@Service
public class IAlbumServiceImpl implements IAlbumService {/*** 添加在属性上,使得Spring自动装配此属性的值* 添加在构造方法上,使得Spring自动调用此构造方法* 添加在Setter方法上,使得Spring自动调用此方法*/@Autowiredprivate AlbumMapper albumMapper;@Overridepublic void addNew(AlbumAddNewDTO albumAddNewDTO) {//检查相册名称是否占用String name = albumAddNewDTO.getName();int countByName = albumMapper.countByName(name);//如果数据已存在还继续插入,我们这边直接报异常,不添加。if (countByName > 0) {throw new RuntimeException();}//创建Album对象Album album = new Album();//复制属性到albumBeanUtils.copyProperties(albumAddNewDTO, album);//执行插入数据albumMapper.insert(album);}
}
以上实现中 不要忘记两个注解@Service @Autowired

@Service添加在类上,标记当前类是业务逻辑组件类

@Autowired 添加在属性上,使得Spring自动装配此属性的值; 添加在构造方法上,使得Spring自动调用此构造方法; 添加在Setter方法上,使得Spring自动调用此方法

以上实现中 countByName为计数——新增数据前检查是否数据已存在

在实现以上业务之前,需要在Mapper中补充功能,用于检查相册名称是否已经被占用,
这边以查看数据库中相册名是否存在为检查方式,需要执行的SQL语句大致是:

#查看当前相册名数量
select count(*) from pms_album where name=?

AlbumMapper.java接口中添加抽象方法:

/*** 根据相册名称统计相册数据的数量* @param name 相册名称* @return 匹配相册名称的相册数据的数量*/
int countByName(String name);

并在AlbumMapper.xml中配置SQL语句:

<!-- int countByName(String name); -->
<select id="countByName" resultType="int">SELECT count(*) FROM pms_album WHERE name=#{name}
</select>

完成后,在AlbumMapperTests中编写并执行测试:

package com.luoyang.small;import com.luoyang.small.mapper.AlbumMapper;
import com.luoyang.small.pojo.entity.Album;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;/*** @author luoyang* @date 2023/11/28*/
@Slf4j
@SpringBootTest
public class AlbumMapperTests {@AutowiredAlbumMapper mapper;@Testvoid insert() {Album album = new Album();album.setName("测试名称001");album.setDescription("测试简介001l啦啦啦啦啦");album.setSort(100); // 注意:由于MySQL中表设计的限制,此值只能是[0,255]区间内的int rows = mapper.insert(album);System.out.println("插入数据完成,受影响的行数:" + rows);}@Testvoid countByName(){String name = "测试名称001";int count = mapper.countByName(name);log.debug("根据名称【{}】统计梳理完成,结果:{}",name,count);}}

测试检查数据库已有名称的数量:1,表示数据已存在;
如果数据已存在还继续插入,我们这边直接报异常,不添加。
在这里插入图片描述

3.Service调用效果展示——编写测试类

完成后,在src/test/java的根包下创建service.AlbumServiceTests测试类*(其中新建service文件夹,相对于src/main/java就不需要导包了)*
在此类中编写并执行测试,如下:

package com.luoyang.small.service;import com.luoyang.small.pojo.dto.AlbumAddNewDTO;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;/*** @author luoyang* @Date 2023/12/12*/
@Slf4j
@SpringBootTest
public class AlbumServiceTests {//不建议声明为实现类型@AutowiredIAlbumService iAlbumService;@Testvoid addNew() {AlbumAddNewDTO albumAddNewDTO = new AlbumAddNewDTO();albumAddNewDTO.setName("测试名称004");albumAddNewDTO.setDescription("测试简介004啦啦啦啦啦");albumAddNewDTO.setSort(100); // 注意:由于MySQL中表设计的限制,此值只能是[0,255]区间内的try {iAlbumService.addNew(albumAddNewDTO);log.debug("添加相册成功!");} catch (Exception e) {log.debug("添加相册失败,{}", e.getMessage());}}
}

在这里插入图片描述
在这里插入图片描述

声明:部分代码来自有网络,文章只供学习参考
创造价值,乐哉分享!

http://www.15wanjia.com/news/190641.html

相关文章:

  • php网站开发实训指导书建设工程+质量+协会网站
  • 做相册集什么网站WordPress引用阿里云矢量图
  • 昆山专业简历制作网站2017年网站开发用什么语言
  • 网站开发中网页之间的连接形式有便宜网站建设 优帮云
  • 哪个网站可以做任务手机建立网站application
  • 网站标签怎么做天猫店
  • 四川专业网站建设推广中企动力深圳分公司
  • 如何快速进行网站开发做网站排版
  • 如何自己搭建一个个人网站建设网络平台
  • 谁会在西安做网站的吗建站如何收费
  • 哈尔滨队网站网页美工学习吧网站
  • 商业网站设计专业wordpress支持代码高亮
  • 作品展示网站模板高端网站建设公司排行
  • 校园网站开发的需求分析网页建设多少钱
  • 建设物业公司网站行业门户网站 建站
  • 网站建设费用预算表wordpress的主题mnews1.9
  • 河津网站制作wordpress和帝国哪个好
  • 抖音点赞自助网站网站会员注册系统
  • 网站的功能板块头条搜索是百度引擎吗
  • 网站开站js网站文字重叠
  • 网站404页面做晚了三只松鼠电商网站建设
  • 网站着陆页怎么做惠州市建设规划局网站
  • 张掖市建设规划局网站网页设计适合什么岗位
  • 网站一次性建设室内设计师多少钱一个月
  • 做创意礼品的网站西安网页设计
  • 南昌做网站市场报价简单的网站建设公司的模板下载
  • awds网站开发留学wordpress 速度太慢
  • 电子商务网站开发是什么微商城开发用华网天下首选
  • 外围网站怎么做智能建筑网站
  • pythom+网站开发规范官网seo