首页 > 其他分享 >Mybatis学习笔记

Mybatis学习笔记

时间:2024-07-19 22:08:18浏览次数:13  
标签:name 笔记 学习 emp org Mybatis import com itheima

Mybatis入门

Mybatis简介

image-20240717163525634

官网:https://mybatis.org/mybatis-3/zh/index.html

入门程序

查询user表中数据

image-20240717165442863

image-20240717163657396

image-20240717163714267

image-20240717163722705

image-20240717163733064

image-20240717163806068

mapper接口

package com.itheima.mapper;

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface UserMapper {

//    查询sql语句
    @Select("select * from user")
    public List<User> list();

}

实体User类

package com.itheima.pojo;

public class User {
    private Integer id;
    private String name;
    private Short age;
    private Short gender;
    private String phone;

    public User(Integer id, String name, Short age, Short gender, String phone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public Short getGender() {
        return gender;
    }

    public void setGender(Short gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                ", phone='" + phone + '\'' +
                '}';
    }
}

数据库配置信息

application.properties文件

#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=1234

测试类

package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

//Springboot整合单元测试的注解
@SpringBootTest
class SpringbootMybatisQuickstartApplicationTests {

    @Test
    void contextLoads() {
    }

//    依赖注入
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testListUser() {
        List<User> userList = userMapper.list();
        userList.stream().forEach(user -> {
            System.out.println(user);
        });
    }

}

运行结果

image-20240717165531588

JDBC介绍(不需要掌握)

image-20240717182920729

image-20240717182857435

image-20240717182958182

数据库连接池

image-20240717191426526

image-20240717191521564

德鲁伊:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

image-20240717191554964

lombok

lombok是一个实用的Java类库,能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,并可以自动化生成日志变量,简化java开发、提高效率。

image-20240717192720905

Mybatis基础操作

删除

image-20240717204053364

package com.itheima.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

//Mapper接口
@Mapper
public interface EmpMapper {

    //    删除员工
    @Delete("delete from emp where id = #{id}")
//    void delete(Integer id);
//    返回删除几条数据
    int delete(Integer id);
}
package com.itheima;

import com.itheima.mapper.EmpMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Test
    void contextLoads() {
        empMapper.delete(17);
    }
}

image-20240717210708749

image-20240717210730651

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

image-20240717210758688

image-20240717210819139

增添

image-20240717213805969

image-20240717213817065

实体类

package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Short gender;
    private String image;
    private Short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

接口

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

//Mapper接口
@Mapper
public interface EmpMapper {
    
//    添加员工
    @Insert("insert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (" +
            "#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    void insert(Emp emp);

}

测试接口

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;
    @Test
    void testAdd() {
//        创建一个实体类emp
        Emp emp = new Emp();
        emp.setUsername("Tom3");
        emp.setName("汤姆3");
        emp.setImage("1.jpg");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntrydate(LocalDate.of(2000,1,1));
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(1);

        empMapper.insert(emp);
    }

}

主键返回

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

image-20240717214308023

image-20240717214638806

更新

Mapper接口

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@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}")
    void update(Emp emp);
}

测试类

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Test
    void testUpdate() {
        Emp emp = new Emp();
        emp.setId(22);
        emp.setUsername("更新操作");
        emp.setName("汤姆666");
        emp.setImage("1.jpg");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntrydate(LocalDate.of(2000,1,1));
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(2);

        empMapper.update(emp);
    }
}

查询

Mapper接口

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    查询员工
    @Select("select * from emp where id = #{id}")
    Emp selectById(Integer id);
}

测试类

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;
    
    @Test
    void testSelect() {
        Emp emp = empMapper.selectById(22);
        System.out.println(emp);
    }
}

运行结果在下面

数据封装

问题:数据库中字段名与实体类中属性名不一致

image-20240719011156267

image-20240719133102585

解决方案

image-20240719133236436

  • 方案一:起别名
@Select("select id, username, password, name, gender, image, job, entrydate, " +
        "dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
Emp selectById(Integer id);
  • 方案二:@Results({@Result()})注解
@Results({
        @Result(column = "dept_id", property = "deptId"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime")
})
@Select("select * from emp where id = #{id}")
Emp selectById(Integer id);
  • 方案三:开启驼峰命名
#开启驼峰命名用于主句封装,前提是数据库字段明明严格采用下划线命名并且实体类属性名采用驼峰明明
mybatis.configuration.map-underscore-to-camel-case=true

然后直接select

传递多个参数

image-20240719133305882

image-20240719133334298

Mapper接口,多个参数要写@Param注解

image-20240719133430405

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    单个参数查询
//    用$代表字串拼接
//    @Select("select * from emp where name like '%${name}%'" +
//            " order by update_time desc ")
//    List<Emp> selectByInfo(String name);

//    多参数要@Param注释
    @Select("select * from emp where name like concat('%', #{name}, '%') and gender = #{gender} and " +
            "entrydate between #{begin} and #{end} order by update_time desc ")
//    List<Emp> selectByInfo(String name, Short gender, LocalDate begin , LocalDate end);
    List<Emp> selectByInfo(@Param("name")String name, @Param("gender")Short gender, @Param("begin")LocalDate begin , @Param("end")LocalDate end);

//    或者多个参数封装到对象里
    @Select("select * from emp where name like concat('%', #{name}, '%') and gender = #{gender} and entrydate >= #{entrydate} order by update_time desc")
    List<Emp> selectMulti(Emp emp);
}
package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;
    
    @Test
    void testSelectByInfo() {
//        测试查找单个参数,带有模糊匹配
//        List<Emp> list = empMapper.selectByInfo((short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
//        List<Emp> empList = empMapper.selectByInfo("张");
//        测试查找多个参数
        List<Emp> empList = empMapper.selectByInfo("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
        System.out.println(empList);
        System.out.println(empList.size());
    }

//    测试查找多个参数
    @Test
    void testSelectMulti() {
        Emp emp = new Emp();
        emp.setName("张");
        emp.setGender((short) 1);
        emp.setEntrydate(LocalDate.of(2010, 1, 1));
        List<Emp> list = empMapper.selectMulti(emp);
        System.out.println(list);
    }
}

可能存在的问题,Mapper传参要写@Param注解,否则传参失败报错

image-20240719134007575

XML映射文件

三点规范:

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

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

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

image-20240719135831555

image-20240719135944597

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.itheima.mapper.EmpMapper">
<!--    resultType:单条记录所封装的类型的全限定类名即Emp类-->
    <select id="selectByInfo" 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>

Mapper中的XML映射

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    xml映射
    List<Emp> selectByInfo(@Param("name")String name, @Param("gender")Short gender, @Param("begin")LocalDate begin , @Param("end")LocalDate end);
}
package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Test
    void testSelectByInfo() {
//        测试查找单个参数,带有模糊匹配
//        List<Emp> list = empMapper.selectByInfo((short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
//        List<Emp> empList = empMapper.selectByInfo("张");
//        测试查找多个参数
        List<Emp> empList = empMapper.selectByInfo("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
        System.out.println(empList);
        System.out.println(empList.size());
    }
}

动态SQL

<if> <where>

where标签可以自动删除语句开头的and

image-20240719152004989

注意SQL语句中的and在每个if开头不可省略

<?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.itheima.mapper.EmpMapper">

<!--    动态查询-->
    <!--    resultType:单条记录所封装的类型的全限定类名即Emp类-->
        <select id="selectByInfo" 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>

mapper

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    xml映射
//    动态查询
    List<Emp> selectByInfo(@Param("name")String name, @Param("gender")Short gender, @Param("begin")LocalDate begin , @Param("end")LocalDate end);
}

test测试类

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Test
    void testSelectByInfo() {

//        动态SQL
//        List<Emp> empList = empMapper.selectByInfo("张", (short) 1, null, null);
//        List<Emp> empList = empMapper.selectByInfo("张", null, null, null);
        List<Emp> empList = empMapper.selectByInfo(null, null, null, null);
        System.out.println(empList);
    }
}

<set>

image-20240719152709497

自动删除语句最后的逗号

image-20240719165836108

动态更新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.itheima.mapper.EmpMapper">

<!--    动态更新操作-->
    <update id="updateDynamic">
        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>

mapper

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    动态修改
    void updateDynamic(Emp emp);
}

测试类

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    //    案例:动态更新
    @Test
    void testUpdateDynamic() {
//        封装的对象属性是根据每一条SQL语句set,不灵活,如果缺少了某条会直接赋值null
        Emp emp = new Emp();
        emp.setId(26);
        emp.setUsername("动态更新");
        emp.setName("动态更新666");
//        emp.setImage("1.jpg");
        emp.setGender((short)2);
        emp.setJob((short)1);
//        emp.setEntrydate(LocalDate.of(2000,1,1));
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(2);

//        empMapper.update(emp);
//        动态更新
        empMapper.updateDynamic(emp);
    }
}

<foreach>

image-20240719172120553

image-20240719172101053

image-20240719172051725

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.itheima.mapper.EmpMapper">

<!--    批量删除-->
<!--
    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>

mapper

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

//Mapper接口
//运行时,会自动生成该接口的实现类对象(动态代理,代理对象),并且将该对象交给IOC容器管理
@Mapper
public interface EmpMapper {
//    xml映射
//    批量删除,返回删除几条数据
    int deleteByIds(@Param("ids") List<Integer> ids);
}

test测试类

package com.itheima;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

//测试类
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
//    依赖注入
//    防止标红 required
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Test
    void testDeleteByIds() {
//        工具类Arrays的asList方法创建集合
        List<Integer> ids = Arrays.asList(13, 14, 15);
        int cnt = empMapper.deleteByIds(ids);
        System.out.println(cnt);
    }
}

<sql><include>

  • :定义可重用的 SQL 片段。

  • :通过属性refid,指定包含的sql片段。

以动态查询为例

<?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.itheima.mapper.EmpMapper">

<!--    sql片段-->
    <sql id="commonSelect">select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp</sql>

<!--    动态查询-->
        <select id="selectByInfo" resultType="com.itheima.pojo.Emp">
            <!--    引入sql片段-->
            <include refid="commonSelect"/>
            <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>

标签:name,笔记,学习,emp,org,Mybatis,import,com,itheima
From: https://www.cnblogs.com/bfs1201/p/18312464

相关文章

  • 【读书笔记】余华 - 活着
    这本书我是通过微信读书听的有声书,我还粗略的把整个故事给孩子和母亲讲了一遍,给母亲讲的时候有些名字忘记了,还通过ChatGPT整理了一下主要是人物和事件。故事里让我印象最深的是村里开始搞土地改革,龙儿死到临头了才泄气,被五花大绑从福贵身边走过的时候哭着鼻子喊“福贵,我是替......
  • 【数据结构】学习day 1.1线性表中的顺序表
    1 "&"区别(c++)#include<stdio.h>voidtest(intx){   x=2024;   printf("intestx=%d\n",x);}intmain(){   intx=1;   printf("beforetestx=%d\n",x);   test(x);   printf("aftertestx=%d\n"......
  • C#学习笔记
    【持续更新】【2024-07-19】新建对象初始化语句的执行总是在构造函数执行之后,如:classPoint{publicintX=1;publicintY=2;publicPoint(intx,inty){X=x;Y=y;}}c......
  • 学习Java的第五天(2024.7.18)
    1.字符串类:String类String类:是引用类型,默认值为null(注意不是空串"")字符串的声明:publicstaticvoidmain(String[]args){//声明字符串Stringstr="abc你好";System.out.println(str);str=newString("");//和strnewString();输出结果都......
  • 学习Java的第六天(2024.7.19)
    1.容器类、集合类之前学过的容器:数组,但是数组有局限:1.数组存储的数据类型有限制2.数组存储的长度受限2.容器类分为:List,Set,Map3.List类:List是一个接口,他的实现类有:ArrayList,LinkedList,Vectorpublicstaticvoidmain(String[]args){Listlist=newArrayLi......
  • 广义容斥原理学习
    广义容斥原理概念广义容斥原理解决的是:计算所有至少满足\(k\)个性质的方案数之和。同样的,我们可以通过计算所有至少满足\(k\)个性质的方案数之和来计算恰好满足\(k\)个性质的方案数。方法设\(\alpha(k)\)表示所有至少满足\(k\)个性质的方案数之和。容易得到:\[\al......
  • 花几千上万学习Java,真没必要!(十四)
    方法:测试代码1:不带参数,不带返回值的方法packagetestmethod.com;publicclassTestClassMethod{//定义一个不带参数且不带返回值的方法publicstaticvoidmyMethod(){//在这里编写方法体System.out.println("Thisismymethod");}......
  • javaScrip的学习(一)
    目录引言一、java和JavaScript的联系二、js中的弹出框1.alert弹出框2.confirm带确认取消的按钮弹框3.prompt带有提示信息且带有输入框的弹框 4.输出到网页中​三、js引入方式1.放在script标签中2.放在外部js文件中四、执行顺序五、书写规范1.语句结尾加分号......
  • 整体二分学习笔记
    整体二分一般适用于解决可以若干次二分解决的问题,当进行若干次二分的复杂度无法接受时,就可以使用整体二分。可以使用整体二分解决的题目需要满足以下性质:1.询问的答案具有可二分性2.修改对判定答案的贡献互相独立,修改之间互不影响效果3.修改如果对判定答案有贡献,则贡献为一......
  • sklearn中的增量学习:特征提取的艺术
    sklearn中的增量学习:特征提取的艺术在机器学习领域,特征提取是构建有效模型的关键步骤。然而,并非所有数据集都适合一次性加载到内存中进行处理,尤其是在处理大规模数据集时。Scikit-learn(sklearn)提供了一些支持增量学习的模型,允许用户逐步地从数据中学习并提取特征。本文将详......