spring实现增删改查学生管理系统-工学云打卡助手习讯云黔职通打卡管理系统

2023-12-11 11:28:33 浏览数 (1)

测试类:

代码语言:java复制
package top.thecat.test;

// 工学云打卡助手习讯云黔职通打卡管理系统项目展示:https://liangzhizhangyu.com/#/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

import java.util.List;

public class ShowStudentTest2 {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");

        List<Users> list = usersService.select2();
        list.forEach(System.out::println);
    }
}
代码语言:java复制
package top.thecat.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

import java.util.List;

public class ShowStudentTest {
    public static void main(String[] args) {
        // 启动Spring
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");

        List<Users> list = usersService.select();
        list.forEach(System.out::println);
    }
}
代码语言:java复制
package top.thecat.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

public class FindStudentByIdTest {
    public static void main(String[] args) {
        // 启动Spring
        // 工学云打卡助手习讯云黔职通打卡管理系统项目展示:https://liangzhizhangyu.com/#/
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");

        Users student = usersService.findUsersById(100001);
        System.out.println(student.toString());

    }
}
代码语言:java复制
package top.thecat.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

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

public class BachAddStudentTest {
    public static void main(String[] args) {
        // 启动Spring
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");
        // 创建对象并且赋予属性(三个对象添加)
        // 工学云打卡助手习讯云黔职通打卡管理系统项目展示:https://liangzhizhangyu.com/#/
        Users users = new Users();
        users.setStudentno(100001);
        users.setSname("白菜");
        users.setSsex("男");
        users.setSbirthday("2023-12-10");
        users.setTc(100);
        users.setSpecialityno("080703");
        Users users1 = new Users();
        users1.setStudentno(100002);
        users1.setSname("小白");
        users1.setSsex("女");
        users1.setSbirthday("2023-12-10");
        users1.setTc(100);
        users1.setSpecialityno("080703");
        Users users2 = new Users();
        users2.setStudentno(100003);
        users2.setSname("大黄");
        users2.setSsex("男");
        users2.setSbirthday("2023-12-10");
        users2.setTc(100);
        users2.setSpecialityno("080703");
//        创建list容器,将对象添加到list
        List<Users> list = new ArrayList<>();
        list.add(users);
        list.add(users1);
        list.add(users2);
//        传递list
        int[] ints = usersService.addStudents(list);
        System.out.println(ints.toString());
    }
}
代码语言:java复制
package top.thecat.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

public class AddStudentTest {
    public static void main(String[] args) {
        // 启动Spring
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");
        // 创建对象并且赋予属性
        Users users = new Users();
        users.setSname("白菜");
        users.setSsex("男");
        users.setSbirthday("2023-12-10");
        users.setTc(100);
        users.setSpecialityno("080703");
        int rows = usersService.addStudents(users);
        System.out.println(rows);
    }
}

业务层

代码语言:java复制
package top.thecat.service;

import top.thecat.pojo.Users;

import java.util.List;

//业务层接口
public interface UsersService {
    int addStudents(Users users);
    int[] addStudents(List<Users> users);
    Users findUsersById(int studentno);
    List<Users> select();
    List<Users> select2();
}
代码语言:java复制
package top.thecat.service.impl;

import top.thecat.dao.UsersDao;
import top.thecat.pojo.Users;
import top.thecat.service.UsersService;

import java.util.List;

// 业务层 接口实现类
public class UsersServiceImpl implements UsersService {
//    依赖注入
    private UsersDao usersDao;

    public UsersDao getUsersDao() {
        return usersDao;
    }

    public void setUsersDao(UsersDao usersDao) {
        this.usersDao = usersDao;
    }

    /**
     * 添加用户
     * @param users
     * @return
     */
    @Override
    public int addStudents(Users users) {
        return this.usersDao.insertUsers(users);
    }

    /**
     * 批量添加用户
     * @param users
     * @return
     // 工学云打卡助手习讯云黔职通打卡管理系统项目展示:https://liangzhizhangyu.com/#/
     */
    @Override
    public int[] addStudents(List<Users> users) {
        return this.usersDao.batchInsertUsers(users);
    }

    /**
     * 学号查看学生信息
     * @param studentno
     * @return
     */
    @Override
    public Users findUsersById(int studentno) {


        return this.usersDao.selectUsersById(studentno);
    }

    @Override
    public List<Users> select() {
        return this.usersDao.selectUsersByName();
    }

    @Override
    public List<Users> select2() {
        return this.usersDao.selectUsersByName2();
    }
}

实体对象

代码语言:java复制
package top.thecat.pojo;

public class Users {
//    成员属性
    private int studentno;
    private String sname;
    private String ssex;
    private String sbirthday;
    private int tc;
    private String specialityno;

//    无参构造方法 用于创建对象
    public Users(){}

//    生成getset
    public int getStudentno() {
        return studentno;
    }

    public void setStudentno(int studentno) {
        this.studentno = studentno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public String getSbirthday() {
        return sbirthday;
    }

    public void setSbirthday(String sbirthday) {
        this.sbirthday = sbirthday;
    }

    public int getTc() {
        return tc;
    }

    public void setTc(int tc) {
        this.tc = tc;
    }

    public String getSpecialityno() {
        return specialityno;
    }

    public void setSpecialityno(String specialityno) {
        this.specialityno = specialityno;
    }

//    重写tostring

    @Override
    public String toString() {
        return "Users{"  
                "studentno="   studentno  
                ", sname='"   sname   '''  
                ", ssex='"   ssex   '''  
                ", sbirthday='"   sbirthday   '''  
                ", tc="   tc  
                ", specialityno='"   specialityno   '''  
                '}';
    }
}

持久层

代码语言:java复制
package top.thecat.dao;

import top.thecat.pojo.Users;

import java.util.List;

public interface UsersDao {
    int insertUsers(Users users);
    int[] batchInsertUsers(List<Users> users);
    Users selectUsersById(int studentno);
    List<Users> selectUsersByName();

    List<Users> selectUsersByName2();
}
代码语言:java复制
package top.thecat.dao.impl;

import org.springframework.jdbc.core.*;
import top.thecat.dao.UsersDao;
import top.thecat.pojo.Users;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

// 接口实现类
public class UsersDaoImpl implements UsersDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    /**
     * 添加用户
     *
     * @param users
     * @return
     */
    @Override
    public int insertUsers(Users users) {
        String sql = "insert into student values(198005,?,?,?,?,?)";
        // 数组存放的是未来向sql中绑定的参数
        Object[] params = new Object[]{users.getSname(),users.getSsex(),users.getSbirthday(),users.getTc(),users.getSpecialityno()};
        return this.jdbcTemplate.update(sql, params);
    }

    /***
     * 批量添加用户
     * @param users
     * @return // 工学云打卡助手习讯云黔职通打卡管理系统项目展示:https://liangzhizhangyu.com/#/
     */
    @Override
    public int[] batchInsertUsers(List<Users> users) {
        String sql = "insert into student values(?,?,?,?,?,?)";
        BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Users temp = users.get(i);
                preparedStatement.setInt(1, temp.getStudentno());
                preparedStatement.setString(2, temp.getSname());
                preparedStatement.setString(3, temp.getSsex());
                preparedStatement.setString(4, temp.getSbirthday());
                preparedStatement.setInt(5, temp.getTc());
                preparedStatement.setString(6, temp.getSpecialityno());
            }

            @Override
            public int getBatchSize() {
                return users.size();    // 获取现在需要添加数据的长的长度
            }
        };
        return this.jdbcTemplate.batchUpdate(sql, setter);
    }

    /**
     * 根据学号查询
     * @param studentno
     * @return
     */
    @Override
    public Users selectUsersById(int studentno) {
        String sql = "select * from student where studentno = ?";
        Object[] params = new Object[]{studentno};
        // 将查询到的结果集 放 users对象
        Users users = new Users();

        this.jdbcTemplate.query(sql, params, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                users.setStudentno(resultSet.getInt("studentno"));
                users.setSname(resultSet.getString("sname"));
                users.setSsex(resultSet.getString("ssex"));
                users.setSbirthday(resultSet.getString("sbirthday"));
                users.setTc(resultSet.getInt("tc"));
                users.setSpecialityno(resultSet.getString("specialityno"));
            }
        });
        return users;
    }

    /**
     * 查多条数据
     * @param
     * @return
     */
    @Override
    public List<Users> selectUsersByName() {
        String sql = "select * from student";
        Object[] params = new Object[]{};       // 不需要绑定参数 就不需要给任何的参数
        return this.jdbcTemplate.query(sql, params, new RowMapper<Users>() {
            @Override
            public Users mapRow(ResultSet resultSet, int i) throws SQLException {
                Users users = new Users();
                users.setStudentno(resultSet.getInt("studentno"));
                users.setSname(resultSet.getString("sname"));
                users.setSsex(resultSet.getString("ssex"));
                users.setSbirthday(resultSet.getString("sbirthday"));
                users.setTc(resultSet.getInt("tc"));
                users.setSpecialityno(resultSet.getString("specialityno"));
                return users;
            }
        });
    }

    /***
     * 查询多条用户信息简化版
     * @return
     */
    @Override
    public List<Users> selectUsersByName2() {
        String sql = "select * from student";
        Object[] params = new Object[]{};       // 不需要绑定参数 就不需要给任何的参数
        return this.jdbcTemplate.query(sql,params,new BeanPropertyRowMapper<>(Users.class));
    }

}

0 人点赞