java--Spring(二)

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,退出");
    }
}
数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问题。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
pulsar-java-spring-boot-starter是一个用于在Spring Boot应用程序中集成Apache Pulsar消息队列的开源库。Apache Pulsar是一个可扩展的、低延迟的分布式消息传递平台,它具有高吞吐量和高可靠性的特点。 pulsar-java-spring-boot-starter允许开发人员在Spring Boot应用程序中轻松地发送和接收Pulsar消息。它提供了一组容易使用的注解和工具类,简化了与Pulsar集群的交互。 使用pulsar-java-spring-boot-starter,开发人员可以通过添加依赖和配置一些属性来快速集成Pulsar到他们的Spring Boot应用程序中。一旦集成完成,开发人员可以使用注解来定义消息的生产者和消费者。通过生产者注解,开发人员可以将消息发送到Pulsar集群,并指定消息的主题和内容。通过消费者注解,开发人员可以订阅Pulsar主题,并定义接收和处理消息的方法。 除了基本的生产者和消费者功能,pulsar-java-spring-boot-starter还提供了一些其他特性。例如,它支持失败重试机制,当消息发送或接收出现问题时,可以自动重试。它还支持消息过滤器,可以按条件过滤接收的消息。而且,它还提供了一些监控和管理功能,可以方便地监控消息的生产和消费情况。 总之,pulsar-java-spring-boot-starter为Spring Boot开发人员提供了一种方便、快捷地集成Apache Pulsar消息队列的方法。它简化了与Pulsar集群的交互,提供了易于使用的注解和工具类,让开发人员可以专注于业务逻辑的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值