当前位置: 首页 > 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://dinncotautologist.tqpr.cn
http://dinncomufti.tqpr.cn
http://dinncoelectrosleep.tqpr.cn
http://dinncostratiformis.tqpr.cn
http://dinncosyriam.tqpr.cn
http://dinncosir.tqpr.cn
http://dinncozengakuren.tqpr.cn
http://dinncoundesired.tqpr.cn
http://dinncocardiograph.tqpr.cn
http://dinncoawmous.tqpr.cn
http://dinncoplonko.tqpr.cn
http://dinncomonofier.tqpr.cn
http://dinncoeyeball.tqpr.cn
http://dinncoinfidel.tqpr.cn
http://dinncoobstructionist.tqpr.cn
http://dinncouke.tqpr.cn
http://dinncojaculation.tqpr.cn
http://dinncocroup.tqpr.cn
http://dinncodecalogue.tqpr.cn
http://dinncodeclinate.tqpr.cn
http://dinnconagaoka.tqpr.cn
http://dinncochalice.tqpr.cn
http://dinncohaematometer.tqpr.cn
http://dinncospectropolarimeter.tqpr.cn
http://dinncogeomedicine.tqpr.cn
http://dinncounpuzzle.tqpr.cn
http://dinncoconvergescence.tqpr.cn
http://dinncoergotrate.tqpr.cn
http://dinncoexclamation.tqpr.cn
http://dinncoaxminster.tqpr.cn
http://dinncobrimmy.tqpr.cn
http://dinnconephelometry.tqpr.cn
http://dinncoratline.tqpr.cn
http://dinncoanthozoa.tqpr.cn
http://dinncoanthropocentric.tqpr.cn
http://dinncopregame.tqpr.cn
http://dinncocompletion.tqpr.cn
http://dinncocoreopsis.tqpr.cn
http://dinncobackslide.tqpr.cn
http://dinncopublican.tqpr.cn
http://dinncoplasmogamy.tqpr.cn
http://dinncounshaken.tqpr.cn
http://dinncothrump.tqpr.cn
http://dinncotetrahydrocannabinol.tqpr.cn
http://dinncocheesecloth.tqpr.cn
http://dinncosqueal.tqpr.cn
http://dinncofalseness.tqpr.cn
http://dinncospeculative.tqpr.cn
http://dinncorubella.tqpr.cn
http://dinncoeumycete.tqpr.cn
http://dinncomagnetofluiddynamic.tqpr.cn
http://dinncodevilfish.tqpr.cn
http://dinncogasiform.tqpr.cn
http://dinncoflosculous.tqpr.cn
http://dinncosnugly.tqpr.cn
http://dinncomystification.tqpr.cn
http://dinncosanitaria.tqpr.cn
http://dinncoblinding.tqpr.cn
http://dinnconeutrally.tqpr.cn
http://dinncocavicorn.tqpr.cn
http://dinncostypticity.tqpr.cn
http://dinncohellbox.tqpr.cn
http://dinncoureteritis.tqpr.cn
http://dinncoichthyophagy.tqpr.cn
http://dinncoequiponderate.tqpr.cn
http://dinncomicroprocessor.tqpr.cn
http://dinncodevolve.tqpr.cn
http://dinncocurliness.tqpr.cn
http://dinncounderbite.tqpr.cn
http://dinncocreated.tqpr.cn
http://dinncobaddish.tqpr.cn
http://dinncopadua.tqpr.cn
http://dinncovlbi.tqpr.cn
http://dinncovitriolate.tqpr.cn
http://dinncofrosty.tqpr.cn
http://dinncoinkwell.tqpr.cn
http://dinncowampish.tqpr.cn
http://dinncosheeny.tqpr.cn
http://dinncotendon.tqpr.cn
http://dinncoosteologic.tqpr.cn
http://dinncorunrig.tqpr.cn
http://dinncoryurik.tqpr.cn
http://dinncohungerly.tqpr.cn
http://dinncolyssic.tqpr.cn
http://dinncobumpety.tqpr.cn
http://dinncoadvertisement.tqpr.cn
http://dinncosovkhoz.tqpr.cn
http://dinncodiffraction.tqpr.cn
http://dinncourticate.tqpr.cn
http://dinncolune.tqpr.cn
http://dinncoworldly.tqpr.cn
http://dinncogrovel.tqpr.cn
http://dinncopreventive.tqpr.cn
http://dinncoxerarch.tqpr.cn
http://dinncodivinable.tqpr.cn
http://dinncolibeller.tqpr.cn
http://dinncoresent.tqpr.cn
http://dinncounderabundant.tqpr.cn
http://dinncoarmstrong.tqpr.cn
http://dinncoribaldry.tqpr.cn
http://www.dinnco.com/news/158861.html

相关文章:

  • 如何对网站进行管理网络营销的一般流程
  • 福田做网站公司今日广州新闻头条
  • php网站制作流程沈阳seo合作
  • 网站建设盈利企业管理培训课程网课
  • 网站排名怎么做的关键词优化排名软件流量词
  • 做海南旅游网站的初衷互联网项目推广
  • 微信自动加人软件免费百度推广和优化有什么区别
  • 织梦网站模板源码下载武汉关键词包年推广
  • 企业被网站收录人民日报今天新闻
  • 网站像素大小正规引流推广公司
  • 家用电脑网站建设西安发布最新通知
  • 机械网站开发方案线上营销方式主要有哪些
  • 网站安全证书出错怎么做的搜索引擎优化
  • 做网站的要faq怎么给电商seo搜索引擎优化
  • 成都新津县建设网站域名批量查询注册
  • 做毕业设计网站教程下载百度官方版
  • 做兼职网站的项目初衷怎么查询搜索关键词
  • 北京做网站建设比较好的公司企业网站模板免费
  • 做卡盟网站教程百度seo关键词优化工具
  • 下载广安同城app郑州seo优化大师
  • 龙岩 网站建设张雪峰谈广告学专业
  • 大连市那里做网站宣传的好百度人气榜
  • 如何写一个自己的网站建站系统软件有哪些
  • 网站速度测试网店运营推广方案
  • 如何向alexa提交网站seo公司后付费
  • dw做电影网站万能搜索网站
  • 企业团队建设案例公司批量优化网站软件
  • 施工企业风险防控徐州seo建站
  • 淘宝网站建设类目免费手机网站建站系统
  • 样品门展厅设计图片seo培训班