关于MyBatis10种超好用的写法(收藏)

用来循环容器的标签forEach,查看例子

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

  • item:集合中元素迭代时的别名
  • index:集合中元素迭代时的索引
  • open:常用语where语句中,表示以什么开始,比如以'('开始
  • separator:表示在每次进行迭代时的分隔符
  • close 常用语where语句中,表示以什么结束

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

  • 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
  • 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
  • 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

针对最后一条,我们来看一下官方说法:

注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。
所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一个list为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则colloection里放的是自己封装的map里的key值

//mapper中我们要为这个方法传递的是一个容器,将容器中的元素一个一个的
//拼接到xml的方法中就要使用这个forEach这个标签了
public List<Entity> queryById(List<String> userids);
//对应的xml中如下
 <select id="queryById" resultMap="BaseReslutMap" >
   select * FROM entity
   where id in
   <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
       #{userid}
   </foreach>
 </select>

concat模糊查询

//比如说我们想要进行条件查询,但是几个条件不是每次都要使用,那么我们就可以
//通过判断是否拼接到sql中
 <select id="queryById" resultMap="BascResultMap" parameterType="entity">
  SELECT * from entity
  <where>
    <if test="name!=null">
      name like concat('%',concat(#{name},'%'))
    </if>
  </where>
 </select>

choose (when, otherwise)标签

choose标签是按顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

例如下面例子,同样把所有可以限制的条件都写上,方便使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。

<!-- choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
  SELECT *
   FROM User u
  <where>
    <choose>
      <when test="username !=null ">
        u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
      </when >
      <when test="sex != null and sex != '' ">
        AND u.sex = #{sex, jdbcType=INTEGER}
      </when >
      <when test="birthday != null ">
        AND u.birthday = #{birthday, jdbcType=DATE}
      </when >
      <otherwise>
      </otherwise>
    </choose>
  </where>
</select>

selectKey 标签

在insert语句中,在Oracle经常使用序列、在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键。使用myBatis的selectKey标签可以实现这个效果。

下面例子,使用mysql数据库自定义函数nextval('student'),用来生成一个key,并把它设置到传入的实体类中的studentId属性上。所以在执行完此方法后,便可以通过这个实体类获取生成的key。

<!-- 插入学生 自动主键-->
<insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId">
  <selectKey keyProperty="studentId" resultType="String" order="BEFORE">
    select nextval('student')
  </selectKey>
  INSERT INTO STUDENT_TBL(STUDENT_ID,
              STUDENT_NAME,
              STUDENT_SEX,
              STUDENT_BIRTHDAY,
              STUDENT_PHOTO,
              CLASS_ID,
              PLACE_ID)
  VALUES (#{studentId},
      #{studentName},
      #{studentSex},
      #{studentBirthday},
      #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
      #{classId},
      #{placeId})
</insert>

调用接口方法,和获取自动生成key

StudentEntity entity = new StudentEntity();
entity.setStudentName("黎明你好");
entity.setStudentSex(1);
entity.setStudentBirthday(DateUtil.parse("1985-05-28"));
entity.setClassId("20000001");
entity.setPlaceId("70000001");
this.dynamicSqlMapper.createStudentAutoKey(entity);
System.out.println("新增学生ID: " + entity.getStudentId());

if标签

if标签可用在许多类型的sql语句中,我们以查询为例。首先看一个很普通的查询:

<!-- 查询学生list,like姓名 -->
<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">
  SELECT * from STUDENT_TBL ST
WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
</select>

但是此时如果studentName为null,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断,增加灵活性。

参数为实体类StudentEntity。将实体类中所有的属性均进行判断,如果不为空则执行判断条件。

<!-- 2 if(判断参数) - 将实体类不为空的属性作为where条件 -->
<select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">
  SELECT ST.STUDENT_ID,
      ST.STUDENT_NAME,
      ST.STUDENT_SEX,
      ST.STUDENT_BIRTHDAY,
      ST.STUDENT_PHOTO,
      ST.CLASS_ID,
      ST.PLACE_ID
   FROM STUDENT_TBL ST
   WHERE
  <if test="studentName !=null ">
    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
  </if>
  <if test="studentSex != null and studentSex != '' ">
    AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
  </if>
  <if test="studentBirthday != null ">
    AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
  </if>
  <if test="classId != null and classId!= '' ">
    AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
  </if>
  <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
    AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
  </if>
  <if test="placeId != null and placeId != '' ">
    AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
  </if>
  <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
    AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
  </if>
  <if test="studentId != null and studentId != '' ">
    AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
  </if>
</select>

使用时比较灵活, new一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会where这个条件,相反不去赋值就可以不在where中判断。

public void select_test_2_1() {
  StudentEntity entity = new StudentEntity();
  entity.setStudentName("");
  entity.setStudentSex(1);
  entity.setStudentBirthday(DateUtil.parse("1985-05-28"));
  entity.setClassId("20000001");
  //entity.setPlaceId("70000001");
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity);
  for (StudentEntity e : list) {
    System.out.println(e.toString());
  }
}

if + where 的条件判断

当where中的条件使用的if标签较多时,这样的组合可能会导致错误。我们以在3.1中的查询语句为例子,当java代码按如下方法调用时:

@Test
public void select_test_2_1() {
  StudentEntity entity = new StudentEntity();
  entity.setStudentName(null);
  entity.setStudentSex(1);
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity);
  for (StudentEntity e : list) {
    System.out.println(e.toString());
  }
}

如果上面例子,参数studentName为null,将不会进行STUDENT_NAME列的判断,则会直接导“WHERE AND”关键字多余的错误SQL。

这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where'。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

上面例子修改为:

<!-- 3 select - where/if(判断参数) - 将实体类不为空的属性作为where条件 -->
<select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">
  SELECT ST.STUDENT_ID,
      ST.STUDENT_NAME,
      ST.STUDENT_SEX,
      ST.STUDENT_BIRTHDAY,
      ST.STUDENT_PHOTO,
      ST.CLASS_ID,
      ST.PLACE_ID
   FROM STUDENT_TBL ST
  <where>
    <if test="studentName !=null ">
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
    </if>
    <if test="studentSex != null and studentSex != '' ">
      AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
    </if>
    <if test="studentBirthday != null ">
      AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
    </if>
    <if test="classId != null and classId!= '' ">
      AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
    </if>
    <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
      AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
    </if>
    <if test="placeId != null and placeId != '' ">
      AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
    </if>
    <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
      AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
    </if>
    <if test="studentId != null and studentId != '' ">
      AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
    </if>
  </where>
</select>

if + set实现修改语句

当update语句中没有使用if标签时,如果有一个参数为null,都会导致错误。

当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。使用if+set标签修改后,如果某项为null则不进行更新,而是保持数据库原值。

如下示例:

<!-- 4 if/set(判断参数) - 将实体类不为空的属性更新 -->
<update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.StudentEntity">
  UPDATE STUDENT_TBL
  <set>
    <if test="studentName != null and studentName != '' ">
      STUDENT_TBL.STUDENT_NAME = #{studentName},
    </if>
    <if test="studentSex != null and studentSex != '' ">
      STUDENT_TBL.STUDENT_SEX = #{studentSex},
    </if>
    <if test="studentBirthday != null ">
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
    </if>
    <if test="studentPhoto != null ">
      STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
    </if>
    <if test="classId != '' ">
      STUDENT_TBL.CLASS_ID = #{classId}
    </if>
    <if test="placeId != '' ">
      STUDENT_TBL.PLACE_ID = #{placeId}
    </if>
  </set>
  WHERE STUDENT_TBL.STUDENT_ID = #{studentId};
</update>

if + trim代替where/set标签

trim是更灵活的去除多余关键字的标签,他可以实践where和set的效果。

trim代替where

<!-- 5.1if/trim代替where(判断参数) -将实体类不为空的属性作为where条件-->
<select id="getStudentList_if_trim" resultMap="resultMap_studentEntity">
  SELECT ST.STUDENT_ID,
      ST.STUDENT_NAME,
      ST.STUDENT_SEX,
      ST.STUDENT_BIRTHDAY,
      ST.STUDENT_PHOTO,
      ST.CLASS_ID,
      ST.PLACE_ID
   FROM STUDENT_TBL ST
  <trim prefix="WHERE" prefixOverrides="AND|OR">
    <if test="studentName !=null ">
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
    </if>
    <if test="studentSex != null and studentSex != '' ">
      AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
    </if>
    <if test="studentBirthday != null ">
      AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
    </if>
    <if test="classId != null and classId!= '' ">
      AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
    </if>
    <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
      AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
    </if>
    <if test="placeId != null and placeId != '' ">
      AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
    </if>
    <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
      AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
    </if>
    <if test="studentId != null and studentId != '' ">
      AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
    </if>
  </trim>
</select>

trim代替set

<!-- 5.2 if/trim代替set(判断参数) - 将实体类不为空的属性更新 -->
<update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.StudentEntity">
  UPDATE STUDENT_TBL
  <trim prefix="SET" suffixOverrides=",">
    <if test="studentName != null and studentName != '' ">
      STUDENT_TBL.STUDENT_NAME = #{studentName},
    </if>
    <if test="studentSex != null and studentSex != '' ">
      STUDENT_TBL.STUDENT_SEX = #{studentSex},
    </if>
    <if test="studentBirthday != null ">
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
    </if>
    <if test="studentPhoto != null ">
      STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
    </if>
    <if test="classId != '' ">
      STUDENT_TBL.CLASS_ID = #{classId},
    </if>
    <if test="placeId != '' ">
      STUDENT_TBL.PLACE_ID = #{placeId}
    </if>
  </trim>
  WHERE STUDENT_TBL.STUDENT_ID = #{studentId}
</update>

foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List 实例将使用“list”作为键,数组实例以“array” 做为键。

foreach元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

注意:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。
这个部分是对关于XML配置文件和XML映射文件的而讨论的。下一部分将详细讨论Java API,所以你可以得到你已经创建的最有效的映射。

1.参数为array示例的写法

接口的方法声明:

public List<StudentEntity> getStudentListByClassIds_foreach_array(String[] classIds);

动态SQL语句:

<!-- 7.1 foreach(循环array参数) - 作为where中in的条件 -->
<select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity">
  SELECT ST.STUDENT_ID,
      ST.STUDENT_NAME,
      ST.STUDENT_SEX,
      ST.STUDENT_BIRTHDAY,
      ST.STUDENT_PHOTO,
      ST.CLASS_ID,
      ST.PLACE_ID
   FROM STUDENT_TBL ST
   WHERE ST.CLASS_ID IN
   <foreach collection="array" item="classIds" open="(" separator="," close=")">
    #{classIds}
   </foreach>
</select>

测试代码,查询学生中,在20000001、20000002这两个班级的学生:

@Test
public void test7_foreach() {
  String[] classIds = { "20000001", "20000002" };
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_array(classIds);
  for (StudentEntity e : list) {
    System.out.println(e.toString());
  }
}

2.参数为list示例的写法

接口的方法声明:

public List<StudentEntity> getStudentListByClassIds_foreach_list(List<String> classIdList);

动态SQL语句:

<!-- 7.2 foreach(循环List<String>参数) - 作为where中in的条件 -->
<select id="getStudentListByClassIds_foreach_list" resultMap="resultMap_studentEntity">
  SELECT ST.STUDENT_ID,
      ST.STUDENT_NAME,
      ST.STUDENT_SEX,
      ST.STUDENT_BIRTHDAY,
      ST.STUDENT_PHOTO,
      ST.CLASS_ID,
      ST.PLACE_ID
   FROM STUDENT_TBL ST
   WHERE ST.CLASS_ID IN
   <foreach collection="list" item="classIdList" open="(" separator="," close=")">
    #{classIdList}
   </foreach>
</select>

测试代码,查询学生中,在20000001、20000002这两个班级的学生:

@Test
public void test7_2_foreach() {
  ArrayList<String> classIdList = new ArrayList<String>();
  classIdList.add("20000001");
  classIdList.add("20000002");
  List<StudentEntity> list = this.dynamicSqlMapper.getStudentListByClassIds_foreach_list(classIdList);
  for (StudentEntity e : list) {
    System.out.println(e.toString());
  }
}

sql片段标签<sql>:通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。这样既可以提高编码效率,还能有效简化代码,提高可读性

需要配置的属性:id="" >>>表示需要改sql语句片段的唯一标识

引用:通过<include refid="" />标签引用,refid="" 中的值指向需要引用的<sql>中的id=“”属性

<!--定义sql片段-->
<sql id="orderAndItem">
  o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
 </sql> 

 <select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
  select
<!--引用sql片段-->
  <include refid="orderAndItem" />
  from ordertable o
  join orderitem i on o.orderitem_id = i.orderitem_id
  where o.order_id = #{orderId}
 </select>

到此这篇关于关于MyBatis10种超好用的写法(收藏)的文章就介绍到这了,更多相关MyBatis 写法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Mybatis3 if判断字符串变态写法

    mybatis我们常用的判空操作,出现了常见问题: 错误写法:if test="status == 'Y'" 结果:抛异常NumberFormatException异常!提示内容非常少,看不出问题在哪里! 正确写法:if test='status == "y"' 还可以这样写:if test="status == 'y'.toString()" 或者可以这样写 if test ='status=="Y"' 补充:Mybatis

  • mybatis分页绝对路径写法过程详解

    这篇文章主要介绍了mybatis分页绝对路径写法过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 共四步, 1.下载jar包,maven的坐标为 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.4</version&

  • MyBatis传入集合 list 数组 map参数的写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性

  • 详解MyBatis批量插入数据Mapper配置文件的写法

    对于MyBatis配置文件的用法一直不是很熟悉,之前一直是使用注解来开发的,但是注解也有不好的地方就是如果数据库的表结构发生变化在代码中修改起来很麻烦. 其实批量插入很简单,这里做些简要的说明.请看配置文件的写法: <insert id="insertAll" parameterType="java.util.List" useGeneratedKeys="true"> <selectKey resultType="l

  • 详解MyBatis 常用写法

    什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录. 1.forEach 循环   forEach 元素的属性主要有 item, idnex, collection,

  • 详解Mybatis注解写法(附10余个常用例子)

    [前言] Mybatis 除了 XML 配置写法,还可以使用注解写法. 首先需要引入 Mybatis 的依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <

  • Mybatis增删改查mapper文件写法详解

      1. 插入 <mapper namespace="需要实现接口的全类名"> <insert id="需要实现的接口里的方法名" parameterType="方法参数类型,如果是对象要写全类名"> INSERT sql命令(命令里通过#{}获取对象属性) <!--注意属性名区分大小写 --> </insert> <mapper> EG: <mapper namespace=&q

  • MyBatis配置文件的写法和简单使用

    初识 MyBatis 一 最初 Apache 有一个batis的开源项目,放在Google code 中,后来因为一些原因迁移到了github,就是今天的myBatis 什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及抽取结果集.MyBatis 使用简单的 XML 或注解来配置和映射基本体,将接口和 Java 的 POJOs(Plain Old Java Objects,普

  • 关于MyBatis10种超好用的写法(收藏)

    用来循环容器的标签forEach,查看例子 foreach元素的属性主要有item,index,collection,open,separator,close. item:集合中元素迭代时的别名 index:集合中元素迭代时的索引 open:常用语where语句中,表示以什么开始,比如以'('开始 separator:表示在每次进行迭代时的分隔符 close 常用语where语句中,表示以什么结束 在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的

  • Java5种遍历HashMap数据的写法

    本文介绍了最好的Java5种遍历HashMap数据的写法,分享给大家,也给自己留一个笔记,具体如下: 通过EntrySet的迭代器遍历 Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator(); while (iterator.hasNext()) { Entry < Integer, String > entry = iterator.next(); System

  • 详解Python中4种超参自动优化算法的实现

    目录 一.网格搜索(Grid Search) 二.随机搜索(Randomized Search) 三.贝叶斯优化(Bayesian Optimization) 四.Hyperband 总结 大家好,要想模型效果好,每个算法工程师都应该了解的流行超参数调优技术. 今天我给大家总结超参自动优化方法:网格搜索.随机搜索.贝叶斯优化 和 Hyperband,并附有相关的样例代码供大家学习. 一.网格搜索(Grid Search) 网格搜索是暴力搜索,在给定超参搜索空间内,尝试所有超参组合,最后搜索出最优

  • C语言基础文件操作方式超全详解建议收藏

    目录 什么是文件 文件名 文件类型 文件指针 文件的打开与关闭 打开方式 文件的顺序读写 关于fread的返回值 对比一组函数 文件随机读取 文件结束判断 perror() ferror() 什么是文件 磁盘上的文件是文件. 在程序设计中,我们一般读的文件有两种:程序文件 和 数据文件 程序文件包括源程序文件(后缀为.c).目标文件(win下后缀为 .obj).可执行文件(win下环境后缀为.exe) 数据文件:文件的内容不一定是程序,而是运行时读写的程序,比如程序运行需要从中读取数据的文件,或

  • Java实现ATM系统超全面步骤解读建议收藏

    目录 1.系统准备,首页,用户开户功能 系统准备,首页设计 总结 总结 2.用户登入,操作页展示,查询账户,退出账户 用户登入功能实现 总结 总结 3.用户存款与取款 用户存款 总结 总结温习 4.用户转账,修改密码,销户 用户转账功能 总结温习 5.源代码在这里这里拿 系统准备内容分析 每个用户的账户信息都是一个对象,需要提供账户类 需要准备一个容器,用户存储系统全部账户对象信息 首页值需要包含:登入和注册2个功能 1.系统准备,首页,用户开户功能 系统准备,首页设计 系统准备内容分析: 每个

  • JavaScript 三种不同位置代码的写法

    下面列举在三种不同的地方写JavaScript代码,实现的效果都是点击按钮button弹出alert警告框 第一种是最常见的,代码如下 html代码 <input type="button" value="按钮1" id="btn1" onclick="pop()"> js代码 function pop() { alert("在JavaScript函数处调用"); } 第二种是最简单的实现方式,

  • sqlserver分页的两种写法分别介绍

    第一种是最传统的写法,用存储过程中的变量作为分页的乘数 复制代码 代码如下: [c-sharp] view plaincopyprint?create proc p_paged1 @pageSize int,@currentPage int as select top (@pageSize) * from student where id not in (select top (@pageSize*(@currentPage-1)) id from student) go exec p_page

  • JS中for循环的四种写法示例(入门级)

    目录 1. 传统for循环 2. for of 循环 3. for in 循环 4. forEach方法 附完整实例: 总结 1. 传统for循环 for (init; cond; inc) { // body } 与C++或Java类似,for关键字后用小括号描述循环设置,在小括号中用两个分号;将循环设置隔断为三个部分,分别为: init初始化语句(指令),在整个循环开始前执行 cond条件(逻辑表达式),表示循环继续的条件 inc自增语句(指令),在每次循环体结束以后执行 整个循环的执行步骤

  • jquery ready()的几种实现方法小结

    1.最常用也是最标准的 复制代码 代码如下: $(document).ready(){ }); 2.是上面的简写: 复制代码 代码如下: $(function(){ }) 很奇怪?为什么能这样?不是判断document对象是否 reADy然后才执行函数的么?document哪去了?我们看下jQuery的源代码: 复制代码 代码如下: // jQuery的构造函数: var jQuery = function( a, c ) { // $(document).ready()的简写形式,只有在$(f

  • Linux中拷贝 cp命令中拷贝所有的写法详解

    今天在编写一个脚本的时候,发现一个比较奇怪的问题:就是在使用cp拷贝当前目录下所有文件到目标目录的时候,源和目标目录大小不同.原来一直没有留意有这样的问题,后来查了些资料,才知道以前一直使用的格式有误, 一.预备 cp就是拷贝,最简单的使用方式就是: cp oldfile newfile 但这样只能拷贝文件,不能拷贝目录,所以通常用: cp -r old/ new/ 那就会把old目录整个拷贝到new目录下.注意,不是把old目录里面的文件拷贝到new目录,而是把old直接拷贝到new下面,结果

随机推荐