发表于: 2018-03-05 20:43:54

1 650


今天完成的事情:

学习Java基础——I/O流

Java的FILE类以及常用方法

       文件和文件夹都是用File代表

       使用相对路径会这绝对路径创建File对象

         文件常用方法:renameTo方法用于对物理文件名称进行修改,但是并不会修改File对象的name属性

     概念:流就是一系列的数据 

     当不同的介质之间有数据交互的时候,Java就使用流来实现,数据源可以是文件,还可以是数据库,网络甚至是其他的程序

    比如读取文件的数据到程序中,站在程序的角度来看,就叫做输入流

          输入流:InputStream;输出流:OutStream

    文件输入流,如下代码就建立了一个文件输入流,这个流可以用来把数据从硬盘的文件,读取到JVM(内存)

字节流

        ASCII码:所有的数据存放在计算机都是以数字的形式存放的。所以字母就需要转换为数字才能够存放.ASCII是这样的一种码表。字包含简单的英文字母,符号,数字等。不包含中文德文

       以字节流的形式读取文件内容:

inputStream是字节输入流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。

FileInputStream是InputStream子类,以FileInputStream为例进行文件读取

         以字节流的形式向文件写入数据

OutputStream是字节输出流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。

FileOutputStream是OutputStream子类,以FileOutputStream为例向文件写出数据

关闭流的方式:所有的流,无论是输入流还是输出流,使用完毕之后,都应该关闭。否则会产生对资源占用的浪费,当流量比较大的时候,会影响到业务的正常开展

      1  在try中关闭

          在try的作用域里关闭文件输入流,这样做有一个弊端:如果文件不存在,或者读取的时候出现问题而抛出异常,那么就不会执行这一行关闭流的代码,存在巨大的资源浪费,不建议使用

     2  在finally中关闭

          这是标准的关闭流的方式

          1 首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally 

          2 在finally 关闭之前,要先判断该引用是否为空

          3 关闭的时候,需要再一次进行try catch处理

      3 使用try()的方式

            把流定义在try()里,trycatch或者finally结束的时候,会自动关闭,这种编写代码的方式叫做try-with-resource,所有的流,都实现了一个接口叫做AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。并且在try,catch,finally结束的时候自动关闭,回收相关资源

字符流

        Reader字符输入流;Writer字符输出流专门用于字符的形式读取和写入数据

       1 FileReader是Reader子类,以FileReader为例进行文件读取

           2  FileWriter是Writer的子类,以FileWriter为例把字符串写入到文件

Java缓存流

       以介质是硬盘为例,字节流和字符流的弊端:在每一次读写的时候,都会访问硬盘。如果表现频率比较高的时候,其性能表现不佳。因此使用缓存流解决以上弊端。

      缓存流在读取的时候,会一次性读较多的数据到缓存中,以后每一次的读取,都是在缓存中访问,直到缓存中的数据读取完毕,再到硬盘中去取。就好比吃饭,不用缓存就是每吃一口都到锅里去铲。用缓存就是先把饭盛到碗里,碗里的吃完了,再到锅里去铲。

      缓存流在写入数据的时候,会先把数据写入到缓存区,直到缓存区达到一定的量,才把这些数据,一起写入到硬盘中去。按照这种操作模式,就不会像字节流,字符流那样每写一个字节都访问硬盘,从而减少了IO操作

      使用缓存流读取数据:缓存字符输入流BufferedReader可以一次读取一行数据

          使用缓存流写出数据:PrintWriter缓存字符输出流,可以一次写出一行数据

       Flush:有时候,需要立即把数据写入到硬盘,而不是等缓存满了才写出来。

数据流

      直接进行字符串的读写:使用数据流的writeUTf()和readUTF可以进行数据的格式化顺序读写


对象流

      对象流指的是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘。一个对象以流的形式进行传输,叫做序列化。该对象所对应的类,必须是实现Serializable接口

      System.out是常用的在控制台输出数据的;System.in可以从控制台输入数据

 

mybatis框架跑起来

pom.xml

<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>fourth</groupId>
   <artifactId>fourthtest</artifactId>
   <version>1.0-SNAPSHOT</version>
   <build>
       <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
       </resources>
   </build>
   <!--<build>-->
       <!--<plugins>-->
           <!--<plugin>-->
               <!--<groupId>org.apache.maven.plugins</groupId>-->
               <!--<artifactId>maven-compiler-plugin</artifactId>-->
               <!--<configuration>-->
                   <!--<source>1.6</source>-->
                   <!--<target>1.6</target>-->
               <!--</configuration>-->
           <!--</plugin>-->
       <!--</plugins>-->
   <!--</build>-->

   <packaging>jar</packaging>

   <name>fourthtest</name>
   <url>http://maven.apache.org</url>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
       </dependency>
       <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.45</version>
       </dependency>

       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.3.0</version>
       </dependency>

       <!--<dependency>-->
           <!--<groupId>log4j</groupId>-->
           <!--<artifactId>log4j</artifactId>-->
           <!--<version>1.2.17</version>-->
       <!--</dependency>-->
       <!--<dependency>-->
           <!--<groupId>org.apache.logging.log4j</groupId>-->
           <!--<artifactId>log4j-api</artifactId>-->
           <!--<version>2.2</version>-->
       <!--</dependency>-->
       <!--<dependency>-->
           <!--<groupId>org.apache.logging.log4j</groupId>-->
           <!--<artifactId>log4j-core</artifactId>-->
           <!--<version>2.2</version>-->
       <!--</dependency>-->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>RELEASE</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>RELEASE</version>
       </dependency>
   </dependencies>

</project>

 sqlMapConfig.xml

<?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>
   <!-- 和Spring整合后environment配置都会被干掉 -->
   <environments default="development">
       <environment id="development">
           <!-- 使用jdbc事务管理,目前由mybatis来管理 -->
           <transactionManager type="JDBC" />
           <!-- 数据库连接池,目前由mybatis来管理 -->
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver" />
               <property name="url" value="jdbc:mysql://127.0.0.1:3306/mydatabases?characterEncoding=UTF-8" />
               <property name="username" value="root" />
               <property name="password" value="root" />
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <mapper resource="sqlmap1/User.xml" />
   </mappers>
</configuration>

User.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="sqlmap1.UserMapper">
   <!-- 需求:通过id查询用户 -->
   <select id="findStudentById" parameterType="int" resultType="fourth.com.student">
       select * from student where id = #{id}
</select>
</mapper>

UserMapperTest.java

package sqlmap1;


import junit.framework.TestCase;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import fourth.com.student;

import java.io.InputStream;

public class UserMapperTest extends TestCase {
private SqlSessionFactory sqlSessionFactory;
   
   @Before
   public void setUp() throws Exception {
String resource = "SqlMapConfig.xml"; //mybatis配置文件
       //得到配置文件的流
       InputStream inputStream = Resources.getResourceAsStream(resource);
       //创建会话工厂SqlSessionFactory,要传入mybatis的配置文件的流
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   }

@Test
   public void testFindStudentById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
       //创建StudentMapper对象,mybatis自动生成mapper代理对象
       UserMapper UserMapper = sqlSession.getMapper(UserMapper.class);
       student student = UserMapper.findStudentById(10);
       System.out.println(student);
   }
}

明天计划的事情:

使用mybatis完成增删改查

 
遇到的问题:

文件创建错误,一开始无法新建类和文件夹

mybatis无法识别路径

 
收获:

mybatis流程


IDEA更改各种注释的颜色


IDEA更改快捷键


返回列表 返回列表
评论

    分享到