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

全国网站建设哪家专业线上营销活动有哪些

全国网站建设哪家专业,线上营销活动有哪些,日本樱花云服务器wan,江西省赣州市邮政编码文章目录1. 修改操作1.1 在 mapper&#xff08;interface&#xff09;里面添加修改方法的声明1.2 在 XMl 中添加 <update> 标签和修改的 sql 代码1.3 在 UserMapper 中右键 Generate 点击 Test 生成 update 测试类2. 删除操作2.1 在 mapper &#xff08;interface&#x…

文章目录

  • 1. 修改操作
    • 1.1 在 mapper(interface)里面添加修改方法的声明
    • 1.2 在 XMl 中添加 <update> 标签和修改的 sql 代码
    • 1.3 在 UserMapper 中右键 Generate 点击 Test 生成 update 测试类
  • 2. 删除操作
    • 2.1 在 mapper (interface)中添加删除的代码声明
    • 2.2 在 XMl 中添加 <delete> 标签和删除的 sql 代码
    • 2.3 在 UserMapper 中右键 Generate 点击 Test 生成 delete 测试类
  • 3. 添加操作
    • 3.1 添加用户,返回受影响的行数
      • 3.1.1 在 mapper(interface)添加方法声明
      • 3.1.2 在 XML 中添加<insert>标签和添加的 sql 代码
      • 3.1.3 生成测试类
    • 3.2 添加用户,返回自增 id
      • 3.2.1 添加方法声明
      • 3.2.2 在 XML 中添加 <insert> 标签和添加的 sql 代码
      • 3.2.3 生成测试类

回顾一下,在上一篇 MyBatis 之一(概念、创建项目、操作模式、交互流程)中,学习了 MyBatis 是一款优秀的持久层框架,学习 MyBatis 可以更方便快速的操作数据库,也学习了如何搭建 MyBatis 的开发环境,与使用 MyBatis 模式和语法操作数据库,并且也简单的了解了 MyBatis 框架的交互流程

本篇将学习如何用 MyBatis 进行数据库的增、删、改操作

这三个操作对应使用 MyBatis 的标签为

  • insert 标签:插入语句
  • update 标签:修改语句
  • delete 标签:删除语句

1. 修改操作

还是使用上一篇中创建的数据库,再给 userinfo 表中添加一条数据

INSERT INTO `mycnblog`.`userinfo` (`id`, `username`, `password`, `photo`,
`createtime`, `updatetime`, `state`) VALUES
(2, 'zhangsan', 'zhangsan', '', '2021-5-21 17:10:48', '2022-5-21 17:10:48', 1);

在这里插入图片描述

1.1 在 mapper(interface)里面添加修改方法的声明

@Mapper
public interface UserMapper {// 修改方法【根据 id 修改名称】public int update(@Param("id") Integer id,@Param("name") String username);
}

1.2 在 XMl 中添加 标签和修改的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 根据用户 id 修改用户名称 --><update id="update">update userinfo set username=#{name}  where id=#{id}</update>
</mapper>

1.3 在 UserMapper 中右键 Generate 点击 Test 生成 update 测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid update() {int result = userMapper.update(2,"老六");Assertions.assertEquals(1,result);}
}

通过断言 asserEquals 判断如果 sql 受影响行数为1,就运行正确,下面运行程序
在这里插入图片描述

然后在 mySQL中 查询,可以看到这样的测试默认情况下是污染数据库的
在这里插入图片描述

在不污染数据的前提下,执行单元测试,要添加注解 @Transactional

在这里插入图片描述

运行程序,可以看到程序虽然执行成功了,但查询数据后,没有被污染

在这里插入图片描述

2. 删除操作

2.1 在 mapper (interface)中添加删除的代码声明

@Mapper
public interface UserMapper {// 删除方法public int delete(@Param("id") Integer id);
}

2.2 在 XMl 中添加 标签和删除的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 根据用户 id 删除用户 --><delete id="delete">delete from userinfo where id=#{id}</delete>
</mapper>

2.3 在 UserMapper 中右键 Generate 点击 Test 生成 delete 测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid delete() {int result = userMapper.delete(2);System.out.println("受影响的行数:" + result);Assertions.assertEquals(1, result);}
}

我这里没添加@Transactiona,所以就直接把这条数据删除了
在这里插入图片描述

3. 添加操作

3.1 添加用户,返回受影响的行数

3.1.1 在 mapper(interface)添加方法声明

@Mapper
public interface UserMapper {// 添加用户,返回受影响的行数public int add(Userinfo userinfo);
}

3.1.2 在 XML 中添加标签和添加的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 添加用户,返回受影响的行数 --><insert id="add">insert into userinfo(username,password,photo) values(#{username},#{password},#{photo})</insert>
</mapper>

3.1.3 生成测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid add() {Userinfo userinfo = new Userinfo();userinfo.setUsername("王五");userinfo.setPassword("123");userinfo.setPhoto("default.png");int result = userMapper.add(userinfo);System.out.println("添加的结果:" + result);Assertions.assertEquals(1,result);}
}

在这里插入图片描述

3.2 添加用户,返回自增 id

3.2.1 添加方法声明

@Mapper
public interface UserMapper {// 添加用户,返回受影响的行数和自增的 idpublic int addGetId(Userinfo userinfo);
}

3.2.2 在 XML 中添加 标签和添加的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 添加用户,返回受影响的行数和自增 id --><insert id="addGetId" useGeneratedKeys="true" keyProperty="id" keyColumn="id">insert into userinfo(username,password,photo) values(#{username},#{password},#{photo})</insert>
</mapper>

在这里插入图片描述

3.2.3 生成测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid addGetId() {Userinfo userinfo = new Userinfo();userinfo.setUsername("张三");userinfo.setPassword("123");userinfo.setPhoto("default.png");System.out.println("添加之前 user id:" + userinfo.getId());int result = userMapper.addGetId(userinfo);System.out.println("受影响的行数:" + result);System.out.println("添加之后 uer id:" + userinfo.getId());Assertions.assertEquals(1,result);}
}

在这里插入图片描述

http://www.sczhlp.com/news/42221/

相关文章:

  • 网站开发工程师是web吗开车搜索关键词
  • 做网站的科技公司谷歌搜索引擎首页
  • 营销型网站建设发难西安网站seo排名优化
  • 如何防止网站被注册我想做百度推广
  • 劳务公司起名字大全免费上海网站建设seo
  • 如何做网站滚动屏幕在线培训
  • 做网站所用的技术站长统计 网站统计
  • 南京做网站需要多少钱baike seotl
  • 沈阳网站建设培训班seo用什么论坛引流
  • 深圳网站设计首选灵点网络靠谱长沙全网覆盖的网络推广
  • 外贸产品网站建设百度广告平台
  • 郑州优之客网站建设网站开发框架
  • 做数据新闻的网站2022年免费云服务器
  • 群晖ds1817做网站免费数据查询网站
  • 网站版权备案北京seo代理公司
  • 广州专业网站改版方案百度怎么投放广告
  • 多种不同产品的网站怎么做seoapp开发需要多少费用
  • 百度显示网站名app拉新渠道商
  • 网站发展阶段怎么做sem代运营
  • wordpress5.2.1网站优化网站优化
  • 劳务公司网站怎么做火蝠电商代运营公司
  • 太原做企业网站百度推广账号登录
  • xp系统做网站服务器seo的基本内容
  • 网站怎么提供下载品牌运营推广方案
  • 呼和浩特网站建设宣传域名注册网站查询
  • 读大语言模型08计算基础设施
  • 做网站网页文件焊工培训内容
  • 朝阳企业网站建设百度新闻网页
  • 楼网络规划设计方案seo刷排名工具
  • 网站建设客户资源电商运营模式