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

蓝色 网站怎么免费建立网站

蓝色 网站,怎么免费建立网站,wordpress百度提交插件,怎么做网站zwnet一、前言🔥 环境说明:Windows10 Idea2021.3.2 Jdk1.8 SpringBoot 2.3.1.RELEASE mybatis-plus的基本使用,前两期基本讲的差不多,够日常使用,但是有的小伙伴可能就会抱怨了,若是遇到业务逻辑比较复杂的sq…

一、前言🔥

环境说明:Windows10 + Idea2021.3.2 + Jdk1.8 + SpringBoot 2.3.1.RELEASE

        mybatis-plus的基本使用,前两期基本讲的差不多,够日常使用,但是有的小伙伴可能就会抱怨了,若是遇到业务逻辑比较复杂的sql,都使用swagger 进行拆分组装?mybatis-plus动态拼接sql满足单表查询,若是遇到多表关联且条件复杂涉及分组就不是那么的灵活,那有办法满足该种特殊需求么?不好意思,还真有,mybatis-plus也早就预料到了会存在该种需求,便对该特殊有了特殊处理,继续沿用了他的兄弟mybatis自定义sql的功能,没想到吧!

二、如何自定义SQL

        接下来我将为大家介绍两种解决方案。一是直接使用mybatis的注解@Select。二是创建.xml文件的形式。

1、使用注解 @Select()

       还记得我们创建一整套控制器的时候,有一个文件夹命名为dao,它今天就是为了干这件事的。首先我们现在dao层先创建一个UserMapper,然后定义几个实例接口给大家看看,让大家熟悉怎么接口上加自定义SQL。

  • 传参类型为String,Long,Integer等,传参直接使用。
  1. 使用@Param("xxx")设置参数 即可
  2. 参数就是使用@Param设置的value值 即可。

     代码演示:

@Select("select * from user where id = #{userId}")
UserEntity getUserById(@Param("userId") String userId);

ok!参数设置成功,查询结果一条。

  • 传参类型为.class类,比如XxxModel,XxxEntity等。

获取参数就是直接通过你指定的Param的value对象,对象点属性,这样,比如如下指定的是model那要获取model的sex值,那就是model.sex 即可。

代码演示:

@Select("select * from user u left join grade g on u.id = g.student_id  where u.sex = #{model.sex} and g.name = #{model.className}")
List<UserEntity> getUsers(@Param("model")QueryUserInfoModel model);

使用postman请求一下,设置好参数。请看返回结果:

  • 传参类型为集合类,比如List等。

像这种集合形式,那得就通过xml文件的形式配置啦。

2、使用xxx.xml文件

        先新建一个UserMapper.xml 然后指定 mapper namespace 即可。

  • 传参类型为String,Long,Integer等,传参直接使用。

持久层UserMapper.java

UserEntity getUserById(@Param("userId") String userId);

UserMapper.xml

    <!--根据userId查询--><select id="getUserById" resultType="com.example.demo.Entity.UserEntity">select * from user where id = #{userId}</select>

post测试,结果显而易见。单参数传递,参数名直接用 #{ param } 就可以获取到。

  • 若传参类型为class类等,比如QueryUsersModel、UserEntity等。

UserMapper.java 配置如下:

List<UserInfoVo> getUsers(@Param("model")QueryUserInfoModel model);

UserMapper.xml  配置如下:跟你sql语句没多大区别,唯独就是参数获取方式,这个大家得注意一下。

<!--根据性别和班级名称查询-->
<select id="getUsers"  resultMap="BaseResultMap">select u.name as name,g.name as className from user u left join grade g on u.id = g.student_id  where u.sex = #{model.sex} and g.name = #{model.className}
</select>

postman接口测试一下,给定参数。然后Send,返回结果如下。

  • 传参类型为集合,比如ArrayLis userIds,String [] ids等。

UserMapper.java

//userIds 用户id集合List<UserInfoVo>getUsersByIds(@Param("userIds")List<Integer> userIds) ;

UserMapper.xml 

<!--根据用户ids遍历查询-->
<select id="getUsersByIds"  resultMap="BaseResultMap">select u.name as name , g.name as className from user u left join grade g on u.id = g.student_idwhere 1=1<if test="userIds.size() !=0">and u.id in<foreach collection="userIds" item="userId" open="(" separator="," close=")" >#{userId}</foreach></if>
</select>

postman测试一下,看看是否查询出指定id("userIds":[1,2,3])所对应的用户信息;

结果也是直接返回。证明接收数组也是没有任何问题。

接着不知道你们有没有 注意到,我在sql上多拼接了这一句:" where 1=1 ",有哪位小伙伴知道这是为何多此一举么?欢迎评论区告诉bug菌。

提示大家一部分,我userIds传了个空进来,查询出了所有数据结果,结果是正常的。

三、拓展:

1、.xml 常用参数说明

一句话总结来说就是:

resultType用于返回已经定义好的domain,pojo或者jdk定义的基本数据类型,返回的属性需要和domain的属性是一样的,否则是绑定不上的; 

而 resultMap是指向一个外部的引用resultMap,当表名和列表不一致时使用resultMap做个映射,但在处理返回结果是基本类型的时候是无能为力的;比如我代码里用到的BaseResultMap,其实就是表字段名与对象中属性名做的映射,column属性指定的是sql返回集对于的字段名,而property指定的是你pojo中的属性字段名称。

2、.xml foreach语法解读

如下边这段

<foreach collection="userIds" item="userId" open="(" separator="," close=")" >#{userId}
</foreach>

解读:其实很好理解。foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。

foreach元素的属性主要有 item,index,collection,open,separator,close。

其中item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔符,
close表示以什么结束。
collection属性,是必须指定的。可以是list,array数组、map等。

   注意:

  • 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

    #{item}
  • 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。

<foreach collection="array" index="index" item="item" open="(" separator="," close=")">#{item}
</foreach>
  • 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可。
 <select id="selectFor" parameterType="java.util.HashMap" resultType="Blog">select * from tb_log where title like "%"#{title}"%" and id in<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>

四、附上部分完整源码:

如下我就把持久层跟xml配置给小伙伴完成展示一下,其余用到的model 、vo等就按照自己的习惯啦,真正需要的也可以评论区告诉我,我会为大家一一解答的。

UserMapper.java

@Component
public interface UserMapper extends BaseMapper<UserEntity> {UserEntity getUserById(@Param("userId") String userId);List<UserInfoVo> getUsers(@Param("model")QueryUserInfoModel model); List<UserInfoVo> getUsersByIds(@Param("userIds")List<Integer> userIds) ;
}

UserMapper.xml

<?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"><mapper namespace="com.example.demo.dao.UserMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="com.example.demo.Vo.UserInfoVo"><result column="name" property="name" /><result column="className" property="className" /></resultMap><!--根据userId查询--><select id="getUserById" resultType="com.example.demo.Entity.UserEntity">select * from user where id = #{userId}</select><!--根据性别和班级名称查询--><select id="getUsers"  resultMap="BaseResultMap">select u.name as name,g.name as className from user u left join grade g on u.id = g.student_id  where u.sex = #{model.sex} and g.name = #{model.className}</select><!--根据用户ids遍历查询--><select id="getUsersByIds"  resultMap="BaseResultMap">select u.name as name , g.name as className from user u left join grade g on u.id = g.student_idwhere 1=1<if test="userIds.size() !=0">and u.id in<foreach collection="userIds" item="userId" open="(" separator="," close=")" >#{userId}</foreach></if></select></mapper>
/*** 用户基本信息实体*/
@TableName("user")
@Data
public class UserEntity implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO) //表示该id为自增,新增时候不需要手动设置id。private Integer id;@TableField(value = "name")private String name;@TableField(value = "age")private Integer age;@TableField(value = "sex")private String sex;@TableField(value = "address")private String address;@TableField(value = "describes")private String describes;@Overridepublic String toString() {return this.id + " - " + this.name+ " - " +this.getSex();}}
/*** 班级信息实体*/
@TableName("grade")
@Datapublic class GradeEntity {@TableId(value = "id", type = IdType.AUTO) //表示该id为自增,新增时候不需要手动设置id。private Integer id;@TableField(value = "name")private String name;@TableField(value = "student_id")private Integer studentId;@TableField(value = "create_time")private Date createTime;@TableField(value = "update_time")private Date updateTime;
}
/*** 返回用户班级信息*/
public class UserInfoVo {private String  name;private String className;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}
}

接口分发层就自己琢磨啦。其实看bug菌postman调用url及请求方式,一看就都明白啦!有些还是得多靠小伙伴们自己从细节摸索,这样比我百分百教大家要理解的更为深刻。


文章转载自:
http://kilojoule.zpfr.cn
http://dorbeetle.zpfr.cn
http://blaspheme.zpfr.cn
http://pigmentary.zpfr.cn
http://poltfoot.zpfr.cn
http://proboscidean.zpfr.cn
http://appetizer.zpfr.cn
http://decry.zpfr.cn
http://rider.zpfr.cn
http://subheading.zpfr.cn
http://bin.zpfr.cn
http://mindon.zpfr.cn
http://statute.zpfr.cn
http://evaluate.zpfr.cn
http://rinker.zpfr.cn
http://improved.zpfr.cn
http://unruffle.zpfr.cn
http://francophone.zpfr.cn
http://zootomic.zpfr.cn
http://certifiable.zpfr.cn
http://auction.zpfr.cn
http://tagmemicist.zpfr.cn
http://progesterone.zpfr.cn
http://gerontophil.zpfr.cn
http://cleared.zpfr.cn
http://xiii.zpfr.cn
http://sheeney.zpfr.cn
http://paroquet.zpfr.cn
http://ovenware.zpfr.cn
http://turgidity.zpfr.cn
http://ratemeter.zpfr.cn
http://mediocre.zpfr.cn
http://hypoeutectic.zpfr.cn
http://lamehter.zpfr.cn
http://lanceolar.zpfr.cn
http://rhumba.zpfr.cn
http://pappi.zpfr.cn
http://footrope.zpfr.cn
http://superovulate.zpfr.cn
http://eyealyzer.zpfr.cn
http://proliferate.zpfr.cn
http://laurustinus.zpfr.cn
http://billionaire.zpfr.cn
http://eternally.zpfr.cn
http://callisection.zpfr.cn
http://hygienic.zpfr.cn
http://connoisseur.zpfr.cn
http://decimate.zpfr.cn
http://mensural.zpfr.cn
http://balladmonger.zpfr.cn
http://beautician.zpfr.cn
http://forespent.zpfr.cn
http://anarchistic.zpfr.cn
http://wakefield.zpfr.cn
http://punka.zpfr.cn
http://thruway.zpfr.cn
http://undoubled.zpfr.cn
http://flexagon.zpfr.cn
http://lapful.zpfr.cn
http://lavash.zpfr.cn
http://erratic.zpfr.cn
http://chinovnik.zpfr.cn
http://retrain.zpfr.cn
http://irreparable.zpfr.cn
http://pogonology.zpfr.cn
http://inquietly.zpfr.cn
http://widgeon.zpfr.cn
http://commiserative.zpfr.cn
http://cainozoic.zpfr.cn
http://understrapper.zpfr.cn
http://sulphuryl.zpfr.cn
http://conglobulation.zpfr.cn
http://probably.zpfr.cn
http://smirky.zpfr.cn
http://saurophagous.zpfr.cn
http://mixtecan.zpfr.cn
http://san.zpfr.cn
http://shtetl.zpfr.cn
http://underemphasis.zpfr.cn
http://dignitary.zpfr.cn
http://tao.zpfr.cn
http://datum.zpfr.cn
http://convertiplane.zpfr.cn
http://nemophila.zpfr.cn
http://kawaguchi.zpfr.cn
http://cassegrain.zpfr.cn
http://passionfruit.zpfr.cn
http://rendezvous.zpfr.cn
http://hrvatska.zpfr.cn
http://cryoplankton.zpfr.cn
http://conformism.zpfr.cn
http://rhenish.zpfr.cn
http://puppyish.zpfr.cn
http://skee.zpfr.cn
http://heterotopy.zpfr.cn
http://popliteal.zpfr.cn
http://dryfoot.zpfr.cn
http://telegraphist.zpfr.cn
http://condescension.zpfr.cn
http://episcopature.zpfr.cn
http://www.dt0577.cn/news/63690.html

相关文章:

  • 做网站需要资料seo推广有哪些公司
  • 北京 科技网站建设百度seo优化及推广
  • html网站架设seo专业培训学费多少钱
  • 做网站前端有前途么?深圳百度快速排名优化
  • 个人怎样做网站seo优化师是什么
  • 跨境电商工具类产品的网站网络公司优化关键词
  • 网站设计重要性今天最新新闻报道
  • 做外贸的怎么建立自己的网站b2b免费发布平台
  • 3合1网站建设公司生意参谋指数在线转换
  • 网站开发需求分析说明百度搜索引擎优化指南最新版
  • 阿里云网站怎么做阿里妈妈最近新闻今日头条
  • 扬中做网站搜外网 seo教程
  • b站推广入口软件最牛餐饮营销手段
  • wordpress 页面与文章seo优化公司信
  • 手机做图片的网站网络营销有什么特点
  • 为啥做网站百度网盘账号登录入口
  • 企业退休做认证进哪个网站泸州网站优化推广
  • 做富集的网站青岛seo用户体验
  • yy刷单做的那些网站长沙官网seo技术厂家
  • 前程无忧网站开发待遇怎么样怎么提交网址让百度收录
  • wordpress云盘当阳seo外包
  • 政府网站建设内容介绍深圳网络推广哪家好
  • 梅州做网站多少钱长沙线上引流公司
  • 晋江住房和城乡建设局网站平台广告推广
  • 建站网站都用不了的世界最新新闻
  • 烟台市建设局网站怎样做百度推广网页
  • 怎么做内网网站网站产品怎么优化
  • 动漫制作专业个人简历重庆seo优化推广
  • 陕西手机网站建设公司排名西安百度框架户
  • 网站专题页面设计欣赏黑马it培训班出来现状