发表于: 2025-04-13 20:58:46

0 92


今天完成的事情:

27.检查一下自己的代码是否符合规范,如果DB的表格有改动,应该改哪些内容,需要多久。

添加字段代码

User

package org.example.model;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private long qq;
private String type;
private String graduatedSchool;
private int number;
private String wish;
private String link;
private String brother;
private String comprehentPlave;
private long createAt;
private long updateAt;

public User() {
}

public User(String name, long qq, String type, String graduatedSchool, int number, String wish, String link, String brother, String comprehentPlave, long createAt, long updateAt) {
this.name = name;
this.qq = qq;
this.type = type;
this.graduatedSchool = graduatedSchool;
this.number = number;
this.wish = wish;
this.link = link;
this.brother = brother;
this.comprehentPlave = comprehentPlave;
this.createAt = createAt;
this.updateAt = updateAt;
}

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 long getQq() {
return qq;
}

public void setQq(long qq) {
this.qq = qq;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getGraduatedSchool() {
return graduatedSchool;
}

public void setGraduatedSchool(String graduatedSchool) {
this.graduatedSchool = graduatedSchool;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public String getWish() {
return wish;
}

public void setWish(String wish) {
this.wish = wish;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getBrother() {
return brother;
}

public void setBrother(String brother) {
this.brother = brother;
}

public String getComprehentPlave() {
return comprehentPlave;
}

public void setComprehentPlave(String comprehentPlave) {
this.comprehentPlave = comprehentPlave;
}

public long getCreateAt() {
return createAt;
}

public void setCreateAt(long createAt) {
this.createAt = createAt;
}

public long getUpdateAt() {
return updateAt;
}

public void setUpdateAt(long updateAt) {
this.updateAt = updateAt;
}

@Override
   public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", qq=" + qq +
", type='" + type + '\'' +
", graduatedSchool='" + graduatedSchool + '\'' +
", number=" + number +
", wish='" + wish + '\'' +
", link='" + link + '\'' +
", brother='" + brother + '\'' +
", comprehentPlave='" + comprehentPlave + '\'' +
", createAt='" + createAt + '\'' +
", updateAt='" + updateAt + '\'' +
'}';
}
}

将主方法进行修改

package org.example;

import com.alibaba.druid.pool.DruidDataSource;
import org.example.model.User;
import org.example.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

public class MainApplication {
private static UserService userService;
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
// 初始化Spring容器
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
userService = context.getBean(UserService.class);

// 新增:验证连接池状态
       checkConnectionPool(context);

System.out.println("=== 用户管理系统 ===");
showMenu();

// 关闭资源
       scanner.close();
((ClassPathXmlApplicationContext) context).close();
}

// 新增方法:检查连接池状态
   private static void checkConnectionPool(ApplicationContext context) {
DruidDataSource dataSource = (DruidDataSource) context.getBean("dataSource");
System.out.println("\n=== 连接池状态 ===");
System.out.println("活跃连接数: " + dataSource.getActiveCount());
System.out.println("空闲连接数: " + dataSource.getPoolingCount());
System.out.println("最大连接数: " + dataSource.getMaxActive());
}
private static void showMenu() {
while (true) {
System.out.println("\n请选择操作:");
System.out.println("1. 添加用户");
System.out.println("2. 更新用户");
System.out.println("3. 删除用户");
System.out.println("4. 查询所有用户");
System.out.println("5. 根据ID查询用户");
System.out.println("0. 退出系统");
System.out.print("请输入选项: ");

int choice = scanner.nextInt();
scanner.nextLine(); // 消耗换行符

           switch (choice) {
case 1:
addUser();
break;
case 2:
updateUser();
break;
case 3:
deleteUser();
break;
case 4:
listAllUsers();
break;
case 5:
getUserById();
break;
case 0:
System.out.println("感谢使用,再见!");
return;
default:
System.out.println("无效选项,请重新输入!");
}
}
}

private static void addUser() {
try {
// 保留原有输入逻辑(但不使用)
       System.out.print("请输入用户名: ");
String name = scanner.nextLine();
System.out.print("请输入QQ: ");
long qq = scanner.nextLong();
scanner.nextLine();
System.out.print("请输入修真类型: ");
String type = scanner.nextLine();
System.out.print("请输入毕业学校: ");
String graduatedSchool = scanner.nextLine();
System.out.print("请输入学号: ");
int number = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入立愿: ");
String wish = scanner.nextLine();
System.out.print("请输入日报链接: ");
String link = scanner.nextLine();
System.out.print("请输入师兄姓名: ");
String brother = scanner.nextLine();
System.out.print("请输入从哪里联系的: ");
String comprehentPlave = scanner.nextLine();
System.out.print("请输入开始时间: ");
int createAt = scanner.nextInt();
System.out.print("请输入结束时间: ");
int updateAt = scanner.nextInt();
scanner.nextLine();
User user = new User(name, qq,type,graduatedSchool,number,wish,link,brother,comprehentPlave,createAt,updateAt);
int result = userService.addUser(user);
if (result == 1) {
System.out.println("添加成功!"+user);
} else {
System.out.println("添加失败!");
}
} catch (Exception e) {
System.err.println("添加用户失败: " + e.getMessage());
}
}


private static void updateUser() {
System.out.println("\n--- 更新用户 ---");
System.out.print("请输入要更新的用户ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // 消耗换行符

       User user = userService.getUserById(id);
if (user == null) {
System.out.println("用户不存在!");
return;
}

System.out.println("当前用户信息: " + user);
System.out.print("请输入新用户名 (直接回车保持原值): ");
String name = scanner.nextLine();
if (!name.isEmpty()) {
user.setName(name);
}

System.out.print("请输入新QQ (直接回车保持原值): ");
String qqInput = scanner.nextLine();
if (!qqInput.isEmpty()) {
user.setQq(Long.parseLong(qqInput));
}
System.out.print("请输入新修真类型 (直接回车保持原值): ");
String type = scanner.nextLine();
if (!type.isEmpty()) {
user.setType(type);
}
System.out.print("请输入新毕业学校 (直接回车保持原值): ");
String graduatedSchool = scanner.nextLine();
if (!graduatedSchool.isEmpty()) {
user.setGraduatedSchool(graduatedSchool);
}
System.out.print("请输入新学号 (直接回车保持原值): ");
String numberInput = scanner.nextLine();
if (!numberInput.isEmpty()) {
user.setNumber(Integer.parseInt(numberInput));
}
System.out.print("请输入新立愿 (直接回车保持原值): ");
String wish = scanner.nextLine();
if (!wish.isEmpty()) {
user.setWish(wish);
}
System.out.print("请输入新日报链接 (直接回车保持原值): ");
String link = scanner.nextLine();
if (!link.isEmpty()) {
user.setLink(link);
}
System.out.print("请输入新师兄姓名 (直接回车保持原值): ");
String brother = scanner.nextLine();
if (!brother.isEmpty()) {
user.setBrother(brother);
}
System.out.print("请输入新从哪里联系的 (直接回车保持原值): ");
String comprehentPlave = scanner.nextLine();
if (!comprehentPlave.isEmpty()) {
user.setComprehentPlave(comprehentPlave);
}
System.out.print("请输入新开始时间 (直接回车保持原值): ");
String createAtInput = scanner.nextLine();
if (!createAtInput.isEmpty()) {
user.setCreateAt(Long.parseLong(createAtInput));
}
System.out.print("请输入新结束时间 (直接回车保持原值): ");
String updateAtInput = scanner.nextLine();
if (!updateAtInput.isEmpty()) {
user.setUpdateAt(Long.parseLong(updateAtInput));
}
int result = userService.updateUser(user);
if (result == 1) {
System.out.println("更新成功!"+user);
} else {
System.out.println("更新失败!");
}
}

private static void deleteUser() {
System.out.println("\n--- 删除用户 ---");
System.out.print("请输入要删除的用户ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // 消耗换行符

       int result = userService.deleteUser(id);
if (result == 1) {
System.out.println("删除成功!");
} else {
System.out.println("更新失败!");
}
}

private static void listAllUsers() {
System.out.println("\n--- 所有用户列表 ---");
List<User> users = userService.queryAllUsers();

if (users.isEmpty()) {
System.out.println("没有用户数据!");
} else {
for (User user : users) {
System.out.println(user);
}
}
}

private static void getUserById() {
System.out.println("\n--- 查询用户 ---");
System.out.print("请输入用户ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // 消耗换行符

       User user = userService.getUserById(id);
if (user != null) {
System.out.println("查询结果: " + user);
} else {
System.out.println("用户不存在!");
}
}
}

修改UserMapper.xml的musql语句

insert id="addUser" parameterType="org.example.model.User" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO ye (name, qq, type, graduated_school, number, wish, link, brother, comprehent_plave, create_at, update_at) VALUES (#{name}, #{qq},#{type}, #{graduatedSchool}, #{number}, #{wish}, #{link}, #{brother}, #{comprehentPlave}, #{createAt}, #{updateAt})

    </insert>

明天计划的事情:(一定要写非常细致的内容)

数据库里插入100万条数据,对比建索引和不建索引的效率查别。

遇到的问题:(遇到什么困难,怎么解决的)

收获:(通过今天的学习,学到了什么知识)

检查了自己的代码是否符合规范



返回列表 返回列表
评论

    分享到