云淡风轻

这, 只是一种可能, 未必是唯一的可能!


  • 首页

  • 归档

  • 分类

  • 标签

一篇文章学会JDBC

发表于 2019-11-02 | 分类于 java , JDBC
字数统计: 2.9k 字 | 阅读时长 ≈ 13 分

什么是JDBC?

JDBC是Java DataBase Connectivity的简称, 是用来规范访问数据库的应用程序接口. 面向关系型数据库.

JDBC与MySQL的配合使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) throws Exception {
// write your code here
// 1. 导入jar包
// 2. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");

// 3. 获取数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LCLDataBase", "root", "root");

// 4. 定义sql语句
String sql = "update t_user set phone = '13611110101', name = 'kevin' where id = 1";

// 5. 获取执行sql的对象 statement
Statement stmt = conn.createStatement();

// 6. 执行sql语句
int result = stmt.executeUpdate(sql);

// 7. 处理结果
System.out.println(result);

// 8. 释放资源
stmt.close();
conn.close();
}

详解各个JDBC对象

DriverManager: 驱动管理对象

功能

  • 负责加载各种不同驱动程序

    • 根据不同的请求, 向调用者返回相应的数据库连接

      getConnection(String url, String user, String password)

      • url : 指定数据库路径

        ​ jdbc:mysql://ip地址(域名):端口号/数据库名称

        ​ jdbc:mysql://localhost:3306/mysqlName

      • user: 用户名

      • password: 密码

Connection : 数据库连接对象

功能

  • 负责与数据库间的通讯
    • SQL语句的执行
    • 管理事务
      • 开启事务
      • 提交事务
      • 回滚事务

Statement : 用以执行SQL查询和更新 (静态SQL语句和单次执行)

执行sql语句

  • boolean execute(): 可以执行任意sql语句

  • int executeUpdate(): 可以执行DML语句, DDL语句

    返回值: 影响的行数, 可以通过该值进行判断sql语句是否执行成功, 大于0则成功

  • ResultSet executeQuery(): 执行DQL语句

ResultSet : 结果集合对象

PreparedStatement: 用以执行包含动态参数的SQL查询和更新 (可以重复执行)

​ sql注入问题, 在拼接sql时, 有一些sql的特殊关键字参与字符串的拼接. 造成安全性问题

步骤

​ 1. 用户名随便写, 密码: a’ or ‘a’ = ‘a

​ 2. sql语句为: select * from t_student where name = ‘lll’ and password = ‘a’ or ‘a’ = ‘a’

​ 3. preparedStatement sql语句: select * from t_student where name = ? and password = ?

​ 4. 给?设置参数

​ setXXX(参数1, 参数2); // 参数1: ?的位置, 从1开始, 参数2: ?的值

优点

1. 可以防止sql注入
2. 效率更高

SQLException 数据库连接的建立和关闭和SQL语句的执行过程中发生了例外情况

JDBC之CRUD的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        Connection conn = null;
Statement stmt = null;
try {
// 1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");

// 2. 获取连接对象
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LCLDataBase", "root", "root");

// 3. 获取statement
stmt = conn.createStatement();

// 4. 创建sql语句
// 添加数据
// String sql = "insert into t_user values (null, 'xixi', '1361110201')";

// 修改数据
// String sql = "update t_user set phone = '13611110102' where id = 7";

// 删除数据
String sql = "delete from t_user where id = 5";

// 5. 执行sql语句
int result = stmt.executeUpdate(sql);

// 6. 查看结果
System.out.println(result);
if (result == 0) {
System.out.println("失败");
} else {
System.out.println("成功");
}

// 7. 释放资源
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 当stmt不为空的时候, 才进行释放
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

// 当conn不为空的时候, 才进行释放
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Connection conn = null;
Statement stmt = null;
try {
// 1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");

// 2. 获取连接对象
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LCLDataBase", "root", "fengqing2018");

// 3. 获取statement
stmt = conn.createStatement();

// 4. 创建sql语句

// 查询数据
String sql = "select * from t_user";

// 5. 执行sql语句
ResultSet result = stmt.executeQuery(sql);

// 6. 查看结果
System.out.println(result);
while (result.next()) {
int id = result.getInt(1);
String name = result.getString(2);
String phone = result.getString(3);
System.out.println(id + ":" + name + ":" + phone);
}

// 7. 释放资源
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 当stmt不为空的时候, 才进行释放
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

// 当conn不为空的时候, 才进行释放
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

JDBCTools 工具类的创建

步骤

  1. 抽取注册驱动

  2. 抽取一个方法获取连接对象

  3. 定义配置文件 (不想传递参数, 还得保证工具类的能用性)

    配置文件, 名称为: jdbc.properties

    ​ url = jdbc:mysql:///LCLDataBase

    ​ user = root

    ​ password = root

  4. 抽取一个方法释放资源

  5. 敲代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class JDBCTools {

private static String url;
private static String user;
private static String password;
private static String driver;

// 构造代码块
static {
try {
// 获取资源
Properties pro = new Properties();

// 获取资源路径
ClassLoader cl = JDBCTools.class.getClassLoader();
URL rsUrl = cl.getResource("jdbc.properties");
pro.load(new FileReader(rsUrl.getPath()));

// 取出配置文件中的参数
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");

// 注册驱动
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}

/* 获取连接 */
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}

/*
* 释放资源
* */
public static void close(Statement stmt, Connection conn) {
close(null, stmt, conn);
}

/*
* 释放资源
* */
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

练习

​ 录入用户名和密码

​ 提示示用户是否登录成功

步骤

  1. 创建表

    1
    2
    3
    4
    5
    CREATE TABLE t_student (
    id INT PRIMARY KEY auto_increment,
    name VARCHAR(32),
    password VARCHAR(32)
    );
  2. 添加数据

    1
    2
    INSERT INTO t_student VALUE (null, 'zhangsna', '123456');
    INSERT INTO t_student VALUES (null, 'lisi', '654321');
  3. 写代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    public static void main(String[] args) {

    // 输入的用户名和密码
    Scanner sc = new Scanner(System.in);

    // 输入用户名
    System.out.println("请输入用户名");
    String name = sc.nextLine();

    // 输入密码
    System.out.println("请输入密码");
    String password = sc.nextLine();

    // 调用方法
    boolean res = new Login().login2(name, password);

    // 判断是否成功
    if (res) {
    System.out.println("登录成功");
    } else {
    System.out.println("用户名或密码错误");
    }
    }

    public boolean login2(String name, String password) {
    ResultSet rs = null;
    PreparedStatement stmt = null;
    Connection conn = null;
    try {
    // 获取连接对象
    conn = JDBCTools.getConnection();

    // 查找数据的sql语句
    String sql = "select * from t_student where name = ? and password = ?";

    // 执行sql语句
    stmt = conn.prepareStatement(sql);

    // 给?赋值
    stmt.setString(1, name);
    stmt.setString(2, password);

    System.out.println(sql);

    // 执行sql语句
    rs = stmt.executeQuery();

    // 返回结果
    return rs.next();

    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    // 释放资源
    JDBCTools.close(rs, stmt, conn);
    }
    return false;
    }

    public boolean login(String name, String password) {
    ResultSet rs = null;
    Statement stmt = null;
    Connection conn = null;
    try {
    // 获取连接对象
    conn = JDBCTools.getConnection();
    stmt = conn.createStatement();

    // 查找数据的sql语句
    String sql = "select * from t_student where name = '" + name + "' and password = '" + password + "'";

    System.out.println(sql);

    // 执行sql语句
    rs = stmt.executeQuery(sql);

    // 返回结果
    return rs.next();

    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    // 释放资源
    JDBCTools.close(rs, stmt, conn);
    }
    return false;
    }

    注意

    当我们使用login()的时候会有sql注入问题, 因为它是执行静态sql语句, 我们以后要使用logn2()的方法, 使用动态参数的sql语句

数据库连接池

什么是数据库连接池

其实就是一个容器(集合), 存放数据库连接的容器

当系统初始化好后, 容器被创建, 容器中会申请一些连接对象, 当用户来访问数据库时, 从容器中获取连接对象, 用户访问完之后, 会将连接对象归还给容器

好处

1. 节约资源
2. 高效

C3P0连接池技术

步骤

  1. 导入jar包

  2. 定义配置文件 c3p0.properties 或者 c3p0-config.xml, 放在src目录下既可

    内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <!DOCTYPE c3p0-config [
    <!ENTITY extra SYSTEM "https://www.mchange.com/projects/c3p0/extra.xml">
    ]>

    <c3p0-config>
    <default-config>
    <!-- 驱动 -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>

    <!-- 数据库地址 -->
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/LCLDataBase</property>

    <!-- 用户名 -->
    <property name="user">root</property>

    <!-- 密码 -->
    <property name="password">fengqing2018</property>

    <!-- 超时时间 -->
    <property name="checkoutTimeout">3000</property>

    <!-- 最大连接数量 -->
    <property name="maxPoolSize">10</property>
    </default-config>
    </c3p0-config>
  1. 创建核心对象, 数据库连接池对象

  2. 获取连接对象, getConnection()

代码

1
2
3
4
5
6
7
8
// 创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();

// 获取连接对象
Connection conn = ds.getConnection();

// 打印连接对象
System.out.println(conn);

注意

c3p0自动加载src目录下的c3p0-config.xml配置文件

druid数据库连接池技术

  1. 导入jar包

  2. 定义配置文件

    内容

    1
    2
    3
    4
    driverClassName = com.mysql.cj.jdbc.Driver
    url = jdbc:mysql:///LCLDataBase
    username = root
    password = root
  1. 加载配置文件

  2. 获取数据库连接池对象

  3. 获取连接对象

代码

1
2
3
4
5
6
7
8
9
10
11
// 通过加载配置文件, 创建数据库连接池对象
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
DataSource ds = DruidDataSourceFactory.createDataSource(pro);

// 获取数据库连接对象
Connection conn = ds.getConnection

// 打印连接对象
System.out.println(conn);

JDBCUtils工具类的创建

  1. 定义一个类JDBCUtils
  2. 提供静态代码块加载配置文件, 初始化连接池对象
  3. 提供方法
    • 获取连接方法
    • 释放资源
    • 获取连接池方法

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class JDBCUtils {
private static DataSource ds;

// 静态代码块 加载配置文件, 获取数据库连接池对象
static {
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}

/*
* 获取连接
* */
public static Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

/*
* 释放资源
* */
public static void close(Statement stmt, Connection conn) {
close(null, stmt, conn);
}

public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/*
* 获取连接池对象
* */
public static DataSource getDataSource() {
return ds;
}
}

Spring JDBC

spring提供的jdbc的简单封装, 方便我们进行方法的调用, 当然我们要理解它的实现原理

  1. 导入jar包

  2. 创建JDBCTemplate对象

  3. 调用方法来完成CRUD的操作

    update() DML语句, 增, 删, 改语句

    queryForMap(), 将查询的结果封装为map集合

    queryForList(), 将查询的结果封装为list集合

    queryForObject(), 将查询的结果封装为对象

    query(), 将查询的结果封装为JavaBean对象

练习

  1. 修改一条记录
  2. 添加一条记录
  3. 删除一条记录
  4. 查询所有记录, 将其封装为map集合
  5. 查询所有记录, 将基封装为list集合
  6. 查询所有记录, 将其封装为Student对象的List集合

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class SpringJdbc {
// 1. 导入jar包
// 2. 创建JdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

/*
* 更新一条记录
*/
@Test
public void update() {

// 3. 创建sql语句
String sql = "update t_student set password = '555' where id = ?";

// 4. 执行sql语句
int rs = template.update(sql, "4");

// 5. 查看结果
System.out.println(rs);
}

/*
* 添加一条记录
*/
@Test
public void insert() {

// 3. 创建sql语句
String sql = "insert into t_student values (null, ?, ?)";

// 4. 执行sql语句
int rs = template.update(sql, "zero", "333");

// 5. 查看结果
System.out.println(rs);
}

/*
* 删除一条记录
*/
@Test
public void delete() {

// 3. 创建sql语句
String sql = "delete from t_student where id = ?";

// 4. 执行sql语句
int rs = template.update(sql, "5");

// 5. 查看结果
System.out.println(rs);
}

/*
* 查询出一条记录, 将结果封装为map集合
*/
@Test
public void queryForMap() {
String sql = "select * from t_student where id = 1";
Map<String, Object> map = template.queryForMap(sql);
System.out.println(map);
}

/*
* 查询出多条记录, 将结果封装成元素为map集合的list集合
*/
@Test
public void queryForList() {
String sql = "select * from t_student where id between 1 and 2";
List<Map<String, Object>> list = template.queryForList(sql);
System.out.println(list);
}

/*
* 查询出多条记录, 将结果封装成元素为自定义对象的list集合
*/
@Test
public void queryForObjectList() {
String sql = "select * from t_student";
List<Student> list = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
System.out.println(list);
}

/*
* 查询数据个数
*/
@Test
public void queryCount() {
String sql = "select count(id) from t_student";
Long total = template.queryForObject(sql, Long.class);
System.out.println(total);
}
}

至此, JDBC的基本使用就已经学完了! 希望对大家能有所帮助!

数据结构第1集 - 基础知识

发表于 2019-10-28 | 分类于 数据结构
字数统计: 1.8k 字 | 阅读时长 ≈ 6 分

引言

计算机解决问题的步骤

1. 从具体的问题抽象出一个适当的数学模型
2. 设计一个求解该数学模型的算法
3. 用计算机语言编写实现该算法的程序, 调试和运行程序直至最终得到问题的解答

数据结构 (Data Structure)

数据结构是组织数据和存储数据的方式

数据结构是指一组相互之间存在一种或多种特定关系的数据的组织方式和它们在计算机内的存储方式, 以及定义在该组数据上的一组操作.

数据的逻辑结构

数据及数据的组织方式被称为逻辑结构

数据结构和算法和程序的关系

程序 = 数据结构 + 算法

基本概念和术语

数据 (Data)

所有能被计算机存储处理的对象

数据元素 (Data Element)

数据为集合, 集合当中一个个的个体即为该集合的一个元素. 数据元素是数据的基本单位

数据项 (Data Item)

数据元素常常还由若干个数据项组成. 它是数据不可分割的最小标识单位.

数据的逻辑结构 (Logical Structure)

数据元素之间的结构关系

逻辑关系: 指数据元素之间的关联方式或邻接关系

集合

任意两个点之间都没有邻接关系, 组织形式松散

线性结构

结点按逻辑关系依次排列成一条链, 结点之间一个一个依次相邻接.

树形结构

具有分支, 层次特性, 上层的结点可以和下层多个结点相邻接, 但下层结点只能和上层的一个结点相邻接.

图结构

最复杂, 任意两个结点都可以相邻接

数据的存储结构 / 数据的物理结构 (Physical Structure)

数据的逻辑结构在计算机中的实现称为数据的存储结构(物理结构)

数据结构的存储 = 数据元素的存储 + 逻辑关系的存储

顺序结构

顺序存储方式: 借助数据元素的相对存储位置来表示数据的逻辑关系

方法: 将所有存储结点存储到一片连续的存储区

特点

1. 预先分配好长度, 需要预估存储数据需要的存储量
2. 插入和删除需要移动其它元素
3. 存取快捷, 是随机存取结构

链式结构

链式存储方式: 借助数据元素地址的指针表示数据的逻辑关系

存放结点的存储单元分为两部分: 数据项 | 指针项

特点

1. 动态分配, 不需要预先确定内存分配
2. 插入和删除不需要移动其它元素
3. 非随机存取结构

索引存储方式

索引存储方式: 借助索引表中的索引指示各存储结点的存储位置

散列存储方式

散列存储方式: 用散列函数指示各结点的存储位置

运算

指在某种逻辑结构上施加的操作, 即对逻辑结构的加工

加工型运算

其操作改变原逻辑结构的值. 如: 结点个数, 结点内容等.

引用型运算

其操作不改变原逻辑结构的值.

基本运算

1. 建立
2. 查找
3. 读取
4. 插入
5. 删除

算法及描述

算法规定了求解给定类型问题所需的所有处理步骤和执行顺序, 使给定类型问题在有限时间内被求解

一个算法是对特定问题求解步骤的一种描述, 它是指令的有穷序列.

特性

1. 有穷性: 一个算法总是在执行有穷步后结束
2. 确定性: 算法的每一步都必须是明确定义的
3. 可行性: 算法中的每一步都是可以通过已经实现的操作来完成的
4. 输入: 一个算法有零个或者多个输入, 这些输入取自于特定的对象集合
5. 输出: 一个算法有一个或者多个输出, 它们是与输入有特定关系的量

算法分析

算法的设计应满足

  1. 正确性: 对于合法的输入产生符合要求的输出
  2. 易读性: 算法应易读, 便于交流, 这也是保证算法正确性的前提 (如: 添加注释)
  3. 健壮性: 当输入非法数据时, 算法还能做出适当的反应而不会崩溃. 如: 输出错误信息; 算法中应该考虑适当的错误处理.
  4. 时空性: 指算法的时间复杂度和空间复杂度, 算法分析主要分析算法的时间复杂度和空间复杂度, 目的是提高算法的效率

时间复杂度

算法运行时需要的总步数, 通常是问题规模的函数

确定算法的计算量

合理地选择一种或几种操作作为标准操作

无特殊说明, 默认以赋值语句作为标准操作.

确定每个算法共执行多少次标准操作, 并将此次数规定为该算法的计算量

算法的最坏情况时间复杂度

以算法在所有输入下的计算量的最大值作为算法的计算量

算法的平均情况时间复杂度

以算法在所有输入下的计算量的加权平均值作为算法的计算量

渐近时间复杂度

T(n) = O(f(n))

f(n)为问题规模n的某个函数

大O表示法也称为渐进表示法

T(n)称为算法的渐进时间复杂度, 简称时间复杂度

常见的时间复杂度按数量级递增排列依次为

O(1) < O(log2(n)) < O(n) < O(nlog2(n)) < O(n^2) < O(n^c) < O(2^n)

常数阶 < 对数阶 < 线性阶 < 线性对数阶 < 平方阶 < 多项式阶 < 指数阶

通常认为:

常数阶O(1), 算法的时间复杂度与输入规模n无关

具有指数阶的算法是实际不可计算的

阶数低于平方阶的算法是高效率的

空间复杂度

S(n) = O(g(n))

g(n)为问题规模n的某个函数

算法执行时所占用的存储空间, 通常是问题规模的函数

是对一个算法在运行过程中临时占用存储空间大小的量度.

一个算法在执行期间所需要的存储空间量包括以下部分

1. 程序代码所占用的空间
2. 输入数据所占用的窠
3. 辅助变量所占用的空间

我们在估算空间复杂度时, 一般只分析辅助变量所占用的空间

Clause and Comparative and The superLative

发表于 2019-10-11 | 分类于 English
字数统计: 678 字 | 阅读时长 ≈ 3 分

在句中做定语, 修饰一个名词或代词, 被修饰的名词叫先行词. 其后的从句就是定语从句, 由关系词((关系代词或关系副词)引出.

定语从句中的关系代词

在从句中做一定的成分, 代替先行词, 起到连接先行词和从句的作用

常用的关系代词有 that, who, which, whom, whose

The girl whom/that I spoke to is my cousin.

先行词是人的话用that, who, whom, whose来引导定语从句

whom 在从句中作宾语

whom / that 在从句中既可作主语又可作宾语

They are the people that/who were seen yesterday.

They are the people whom/that/who I saw yesterday.

They are the people whose wallets were lost yesterday.

先行词是动物/事物的话, 用which, that, whose来引导定语从句

which, that 在从句中可作主语和宾语, 作宾语时可以省略

He came back for the book which/that he had forgotten.

He came back for the book which/that was one the desk.

This is the chair whose legs were broken.

定语从句中的关系副词

when, where, why

why 用于修饰表示原因的名词

We don’t know the reason why he didn’t show up.

when 修饰表示时间的名词

We’ll put off the picnic until next week, when hte weather may be better.

where 修饰表示地点的名词

We don’t know the place where he lives.

宾语从句的连词

在句子中起宾语作用的从句

从属连词: that, if, whether

He said that he was there yesterday.

He doesn’t know if/whether he was there.

连接代词: who, whom, whose, what

Do you know who has won the game?

Do you know whom he likes?

Do you know whose book it is?

Do you know what he is looking at?

连接副词: when, where, why, how

He wants to know when the party is.

He want to know where the party is.

He wants to know why they have a party.

He wants to know how they come.

He told me that he would go to college the next year.

比较级和最高级

  1. 通常在形容词和副词后面加”er”, “est”, hard - harder - the hardest

  2. 词尾是不发音的单章节e时, 加”r”, “st”, nice - nicer - the nicest

  3. 词尾是辅音 + y的双音节时, 去掉”y”, 加”ier”, “iest”, dry - drier - the driest

  4. 以一个辅音结尾的重读闭音节时, 又写最后一个字母, 加”er”, “est”, hot - hotter - hottest

  5. 多音节和双音节, 在形容词和副词前加

    1. 程度加强: more, the most
    2. 程度减弱: less, the least

    interesting - more / less interesting

    ​ - the most / the least interesting

  6. 不规则变化, good - better - the best, many - more - the most

形容词或副词的比较级, 表示 “比较…”

He is taller than his brother.

The book is more expensive than the pen.

Her English is better than his.

形容词的最高级, 表示 “最…”

He is the tallest in his class.

The book is the most expensive of the three books.

Her English is the best among the three students.

Causative and Sensory and Hypothesis

发表于 2019-10-11 | 分类于 English
字数统计: 571 字 | 阅读时长 ≈ 2 分

使役动词

有have, make, let, 表示 “使…, 让…”

have/make/let + 宾语 + v

He make me laugh.

I let him go.

Please have hime come here.

get + 宾语 + to + v

I can’t get anyone to do the work properly.

help + 宾语 + (to) + v

I helped him (to) repair the car.

使役动词 + 宾语 + 过去分词, 表示让某物/人被别人…

I must get my hair cut.

He couldn’t make himself heard.

Can you get the work finished in time?

感官动词

see / watch / observe / notice / hear / smell / taste / feel + 宾语 + 动词原形 / 现在分词

加动词原形, 表示动作的真实性

I saw him work in the garden yesterday.

加现在分词 ,表示动作的持续性, 进行性

I saw him working in the garden yesterday.

感官动词 + 宾语 + 过去分词

John saw the man knocked down by the car.

只单纯表示条件

条件状语从句, 用if引导, 从句为现在时, 语句用将来时, 表示”如果…”

If you get up early, you will catch up with the train.

if引导的条件状语从句, 可以放在句首, 也可以放在句尾

If you ask him, he will help you.

She will be upset if you fail the exam.

与现在事实相反的虚拟

if从句是一种虚拟的条件或假设, 与现在事实相反

从句: 动词过去式, be动词要用were

主句: would / could / should / might + 动词原形

If I were you, I would join them.

She would come with you if you invited her.

与过去事实相反的虚拟语气

if从句是一种虚拟的条件或假设, 和过去事实相反

从句: had + 动词过去分词

主句: would / could / should / might + have + 动词过去分词

If I had got there earlier, I would have met her.

If he had taken my advise, he would not have made such a mistake.

wish / as if + 过去时/过去完成时

wish 后面的从句译为: 希望…就好了, 是不可能实现的假设

与现在事实相反的愿望

I wish I were as tall as you. (一般过去时)

与过去事实相反的愿望

He wished he hadn’t said that. (过去完成时)

将来不大可能实现的愿望

I wish it would rain tomorrow. (would / could / should / might)

as if 看起来, 好像…

与现在事实相反

You look as if you didn’t care.

与过去事实相反

He talks about Rome as if he had been there before.

与将来事实相反

He opened his mouth as if he would say something.

Non-predicate Verbs

发表于 2019-10-11 | 分类于 English
字数统计: 659 字 | 阅读时长 ≈ 3 分

动词不定式

to + 动词原形, 也就是动词的不定式, 除了不能做谓语, 其它都能做, 例如, 主语, 宾语, 宾补, 定语, 表语和状语

作主语: To get there by bike will take us an hour.

作宾语: The driver failed to see the car in time.

作宾补: We believe hime to be guilty.

作定语: The next train to arrive is from Seoul.

作表语: My suggestion is to put off the metting.

作状语: I come here only to say goodbye to you.

动名词

动词原形 + ing, 也就是动名词, 具有动词的特征和变化形式, 但在句子中的用法及功能类同名词, 在句子中可以作主语, 宾语, 表语, 定语, 也可以被副词修饰, 或支配宾语

作主语: Reading is an art.

作宾语: The went on walking and never stopped talking.

作表语: Your task is quickly cleaning the windows.

作定语: This is a reading room.

假主语/形式主语

不定式或动名词可以在主语的位置上, 但一般用it代替它作形式主语

It is a great honor to be invited.

It is no use crying over split milk.

假宾语/形式宾语

在宾语的位置上, 用it代替它作形式宾语

We think it important to learn English.

I found it pleasant walking in the park.

不定式和动名词的否定形式

在不定式或动名词前加not就可以了

He pretened not to see her.

He regrets not join them.

不定式表示目的

in order to + v 表示目的, 可以放在句首或句尾

so as to + v 表示目的, 只能放在句尾

I have written it down in order to remember it.

He shouted and waved som as to be noticed.

否定形式

in order not to / so as not to

I have written it down in order not to forgot it.

He said nothing so as not to be noticed.

常见的不定式和动名词

too…to… 太…以至于不能…

The room is too small to live.

enough + n + to + v

There is enough food to eat. (作形容词)

adj + enough + to + v 足够可以

The box is big enough to contain six apples. (作副词)

on + 动名词, 一…就…

On seeing the snake, the girl is very frightened.

There is no hope of + 动名词, 没希望

There is no hope of seeing him.

feel like + 动名词, 想要做…

I feel like eating ice cream now.

have a hard time + 动名词

做…很难

They have a hard time solving the problem themselves.

现在分词 / 动名词

Writing books is his job. (动名词)

He is writing a book. (现在分词)

p.p. / 过去分词

用在完成时和被动语态当中

He has written the homework. (现在完成时)

The homework is written. (被动语态)

分词句

包含现在分词或过去分词的分句

The students went out the classroom, laughing and talking.

Accompanied by his friend, he went to the railway station.

Passive Voice

发表于 2019-10-11 | 分类于 English
字数统计: 367 字 | 阅读时长 ≈ 1 分

被动语态的构成和含义

如果主语是动作的承受者, 则用动词的被动语态作谓语

be + 动词的过去分词 + by sb

He is taken to America by his mother.

被动语态的各种时态

The information is needed by us. (一般现在时)

The book was being read by him. (过去进行时)

The computer has been used by her. (现在完成时)

The room will be cleaned. (一般将来时)

The computer could have been used by them. (现在完成时)

被动语态和情态动词结合

may/maight, must/have to, should/ought to, had better/would rather, used to/would + be + 动词的过去分词

need + doing/to be + 动词的过去分词

The room may be cleaned.

The door must be locked.

The house should be furnished.

The tree had better be watered now.

被动语态和行为者

动作的行为者不明了, 不重要, 或上下文中提到了行为者时, 可以省略 by + 行为者

The information is needed.

The book is being read.

The door was opened.

被动语态的疑问句

助动词 + 主语 + 情态动词 + 动词去过分词?

Is the information needed by him?

Yes, it is.

No, it isn’t.

Has the computer been used by her?

Yes, it has.

No, it hasn’t.

Will the room be cleaned?

Yes, it will.

No, it won’t.

被动语态的特殊疑问句

疑问词 + 助动词 + 主语 + (其它助动词) + 动词的过去分词?

The information is needed by them?

What is needed by them?

The girl is taken to Shanghai?

Where is the girl taken?

The book has been read three times.

How many times has the book been read?

Verb and Model verb

发表于 2019-10-10 | 分类于 English
字数统计: 774 字 | 阅读时长 ≈ 3 分

分类

实意动词 (Notional Verb)

助动词 (Auxiliary Verb)

系动词 (Link Verb)

情态动词 (Model Verb)

动词有数量和时态的变化, 时态通常有三大时态, 现在, 过去, 将来

根据动作进行的状态可分为, 一般时, 进行时, 完成时

时态 = 时间 + 状态

情态动词

can, could, may, might

can, could

表示能力, 可以用be able to 代替

表示现在和过去的能力

表示可能性, can的可能性大

表示请求和允许

He can/could swim.

He can/could come tomorrow.

Can/Could I stay here?

may, might

表示可能性, may的可能性大

请求, 允许, might更委婉

He may/might come here.

May/Might I join you?

Yes, please.

No, you can’t.

No, you mustn’t.

must, have to

必须, 必要, must表示主观多一些, have to表示客观多一些, 有不得不的意思.

have to 有时态和数量的变化

两者的否定含义也不大相同

You mustn’t go. 你不准去

You don’t have to go. 你不必去

should / ought to

表示劝告, 建议, 命令. should表示主观, ought to表示客观, 在疑问句中, 通常使用should

You should do the job right now.

Should they stay here now?

need

做情态动词,

You need come here early.

You needn’t come here early.

Need he come here early?

Yes, he need.

No, he needn’t.

实意动词

有时态和数量的变化

He needs to come here early.

He doesn’t need to come here early.

Does he need to come here early?

Yes, he does.

No, he doesn’t.

don’t have to

不必, 不需要

Must I come here early?

No, you needn’t.

No, you don’t have to.

had better

表示最好做某事, had不表征过去, 后面接动词原形

Your had better eat more.

You’d better finish it right now.

would rather

表示宁愿, 宁可, 最好, 还是…为好, 语感上要轻

You would rather deal with it now.

否定形式 had better not, would rather not

You had better not eat more.

used to would

过去常常, 表示过去习惯性动作, used to 可指过去的状态或情况, would则不能

The novel used to popular.

表示反复发生的动作, 如果某一动作没有反复性, 则不能用would

He would practise learn English every week.

I used to live in Beijing.
表示过去经常性或习惯性的动作或状态, 现在已经结束, would则表示有可能再发生

People used to believe that earth was flat.

He would go to the park as soon as he was free.

情态动词的否定

情态动词 + not + 原形

He can’t sing an English song.

He may not know her.

He mustn’t go there.

He doesn’t have to go there.

情态动词的提问

Can he sing an English song?

Yes, he can.

No, he can’t.

Must he go there?

Yes, he must.

No, he needn’t.

Does he have to go there?

Yes, he does.

No, he doesn’t.

情态动词 + have + 过去分词

表示过去的事实, 推测的含义

He can/could/may/might/must have arrived

should + have + 过去分词 本应该

He should have arrived.

They should have finished the work.

needn’t + have + 过去分词 本不需要

You needn’t have done so.

must have + 过去分词 准是已经

He must have arrived

can’t have + 过去分词 不可能已经

He can’t have arrived

There/Here be and Tense

发表于 2019-10-10 | 分类于 English
字数统计: 783 字 | 阅读时长 ≈ 3 分

There be / Here be

可以翻译成 “有, 是”, be动词有单复数的变化

There be 句型

There is a book on the bookshelf.

There are some books on the bookshelf.

Here be 句型

Here is the bus stop.

Here are your books.

一般现在时

表示通常性, 规律性, 习惯性的状态或动作. 主语是单三时, 动词要有单三的变化. 主语为非单三时, 使用动词原形

They often get up at 7:00.

He often gets up at 7:00.

动词的单三变化规则

  1. 在动词末尾直接加s. play - plays
  2. 以o, s, x, ch结尾的动词, 加es. guess - guesses
  3. 以 辅音字母 + y结尾 的动词, 变y为i加es. study - studies.

一般现在时的否定和疑问句用do, does帮助构成

He doesn’t like the car.

Does he like the car?

Yes, he does.

No, he doesn’t.

现在进行时

正在进行或发生的动作. 一般有now等时间副词. 句式为 be + doing

I am watching TV.

He is watching TV.

They are watching TV.

动词的现在分词变化规则

  1. 在动词末尾直接加ing. work - working
  2. 以不发音的e结尾的动词, 去e加ing. take - taking
  3. 重读闭音节的动词, 双写末尾字母加ing. cut - cutting
  4. 以ie结尾的动词, 变ie为y加ing. lie - lying

现在进行时变否定句或疑问句, 将be动词否定或提前

They aren’t watching TV.

Are they watching TV.

一般过去时

过去某个时间里发生的状态或动作. 过去经常性, 习惯性的行为

动词的过去式变化规则

  1. 一般在动词后面加ed. play - played
  2. 以字母e结尾的动词, 直接加d. like - liked
  3. 以 辅音字母 + y结尾 的动词, 变y为i加ed. supply - supplied
  4. 以重读闭章节结尾, 且末尾只有一个辅音字母的动词, 双写末尾字母, 再加ed. plan - planned

He worked very hard last night.

They came here by car.

过去进行时

过去某一时刻或某一段时间进行或发生的动作, was / were + doing

He was talking with his friends just now.

They were waiting for you.

一般将来时

表示将来某一时刻的动作或状态, 或将来某一段时间内经常发生的动作或状态, 常和表示将来的时间状语连用

tomorrow, next week, in the future

will/shall + 动词原形

They will go to Shanghai by ship tomorrow.

We shall leave for Shanghai next month.

be going to + 动词原形

近期事先考虑过的将要发生的事情, 打算, 就要

They are going to play football this afternoon.

He is going to learn English next year.

be doing 表示位置转移的动词

go, come, leave, start, arrive

They are leaving for Japan.

She is arriving tomorrow.

现在完成时

过去发生, 已经完成, 对现在造成影响或后果, 动作可能还会持续. has/have + 动词的过去分词

时间状语: already, yet

They have already arrived in Shanghai.

She has played soccer for 3 hours.

She hasn’t finished the homework yet.

过去完成时

had + 动词的过去分词, 动作发生在过去的过去, 已经完成, 对过去造成了一定的影响或后果

They had arrived in Shanghai.

She had played soccer for 3 hours.

She hadn’t finished the homework yet.

Adjective and Adverb

发表于 2019-10-10 | 分类于 English
字数统计: 215 字 | 阅读时长 ≈ 1 分

形容词

通常形容人或事物的状态, 性质, 大小等, 通常用在名词前, be动词后

the beautiful girl.

The girl is beautiful.

The + 形容词 = 复数名词, 一类人或事物, 后面的动词使用复数

The old need more care than the young.

副词

可以修饰动词, 形容词, 其它副词以及其它结构

He runs fast.

She is very beautiful.

They work very hard.

副词的位置

  1. 放在助动词之后, 实义动词的前后都可以

    He speaks very fast.

  2. 放在形容词之前, 其它副词的前后都可以

    They have already left.

  3. 多个助动词, 放在第一个助动词之后

    They have already been repaired.

常用的频度副词

always, usually, often, somtimes, never, 一般放在be动词后面, 实义动词前面; 助动词和实义动词之间

He always come early.

She often writes homework at 7:30.

Noun

发表于 2019-10-10 | 分类于 English
字数统计: 580 字 | 阅读时长 ≈ 2 分

可数名词 countable noun

数得过来的事物, 才是名词, apple, pencil, student

名词变复数有一定规则

  1. 一般直接在末尾加上s, apple - apples

  2. 以s, x, z, ch, sh结尾的单词, 在末尾加上es

    bus - buses, tomato - tomatoes

  3. 以 辅音字母 + y 结尾的词, 变y为i, 在末尾加上es

    candy - candies

  4. 以o结尾的名词, 如果不是外来词或缩写词, 在末尾加上es

    tomato - tomatoes, hippo - hippos

不可数名词 uncountable noun

无法计算的数量或抽象的概念

salt, coffee, water, history, love

可数名词首字母元音时, 加an, 首字母辅音时, 加a, an, 或者加量词 (one, two, three)

an apple, a box of apples, a tomato, a bag of tomatoes

不可数名词, 只能加量词

a cup of coffee

不定量的表达法

some, any, all, most, every

some, any 都表示一些

some用在肯定名中, 或者用在疑问句中想要等到肯定的回答

any用在否定句和疑问句中

I have been expecting some letters the whole morning, but there weren’t any for me.

most 表示大多数, 后面接复数

Most people here are from China.

every 表示每一个, 所有

Every one likes the film.

all 表示所有, 后面接可数名词的复数, 或者不可数名词

All the coffee is served on time.

All the children like to play football.

both 两者都, either 两者之一, neither 两者都不

可作形容词, 代词, 副词

Both his eyes were serverely burned.

There are trees on either side of the street.

Neither answer is correct.

many 表示许多, 修饰可数名词

many books, a lot of books

much 表示许多, 修饰不可数名词

much water, plenty of water

a lot of (lots of ), plenty of 可以修饰可数和不可数名词

a few 肯定的含义, 表示几个, 接可数名词复数

A few books are put into the box.

few 否定的含义, 表示没几个, 接可数名词复数

Few books are put into the box.

a little, 肯定的含义, 表示一点, 接不可数名词

There is a little water in the bottle.

little, 否定的含义, 表示没多点, 接不可数名词

There is little water in the bottle.

none, no one, 一个也不, 一点也不

none 可以接of短语, 动词可以用单数, 也可用复数

None of us has/have arrived.

no one 不可以接of短语, 动词只能用单数

No one knows the answer.

12
云淡风轻

云淡风轻

Objective-C, Swift, DataBase

18 日志
7 分类
9 标签
© 2019 云淡风轻
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4