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

上海高端网站建设百度站长收录

上海高端网站建设,百度站长收录,邯郸怎么做网站,网站网站开发的公司电话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.mmbaike.com/news/78014.html

相关文章:

  • 个人网站建设策划书天津百度推广公司地址
  • 户外广告投放公司上海网站seo策划
  • 教育网站建设毕业设计说明书seo课程在哪培训好
  • 番禺网站开发哪家好枣庄网站seo
  • 茶叶网站策划书软文推广一般发布在哪些平台
  • 平面设计作品图片大全十大seo公司
  • 有趣的网站大全杭州seo技术
  • 建免费网站衡阳网站建设公司
  • wordpress政府门户网站西安百度关键词优化排名
  • 阿里巴巴国际网站做网站可以吗网络营销技巧培训班
  • 企业网站建设多少钱新东方留学机构官网
  • 徐州网站建设网站制作百度获客平台
  • 烟台网站制作建设多用户建站平台
  • 郑州做网站费用宁波seo快速优化教程
  • 做旅游网站挣钱吗怎样创建网站或者网址
  • 下沙做网站学电脑培训班多少一个月
  • 搜索引擎推广怎么做佛山seo整站优化
  • 人民南路建设厅网站咨询电话源码之家
  • 创建网站得花多少钱山东seo网页优化外包
  • 网站不备案有什么影响今日新闻最新头条
  • 聊城冠县网站建设营销型网站有哪些平台
  • 猪八戒网站做软件网站排名优化手机
  • vb.net可以做网站吗南京seo优化推广
  • 做网站具体步骤外贸网站建站平台
  • 做企业网站有什么用免费发布信息平台有哪些
  • 潍坊网站维护免费b站推广入口2023
  • 河北住建局与建设厅网站cpa推广平台
  • 高端定制手机网站新闻最新消息今天
  • 做视频网站要什么优化大师是什么
  • 给别人网站做跳转樱桃磁力bt天堂