Spring框架简单应用——增删改查

Spring

Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。

  • 目的:解决企业应用开发的复杂性
  • 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
  • 范围:任何Java应用
    Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

增删改查

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.zr"/>
        <net.zr.bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/student?characterEncoding=utf-8"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </net.zr.bean>
        <net.zr.bean id="jdbcTemlate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"/>
        </net.zr.bean>
</beans>

实体类
通过自己的数据库设计
dao层

package com.zr.dao;

import com.zr.bean.Student;

import java.util.List;

public interface IStudentDao {
    boolean addStu(Student student);
    boolean delStu(int stuid);
    boolean updStu(Student student);
    Student findById(int stuid);
    List<Student>findAll();
}

daoImpl

package com.zr.dao.impl;

import com.zr.bean.Student;
import com.zr.dao.IStudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDaoImpl implements IStudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

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

    @Override
    public boolean addStu(Student student) {
        boolean flag=false;
        String sql="insert into user values(null,?,?,?,?)";
        int row = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getSex(), student.getAge());
        if (row > 0) {

            flag=true;
        }
        return flag;
    }

    @Override
    public boolean delStu(int stuid) {
        boolean flag=false;
        String sql="delete from user where id=?";
        int row = jdbcTemplate.update(sql,stuid);
        if (row > 0) {

            flag=true;
        }
        return flag;
    }


    @Override
    public boolean updStu(Student student) {
        boolean flag=false;
        String sql="update user set username=?,password=?,sex=?,age=? where id=?";
        int update = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getSex(), student.getAge(), student.getId());
        if (update>0) {

            flag=true;
        }
        return flag;
    }

    @Override
    public Student findById(int stuid) {
        String sql="select * from user where id=?";
        BeanPropertyRowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
        Student student=jdbcTemplate.queryForObject(sql,new Object[]{stuid},rowMapper);
        return student;
    }

    @Override
    public List<Student> findAll() {
        String sql="select * from user";
        BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
        List<Student> studentList = jdbcTemplate.query(sql, rowMapper);
        return studentList;
    }
}

service层

package com.zr.service;

import com.zr.bean.Student;

import java.util.List;

public interface IStudentService {
    boolean addStu(Student student);
    boolean delStu(int stuid);
    boolean updStu(Student student);
    Student findById(int stuid);
    List<Student> findAll();
}

serviceImpl

package com.zr.service.impl;

import com.zr.bean.Student;
import com.zr.dao.IStudentDao;
import com.zr.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private IStudentDao studentDao;
    @Override
    public boolean addStu(Student student) {
        return studentDao.addStu(student);
    }

    @Override
    public boolean delStu(int stuid) {
        return studentDao.delStu(stuid);
    }

    @Override
    public boolean updStu(Student student) {
        return studentDao.updStu(student);
    }

    @Override
    public Student findById(int stuid) {
        return studentDao.findById(stuid);
    }

    @Override
    public List<Student> findAll() {
        return studentDao.findAll();
    }
}

测试类

package com.zr.test;

import com.zr.bean.Student;
import com.zr.service.impl.StudentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class TestStudent {
    ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
    StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
    static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
        while (true){
            showMenu();
            System.out.println("请输入你需要的功能");
            int n=scanner.nextInt();
            if (n==6){
                System.out.println("已退出");
                break;
            }
            switch (n){
                case 1:
                    addStu();
                    break;
                case 2:
                    delStu();
                    break;
                case 3:
                    findById();
                    break;
                case 4:
                    fingAll();
                    break;
                case 5:
                    updateStu();
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    private static void updateStu() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
        StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        //创建对象
        System.out.println("请输入姓名");
        String username =scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        System.out.println("请输入性别");
        String sex= scanner.next();
        System.out.println("请输入年龄");
        int age=scanner.nextInt();
        System.out.println("请输入你需要修改的ID号");
        int id=scanner.nextInt();
        Student student = new Student(username,password,sex,age,id);
        //获取自动创建的对象-IOC

        boolean b = ssi.updStu(student);
        if (b){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }
    }

    private static void fingAll() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
        StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        StudentServiceImpl studentServiceImpl = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        List<Student> all = ssi.findAll();
        for (Student stu:all){
            System.out.println(
                    stu
            );
        }
    }

    private static void findById() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
        StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        StudentServiceImpl studentServiceImpl = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        System.out.println("请输入ID号");
        int id=scanner.nextInt();
        Student byId = ssi.findById(id);
        System.out.println(byId);
    }

    private static void delStu() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
        StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        System.out.println("请输入需要删除的ID号");
        int id=scanner.nextInt();
        boolean b=ssi.delStu(id);
        if (b){
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
    }

    private static void addStu() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
        StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
        //创建对象
        System.out.println("请输入姓名");
        String username =scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        System.out.println("请输入性别");
        String sex= scanner.next();
        System.out.println("请输入年龄");
        int age=scanner.nextInt();

        Student student = new Student(username,password,sex,age);
        //获取自动创建的对象-IOC

        boolean b = ssi.addStu(student);
        if (b){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }

    private static void showMenu() {
        System.out.println("欢迎使用屠吉吉管理的系统");
        System.out.println("1,添加所有");
        System.out.println("2,删除用户");
        System.out.println("3,根据id查询用户");
        System.out.println("4,查询所有用户");
        System.out.println("5,修改用户");
        System.out.println("6,退出");
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值