Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 161 additions & 58 deletions src/main/java/com/github/hcsp/sql/Sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
package com.github.hcsp.sql;
import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应使用 '.*' 形式的导入 - java.sql.* 。

import java.util.ArrayList;
import java.util.List;

public class Sql {
Expand Down Expand Up @@ -57,7 +56,9 @@ public class Sql {

// 用户信息
public static class User {
Integer id;

public static Integer id;

String name;
String tel;
String address;
Expand All @@ -74,24 +75,36 @@ public String toString() {
*
* @param goodsId 指定的商品ID
* @return 有多少用户买过这个商品
* @param databaseConnection
*/

// 例如,输入goodsId = 1,返回2,因为有2个用户曾经买过商品1。
// +-----+
// |count|
// +-----+
// | 2 |
// +-----+
public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, Integer goodsId) throws SQLException {
try (PreparedStatement statement = databaseConnection.prepareStatement("select count(distinct user_id) from `order` where GOODS_ID = ?")) {
statement.setInt(1, goodsId);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
return resultSet.getInt(1);
}
}
return 0;


}

/**
* 题目2:
* 分页查询所有用户,按照ID倒序排列
*
* @param pageNum 第几页,从1开始
* @param pageNum 第几页,从1开始
* @param pageSize 每页有多少个元素
* @return 指定页中的用户
* @param databaseConnection
*/
// 例如,pageNum = 2, pageSize = 3(每页3个元素,取第二页),则应该返回:
// +----+----------+------+----------+
Expand All @@ -100,25 +113,45 @@ public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, In
// | 1 | zhangsan | tel1 | beijing |
// +----+----------+------+----------+
public static List<User> getUsersByPageOrderedByIdDesc(Connection databaseConnection, int pageNum, int pageSize) throws SQLException {
return null;
}
List<User> users = new ArrayList<>();
try (PreparedStatement statement = databaseConnection.prepareStatement("select * from user order by id desc limit (?-1)*?,?")) {
statement.setInt(1, pageNum);
statement.setInt(2, pageSize);
statement.setInt(3, pageSize);
ResultSet resultSet = statement.executeQuery();
users = new ArrayList<>();
while (resultSet.next()) {
User user = new User();
user.id = resultSet.getInt(1);
user.name = resultSet.getNString(2);
user.tel = resultSet.getString(3);
user.address = resultSet.getNString(4);

// 商品及其营收
public static class GoodsAndGmv {
Integer goodsId; // 商品ID
String goodsName; // 商品名
BigDecimal gmv; // 商品的所有销售额
users.add(user);
}
}
return users;

@Override
public String toString() {
return "GoodsAndGmv{" + "goodsId=" + goodsId + ", goodsName='" + goodsName + '\'' + ", gmv=" + gmv + '}';
}
}

/**
* 题目3:
* 查询所有的商品及其销售额,按照销售额从大到小排序
*/
// 商品及其营收
public static class GoodsAndGmv {
Integer goodsId; // 商品ID
String goodsName; // 商品名
BigDecimal gmv; // 商品的所有销售额

@Override
public String toString() {
return "GoodsAndGmv{" + "goodsId=" + goodsId + ", goodsName='" + goodsName + '\'' + ", gmv=" + gmv + '}';
}
}

/**
* 题目3:
* 查询所有的商品及其销售额,按照销售额从大到小排序
* @param databaseConnection
* @return tag
*/
// 预期的结果应该如图所示
// +----+--------+------+
// | ID | NAME | GMV |
Expand All @@ -131,28 +164,47 @@ public String toString() {
// +----+--------+------+
// | 3 | goods3 | 20 |
// +----+--------+------+
public static List<GoodsAndGmv> getGoodsAndGmv(Connection databaseConnection) throws SQLException {
return null;
}
public static List<GoodsAndGmv> getGoodsAndGmv (Connection databaseConnection) throws SQLException {
List<GoodsAndGmv> gmvList = new ArrayList<>();

try (PreparedStatement statement = databaseConnection.prepareStatement("select `order`.goods_id, goods.name, `order`.goods_price * sum(`order`.goods_num) as gmv from `order`\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"join goods\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"on `order`.goods_id = goods.id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"group by `order`.goods_id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"order by gmv desc")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
GoodsAndGmv goodsAndGmv = new GoodsAndGmv();
goodsAndGmv.goodsId = resultSet.getInt(1);
goodsAndGmv.goodsName = resultSet.getString(2);
goodsAndGmv.gmv = resultSet.getBigDecimal(3);
gmvList.add(goodsAndGmv);
}
}
return gmvList;

// 订单详细信息
public static class Order {
Integer id; // 订单ID
String userName; // 用户名
String goodsName; // 商品名
BigDecimal totalPrice; // 订单总金额
}

@Override
public String toString() {
return "Order{" + "id=" + id + ", userName='" + userName + '\'' + ", goodsName='" + goodsName + '\'' + ", totalPrice=" + totalPrice + '}';

// 订单详细信息
public static class Order {
Integer id; // 订单ID
String userName; // 用户名
String goodsName; // 商品名
BigDecimal totalPrice; // 订单总金额

@Override
public String toString() {
return "Order{" + "id=" + id + ", userName='" + userName + '\'' + ", goodsName='" + goodsName + '\'' + ", totalPrice=" + totalPrice + '}';
}
}
}

/**
* 题目4:
* 查询订单信息,只查询用户名、商品名齐全的订单,即INNER JOIN方式
*/
/**
* 题目4:
* 查询订单信息,只查询用户名、商品名齐全的订单,即INNER JOIN方式
* @param databaseConnection
* @return tag
*/
// 预期的结果为:
// +----------+-----------+------------+-------------+
// | ORDER_ID | USER_NAME | GOODS_NAME | TOTAL_PRICE |
Expand All @@ -169,14 +221,35 @@ public String toString() {
// +----------+-----------+------------+-------------+
// | 6 | zhangsan | goods3 | 20 |
// +----------+-----------+------------+-------------+
public static List<Order> getInnerJoinOrders(Connection databaseConnection) throws SQLException {
return null;
}
public static List<Order> getInnerJoinOrders (Connection databaseConnection) throws SQLException {
List<Order> orders = new ArrayList<>();

/**
* 题目5:
* 查询所有订单信息,哪怕它的用户名、商品名缺失,即LEFT JOIN方式
*/
try (PreparedStatement statement = databaseConnection.prepareStatement("select `order`.id as order_id, user.name as user_name, goods.name as goods_name, `order`.goods_price * sum(`order`.goods_num) as total_price from `order`\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"join goods\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\ton `order`.goods_id = goods.id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"join user\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\ton `order`.user_id = user.id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"group by `order`.id")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Order order = new Order();
order.id = resultSet.getInt(1);
order.userName = resultSet.getString(2);
order.goodsName = resultSet.getString(3);
order.totalPrice = resultSet.getBigDecimal(4);
orders.add(order);
}
}
return orders;

}

/**
* 题目5:
* 查询所有订单信息,哪怕它的用户名、商品名缺失,即LEFT JOIN方式
* @return tag
* @param databaseConnection
*/
// 预期的结果为:
// +----------+-----------+------------+-------------+
// | ORDER_ID | USER_NAME | GOODS_NAME | TOTAL_PRICE |
Expand All @@ -197,21 +270,51 @@ public static List<Order> getInnerJoinOrders(Connection databaseConnection) thro
// +----------+-----------+------------+-------------+
// | 8 | NULL | NULL | 60 |
// +----------+-----------+------------+-------------+
public static List<Order> getLeftJoinOrders(Connection databaseConnection) throws SQLException {
return null;
}
public static List<Order> getLeftJoinOrders (Connection databaseConnection) throws SQLException {
List<Order> orders = new ArrayList<>();

// 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库
public static void main(String[] args) throws SQLException {
File projectDir = new File(System.getProperty("basedir", System.getProperty("user.dir")));
String jdbcUrl = "jdbc:h2:file:" + new File(projectDir, "target/test").getAbsolutePath();
try (Connection connection = DriverManager.getConnection(jdbcUrl, "root", "Jxi1Oxc92qSj")) {
System.out.println(countUsersWhoHaveBoughtGoods(connection, 1));
System.out.println(getUsersByPageOrderedByIdDesc(connection, 2, 3));
System.out.println(getGoodsAndGmv(connection));
System.out.println(getInnerJoinOrders(connection));
System.out.println(getLeftJoinOrders(connection));
try (PreparedStatement statement = databaseConnection.prepareStatement("select `order`.id AS ORDER_ID, \n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\t\tuser.name AS USER_NAME, \n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\t\tgoods.name AS GOODS_NAME, \n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\t\t`order`.goods_price * sum(`order`.goods_num) AS total_price \n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"from `order`\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\tleft join goods\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\t\ton `order`.goods_id = goods.id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\tleft join user\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"\t\ton `order`.user_id = user.id\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"group by `order`.id")) {
ResultSet resultSet = statement.executeQuery();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你看这个方法是不是和getInnerJoinOrders有大量的代码重复?考虑过抽取一个公用方法出来么?

while (resultSet.next()) {
Order order = new Order();
order.id = resultSet.getInt(1);
order.userName = resultSet.getString(2);
order.goodsName = resultSet.getString(3);
order.totalPrice = resultSet.getBigDecimal(4);
orders.add(order);
}
}
return orders;


}
// 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库
public static void main (String[]args) throws SQLException {
File projectDir = new File(System.getProperty("basedir", System.getProperty("user.dir")));
String jdbcUrl = "jdbc:h2:file:" + new File(projectDir, "target/test").getAbsolutePath();
try (Connection connection = DriverManager.getConnection(jdbcUrl, "root", "Jxi1Oxc92qSj")) {
System.out.println(Sql.countUsersWhoHaveBoughtGoods(connection, 1));
System.out.println(Sql.getUsersByPageOrderedByIdDesc(connection, 2, 3));
System.out.println(Sql.getGoodsAndGmv(connection));
System.out.println(getInnerJoinOrders(connection));
System.out.println(getLeftJoinOrders(connection));
}
}
}

}