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

网站icpseo的作用有哪些

网站icp,seo的作用有哪些,网络培训软件,关于婚纱摄影的网站模板Mybatis基础操作 功能列表: 查询 根据主键ID查询 条件查询新增更新删除 根据主键ID删除 根据主键ID批量删除 准备 实施前的准备工作: 准备数据库表创建一个新的 springboot 工程,选择引入对应的起步依赖(mybatis、mysql 驱动、…

Mybatis基础操作

功能列表:

  1. 查询
    根据主键ID查询
    条件查询
  2. 新增
  3. 更新
  4. 删除
    根据主键ID删除
    根据主键ID批量删除

准备

实施前的准备工作:

  • 准备数据库表
  • 创建一个新的 springboot 工程,选择引入对应的起步依赖(mybatis、mysql 驱动、lombok)
  • application.properties 中引入数据库连接信息
  • 创建对应的实体类 Emp(实体类属性采用驼峰命名)
  • 准备 Mapper 接口 EmpMapper

删除

 功能实现

当我们点击后面的"删除"按钮时,前端页面会给服务端传递一个参数,也就是该行数据的 ID。我们接收到 ID 后,根据 ID 删除数据即可。

 SQL语句:

接口方法:

注意:如果mapper接口方法形参只有一个普通类型的参数,#{…} 里面的属性名可以随便写,如:#{id}#{value}

完整代码:

@Mapper
public interface EmpMapper {//根据ID删除数据@Delete("delete from emp where id = #{id}")public void delete(Integer id);//public int delete(Integer id);
}
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;//根据ID删除@Testpublic void testDelete(){//int delete = empMapper.delete(16);//System.out.println(delete);empMapper.delete(18);}
}

日志输入 

在 Mybatis 当中我们可以借助日志,查看到 sql 语句的执行、执行传递的参数以及执行结果。具体操作如下:

  1. 打开 application.properties 文件
  2. 开启 mybatis 的日志,并指定输出到控制台
#指定mybatis输出日志的位置,输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

预编译 SQL

预编译 SQL 有两个优势:

  1. 性能更高
  2. 更安全(防止 SQL 注入)

SQL 注入 

SQL 注入:是通过操作输入的数据来修改事先定义好的 SQL 语句,以达到执行代码对服务器进行攻击的方法。

参数占位符

在 Mybatis 中提供的参数占位符有两种:${…} 、#{…}

#{…}
执行 SQL 时,会将#{…}替换为?,生成预编译 SQL,会自动设置参数值
使用时机:参数传递,都使用#{…}
${…}
拼接 SQL。直接将参数拼接在 SQL 语句中,存在 SQL 注入问题
使用时机:如果对表名、列表进行动态设置时使用

插入&新增

SQL语句:

insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) 
values ('songyuanqiao','宋远桥',1,'1.jpg',2,'2012-10-09',2,'2022-10-01 10:00:00','2022-10-01 10:00:00');

接口方法:

新增(主键返回)

描述:在数据添加成功后,需要获取插入数据库数据的主键。如:添加套餐数据时,还需要维护套餐菜品关系表数据。

默认情况下,执行插入操作时,是不会主键值返回的。如果我们想要拿到主键值,需要在 Mapper 接口中的方法上添加一个 Options 注解,
并在注解中指定属性 useGeneratedKeys = true keyProperty ="实体类属性名

实现:

更新

SQL语句(根据ID更新员工信息)
接口方法

@Mapper
public interface EmpMapper {//更新员工@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}," +" job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")public void update(Emp emp);
}

查询

根据ID查询

SQL语句(根据ID查询)

接口方法

@Mapper
public interface EmpMapper {@Select("select * from emp where id = #{id}")public Emp getById(Integer id);
}

 数据封装

实体类属性名 和 数据库表查询返回的字段名一致,mybatis会自动封装。
如果实体类属性名 和 数据库表查询返回的字段名不一致,不能自动封装。

数据封装---解决方法

起别名在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样。

手动结果映射:通过 @Results及@Result 进行手动结果映射。

开启驼峰命名:如果字段名与属性名符合驼峰命名规则,mybatis会自动通过驼峰命名规则映射

条件查询

SQL语句条件查询

select *  from emp where name like '%张%' and gender = 1 and entrydate between '2010-01-01' and '2020-01-01 ' order by update_time desc;

接口方法

方式一
模糊查询使用${…}进行字符串拼接,这种方式呢,由于是字符串拼接,并不是预编译的形式,所以效率不高、且存在sql注入风险。

方式二(解决SQL注入风险)
使用 MySQL 提供的字符串拼接函数:concat(‘%’ , ‘关键字’ , ‘%’)

@Mapper
public interface EmpMapper {//条件查询员工//方式一@Select("select * from emp where name like '%${name}%' and gender = #{gender} and " +"entrydate between #{begin} and #{end} order by update_time desc ")public List<Emp> list(String name, Short gender, LocalDate begin , LocalDate end);
}@Mapper
public interface EmpMapper {//条件查询员工//方式二@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and " +"entrydate between #{begin} and #{end} order by update_time desc ")public List<Emp> list(String name, Short gender, LocalDate begin , LocalDate end);
}

参数名说明

Mybatis 的 XML 配置文件

XML 配置文件规范

规范:

        XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名)

        XML映射文件的namespace属性为Mapper接口全限定名一致。

        XML映射文件中sql语句的idMapper 接口中的方法名一致,并保持返回类型一致。

XML 配置文件实现

@Mapper
public interface EmpMapper {//动态条件查询public List<Emp> list(String name, Short gender, LocalDate begin , LocalDate end);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--resultType: 单条记录封装的类型--><select id="list" resultType="com.itheima.pojo.Emp">select * from emp where name like concat('%',#{name},'%') and gender = #{gender}  and entrydate between #{begin} and #{end} order by update_time desc</select></mapper>

XML映射文件    MybatisX

MybatisX 是一款基于 IDEA 的快速开发Mybatis的插件,为效率而生。

使用 Mybatis 的注解,主要是来完成一些简单的增删改查功能。如果需要实现复杂的 SQL 功能,建议使用 XML 来配置映射语句。

官方说明:入门_MyBatis中文网

Mybatis动态SQL

什么是动态SQL

SQL 语句会随着用户的输入或外部条件的变化而变化,我们称为:动态 SQL

 

Mybatis动态SQL <if>

  • <if>:用于判断条件是否成立。使用 test 属性进行条件判断,如果条件为 true,则拼接 SQL。
  • <where>:where 元素只会在子元素有内容的情况下才插入 where 子句。而且会自动去除子句的开头的 AND 或 OR。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--resultType: 单条记录封装的类型--><select id="list" resultType="com.itheima.pojo.Emp">select * from emp <where><if test="name != null">name like concat('%', #{name}, '%')</if><if test="gender != null">and gender = #{gender}</if><if test="begin != null and end != null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select></mapper>

 更新员工

  • <set>:动态地在行首插入 SET 关键字,并会删掉额外的逗号。(用在update语句中)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!-- 动态更新员工--><update id="update2">update emp<set><if test="username != null">username = #{username},</if><if test="name != null">name = #{name},</if><if test="gender != null">gender = #{gender},</if><if test="image != null">image = #{image},</if><if test="job != null">job = #{job},</if><if test="entrydate != null">entrydate = #{entrydate},</if><if test="deptId != null">dept_id = #{deptId},</if><if test="updateTime != null">update_time = #{updateTime}</if></set>where id = #{id}</update></mapper>

动态 SQL-foreach  <foreach>

批量删除员工 (18,19,20)

@Mapper
public interface EmpMapper {//批量删除员工public void deleteByIds(List<Integer> ids);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--批量删除员工 (18,19,20)--><!--collection: 遍历的集合item: 遍历出来的元素separator: 分隔符open: 遍历开始前拼接的SQL片段close: 遍历结束后拼接的SQL片段--><delete id="deleteByIds">delete  from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete></mapper>

动态 SQL-sql&include

  • <sql>:定义可重用的 SQL 片段。
  • <include>:通过属性 refid,指定包含的 sql 片段。

上一节:

Mybatis入门(day08)-CSDN博客

下一节:

http://www.hengruixuexiao.com/news/17613.html

相关文章:

  • 如何做网站给女朋友长沙网站推广服务公司
  • 俄罗斯网站开发站长工具平台
  • 网站简繁转换今日重庆重要消息
  • 深圳做网站维护的公司免费海报模板网站
  • 南京鼓楼做网站公司sem代运营
  • 网站建设的可用性做整站优化
  • 个人可以做新闻网站重庆seo整站优化系统
  • 哪个企业的网站做的比较好淘宝关键词挖掘工具
  • wordpress 房产中介seo一个月工资一般多少
  • 网站建设的步骤过程江苏seo外包
  • 有哪些网站是可以做免费推广的海外市场推广做什么的
  • php网站建设培训59软文网
  • 蚌埠建设网站公司镇江网络
  • 合肥做网站的企业牛排seo
  • 应用中心下载优化设计单元测试卷答案
  • 不会代码可不可以做网站网站百度不收录
  • 公司网站制作的公司沧州网络推广公司
  • 网站后台上传附件交换友情链接的途径有哪些
  • 注册网站域名后免费建站打字赚钱平台 学生一单一结
  • 网站开发及上线过程线上宣传方式
  • 德清建设银行官方网站seo实战密码在线阅读
  • 永定路网站建设2023年6月疫情恢复
  • 编写网站 语言一诺网络推广公司
  • 央视网商城seo优化的作用
  • 吉安建设公司网站石家庄关键词优化报价
  • 香河住房与建设局网站广州网站排名优化公司
  • 微网站制作提供商推荐淘宝如何刷关键词增加权重
  • wordpress替换本地字体seo工具包
  • 网站内链技巧海外广告优化师
  • 企业门户网站的建设费用金华seo