java基础知识

StringBuilder 操作字符串
/* 
  public StringBuilder append(任意类型): 添加数据,并返回对象本身
  public StringBuilder reberse() : 返回相反的字符序列
  public int length(): 返回长度(字符出现的个数)
  publis String toString(): 通过toString() 可以实现把StringBuilder转换为String
*/
StringBuilder res = new StringBuilder();
res.append("xx");//添加数据
res.reverse();//翻转字符串
res.length();//返回长度(字符出现的个数)
res.toString();//转换为String类型

常用API
Random  //随机数
Math    //数学运算
System	//System 类包含一些有用的类字段和方法。它不能被实例化。	
Object  //所有类的父类
  toString
  equals  //也是使用的== 进行比较   比较的是内存地址  重写可以比较内容,自动生成
BigDecimal // 精确计算   使用字符形式
  //特殊:divide(另一个BigDecimal对象,精确几位,舍入模式)
自动装箱,自动拆箱
数组的高级操作
  int max = arr.length - 1;
          int min = 0;
          while (min <= max){
              int mid = (min + max) >> 1;
              if (arr[mid] > num){
                  max = mid -1;
              }else if (arr[mid] < num){
                  min = mid + 1;
              }else{
                  return mid;
              }
          }
          return -1;
   for (int i = 0; i < arr.length - 1 ; i++) {
              for (int j = 0;j < arr.length - 1 - i;j++){
                  if (arr[j] > arr[j + 1]){
                      int temp = arr[j];
                      arr[j] = arr[j + 1];
                      arr[j + 1] = temp;
                  }
              }
          }
          return arr;
时间
Date()  //当前时间
Date(毫秒) //根据毫秒获取日期   long格式
SimpleDateFormate //格式化Date日期  与  解析日期格式的日期
异常
异常处理

throws

集合 Collection
集合
遍历集合:Iterator
File类 表示文件的位置,对文件以及文件夹进行操作
File path = new File("C:\\test\\a.txt");
boolean res = path.createNewFile();   //只能创建文件
//如果文件存在,不创建,返回false
//如果文件不存在,创建成功后,返回true
IO 对硬盘中的文件进行读写

Maven学习

命令
mvn compile //编译
mvn clean   //删除编译
mvn test    //做测试使用的
mvn package //打包
mvn install //安装到本地仓库
UTF-8问题
<project.build.sourceEncoding>
    UTF-8
</project.build.sourceEncoding>
Assert弃用
POMxml配置项先关信息
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <!--指定pom的模型版本-->
  <modelVersion>4.0.0</modelVersion>
  <!--打包方式,web为war包,java程序为jar包-->
  <packaging>war</packaging>

  <name>web01</name><!--可以删掉-->
<!--  组织id-->
  <groupId>cn.zhangfayuan</groupId>
<!--  项目id-->
  <artifactId>web01</artifactId>
<!--  版本号   release (完成版)   snapshot(开发板)-->
  <version>1.0-SNAPSHOT</version>
<!--设置当前工程的所有依赖-->
  <dependencies>
<!--    具体的依赖-->
<!--    <dependency>-->
<!--      <groupId></groupId>-->
<!--      <artifactId></artifactId>-->
<!--    </dependency>-->
  </dependencies>

  <!--构建插件-->
  <build>
    <plugins>
      <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.1</version>
        <configuration>
          <port>8088</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

依赖管理
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
  </dependency>
</dependencies>
依赖传递
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
  </dependency>
  <!-- 依赖传递,同样使用依赖管理的格式,在project02中的依赖,当前引入的程序中也可以进行使用 -->
  <dependency>
    <groupId>cn.zhangfayuan</groupId>
    <artifactId>project02</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>
可选依赖(不透明,隐藏依赖)
 <!-- 依赖传递,同样使用依赖管理的格式,在project02中的依赖,当前引入的程序中也可以进行使用 -->
  <dependency>
    <groupId>logkit</groupId>
    <artifactId>logkit</artifactId>
    <version>1.0.1</version>
    <optional>true</optional><!-- 可选依赖 -->
</dependency>
排除依赖
 <dependency>
   <groupId>cn.zhangfayuan</groupId>
   <artifactId>project02</artifactId>
   <version>1.0-SNAPSHOT</version>
   <exclusions>
     <exclusion><!-- 排除依赖 -->
       <groupId>logkit</groupId>
       <artifactId>logkit</artifactId>
     </exclusion>
   </exclusions>
</dependency>
依赖的范围
简介

image-20211115173641966

配置

scope

依赖传递的范围

image-20211115174157651

Maven项目构建生命周期

IOC 入门案例

步骤
Ioc配置
bean

image-20211115194623451

image-20211115194923118

image-20211115200047251

MyBatis学习

设置

1、pom文件中引入相关坐标

或者是以内包 libs目录下,并加入包

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.25</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.5.7</version>
</dependency>
2、新增配置文件

UsersMapper.xml [src目录下 或 resources资源目录下]

<?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="UsersMapper">
    <select id="getAll" resultType="users">  <!-- 使用了类别名,全名 cn.pithy.bean.Users  -->
        SELECT * FROM users
    </select>

    <select id="findById" resultType="cn.pithy.bean.Users" parameterType="java.lang.Integer">
        SELECT * from users where id = #{id}
    </select>

    <insert id="insert" parameterType="cn.pithy.bean.Users" useGeneratedKeys="true" keyProperty="id">
        INSERT into users (username, password) values (#{username},#{password})
    </insert>

    <update id="update" parameterType="cn.pithy.bean.Users">
        UPDATE users set username = #{username},password=#{password} where id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        DELETE from users where id = #{id}
    </delete>
</mapper>
3、新增核心配置文件

MybatisConfig.xml [src目录下 或 resources资源目录下]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
  <!-- 类名起别名 -->
   <typeAliases>
        <typeAlias type="cn.pithy.bean.Users" alias="users" />
    </typeAliases>
  
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="jdbc"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:8889/sprinigboot_data"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UsersMapper.xml" />
    </mappers>
</configuration>
4、编写对象类
5、编写程序
public class UsersDao {

    public List<Users> getAll() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        //实例化Sql session工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂对象获取sql session 对象
        //        SqlSession sqlSession = build.openSession(true); 自动提交事务
        SqlSession sqlSession = build.openSession();
        //执行xml中的sql语句
        List<Users> users = sqlSession.selectList("UsersMapper.getAll");
        //释放资源
        sqlSession.close();
        resourceAsStream.close();
        return users;
    }

    public Users getById(int id) throws IOException{
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //        SqlSession sqlSession = build.openSession(true); 自动提交事务
        SqlSession sqlSession = build.openSession();
        Users user = sqlSession.selectOne("UsersMapper.findById", id);
        sqlSession.close();
        resourceAsStream.close();
        return user;
    }

    public void insert() throws IOException{
        InputStream resourceAsStream = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//        SqlSession sqlSession = build.openSession(true); 自动提交事务
        SqlSession sqlSession = build.openSession();
        Users user = new Users();
        user.setUsername("woCao");
        user.setPassword("99999999");

        int insert = sqlSession.insert("UsersMapper.insert", user);
        if (insert > 0){
            sqlSession.commit();
            System.out.println("Success" + insert + user.getId());
        }else {
            System.out.println("Error" + insert);
        }
        sqlSession.close();
        resourceAsStream.close();
    }

    public void update() throws IOException{
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Users user = sqlSession.selectOne("UsersMapper.findById", 6);
        user.setUsername("woCaoLei哈哈哈");
        int update = sqlSession.update("UsersMapper.update",user);
        if (update > 0){
            System.out.println("Success" + update + user.getId());
        }else {
            System.out.println("Error" + update);
        }
        sqlSession.close();
        resourceAsStream.close();
    }

    public void delete(int id) throws IOException{
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        int delete = sqlSession.delete("UsersMapper.delete", id);
        if (delete > 0){
            System.out.println("Success" + delete );
        }else {
            System.out.println("Error" + delete);
        }
        sqlSession.close();
        resourceAsStream.close();
    }
}
相关配置项

image-20211117140408490

传统方式实现DAO层

控制层=》业务层=》接口层

package

—-dao

—-controller 控制层

​ |—-UsersController.java

—-service 业务接口层

​ |—-impl

​ |—-UsersServiceImp.java 业务接口实现

​ |—-UsersService.java 业务接口类

—- mapper 持久层接口、

​ |—-impl *** 接口代理方式实现的话 这个目录不要***

​ |—- UsersMapperImpl.java 接口的实现类

resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
sqlSession = build.openSession(true);
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //注意这个部分
list = mapper.getAll();

​ |—-UsersMapper.java 接口类 *** 定义接口类以及实现方法 ***

LOG4J 打印sql信息

设置步骤
  1. 导入jar包【pom文件中加入LOG4J坐标】

  2. 配置核心配置文件

   <!--  集成LOG4J日志  -->
   <settings>
     <setting name="logImpl" value="log4j"/>
   </settings>
  1. src目录下配LOG4J配置文件【maven项目配置在resources资源目录下】

image-20211117143340210

MyBatis 进阶

接口代理的方式实现DAO持久层

image-20211117152541396

分页(使用查看文档)

image-20211117164035061

一对一查询

封装映射对象关系

image-20211117172438921

一对多查询

image-20211118104712273

多对多查询

image-20211118105415466

MyBatis高级操作

MyBatis注解开发

常用单标操作注解

注解实现多表操作

一对一操作

MyBatis-plus+SpringBoot

1、安装SpringBoot

2、安装依赖

  <!--        引入mybatis依赖-->
          <dependency>
              <groupId>com.baomidou</groupId>
              <artifactId>mybatis-plus-boot-starter</artifactId>
              <version>3.4.3.4</version>
          </dependency>
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>druid</artifactId>
              <version>1.2.8</version>
          </dependency>

3、入口文件扫描Dao

@SpringBootApplication
@MapperScan("cn.pithy.dao")
public class SpringTest1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringTest1Application.class, args);
    }

}

4、编写配置文件

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_data?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=zha

5、编写类

package cn.pithy.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor  //有参构造
@NoArgsConstructor	//无参构造
@ToString //tostring
@Accessors(chain = true)  //开启链式操作
public class Users {
    private int id;
    private String name;
    private int age;
    private String address;
    private String phone;
}

6、创建dao接口,继承BaseMapper

7、可以使用了

SpringBoot配置

读取配置内容

@Value
@Value("${message1}")
private String message1;
@RequestMapping("/getValue")
public String getValue()
{
  String envGetName =  env.getProperty("name");
  return envGetName;
}

Environment
@Autowired
private Environment env;

@RequestMapping("/getValue")
public String getValue()
{
  String envGetName =  env.getProperty("name");
  return envGetName;
}
   
    
@ConfigurationProperties
  package cn.pithy.springbootinit;
  
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.stereotype.Component;
  
  /**
   * @author zhangfayuan
   */
  @Component
  @ConfigurationProperties(prefix = "persion")
  public class Persion {
  
      private String name;
      private int age;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
      @Override
      public String toString() {
          return "Persion{" +
                  "name='" + name + '\'' +
                  ", age=" + age +
                  '}';
      }
  }
  
  @Autowired
  private Persion persion;
  @RequestMapping("/getValue")
  public String getValue()
  {
    return persion.toString();
  }
  

profile不同环境切换

1、配置方式
多profile配置方式
## -- application.properties
spring.profiles.active=pro

## -- application-dev.properties
server.port=8081

## -- application-test.properties
server.port=8082

## -- application-pro.properties
server.port=8083
yml多文档方式
---
server:
  port: 8081
spring:
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: test
---
server:
  port: 8083

spring:
  profiles: pro
---
spring:
  profiles:
    active: pro
2、profile激活方式
配置文件

上面的方式

虚拟机参数

-Dspring.profiles.active=dev

命令行参数

--spring.profiles.active=dev

注解大全

url: jdbc:mysql://47.92.25.174:3336/ai_algo_aat?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=CTT&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: algo
password: algo123QAZ

目录结构