利用Spring实现增删改

准备所需的jar包:

   搭建Spring的开发环境:spring-expression、spring-beans、spring-context、spring-core、log4j、commons-logging;

   连接数据库:spring-jdbc、spring-tx、mysql-connector-java;

    注解注入:spring-aop

    注意:凡是Spring开头的jar包必须保证版本一致!!!

数据库:

   创建数据库:

    利用Spring实现增删改_第1张图片 

    创建student表:

    利用Spring实现增删改_第2张图片

实体类:

package spring.Bean;

/**
 * 实体类
 * @Author yang
 */
public class Student {
    /*
    id
     */
    private int id;
    /*
    名字
     */
    private String name;
    /*
    性别
     */
    private String sex;
    /*
    年龄
     */
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Dao层接口:

package spring.dao;

import spring.Bean.Student;

public interface IStudentDao {
    /**
     * 添加student的方法
     * @param student
     * @return
     */
    boolean addStudent(Student student);
    
    /**
     * 删除student的方法
     * @param studentId
     * @return
     */
    boolean delStudent(int studentId);

    /**
     * 修改student的方法
     * @param student
     * @return
     */
    boolean updStudent(Student student);
}

Dao层接口的实现类:

package spring.dao.impl;

import spring.Bean.Student;
import spring.dao.IStudentDao;

public class StudentDao implements IStudentDao {
    @Override
    public boolean addStudent(Student student) {
        return false;
    }

    @Override
    public boolean delStudent(int studentId) {
        return false;
    }

    @Override
    public boolean updStudent(Student student) {
        return false;
    }
}

Service层接口:

package spring.service;

import spring.Bean.Student;

public interface IStudentService {
    /**
     * 添加student的方法
     * @param student
     * @return
     */
    boolean addStudent(Student student);

    /**
     * 删除student的方法
     * @param studentId
     * @return
     */
    boolean delStudent(int studentId);

    /**
     * 修改student的方法
     * @param student
     * @return
     */
    boolean updStudent(Student student);
}

Service层接口的实现类

package spring.service.impl;

import spring.Bean.Student;
import spring.service.IStudentService;

public class Service implements IStudentService {
    @Override
    public boolean addStudent(Student student) {
        return false;
    }

    @Override
    public boolean delStudent(int studentId) {
        return false;
    }

    @Override
    public boolean updStudent(Student student) {
        return false;
    }
}

Spring配置文件:

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="spring">context:component-scan>
    
    <bean id="studentDao"
            class="spring.dao.impl.StudentDao"/>
    
    <bean id="studentService"
            class="spring.service.impl.Service"/>
    
    <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/spring"/>
        <property name="username" value="root"/>
        <property name="password" value="1"/>
    bean>
    
    <bean id="jdbcTemplate"
            class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    bean>
beans>

注入:

 service层

package spring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import spring.Bean.Student;
import spring.dao.IStudentDao;
import spring.service.IStudentService;

public class StudentService implements IStudentService {
    /*
    自动注入
     */
    @Autowired
    private IStudentDao studentDao;

    public void setStudentDao(IStudentDao studentDao) {
        this.studentDao = studentDao;
    }
    @Override
    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    @Override
    public boolean delStudent(int studentId) {
        return studentDao.delStudent(studentId);
    }

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

 dao层:

/*
自动注入
 */
@Autowired
private JdbcTemplate jdbcTemplate;

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

 添加方法:

@Override
public boolean addStudent(Student student) {
    boolean flag = false;
    //添加数据的Sql语句
    String addSql = "insert into student values(null,?,?,?)";
    //执行Sql语句
    int row = jdbcTemplate.update(addSql, student.getName(), student.getSex(), student.getAge());
    if(row > 0){
        flag = true;
    }
    return flag;
} 

 测试:

package spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.Bean.Student;
import spring.service.IStudentService;

public class TestADUS {
    private ApplicationContext ac;
    /**
     * 加载配置文件
     */
    @Before
    public void into(){
        ac = new ClassPathXmlApplicationContext("spring.xml");
    }
    @Test
    public void testAdd(){
        //创建Student类对象
        Student student = new Student();
        student.setName("zhangsan");
        student.setSex("boy");
        student.setAge(18);
        //获取StudentService
        IStudentService studentService = (IStudentService)ac.getBean("studentService");
        //添加Student
        boolean b = studentService.addStudent(student);
        System.out.println(b);
    }
}

@Test
public void testAdd(){
    //创建Student类对象
    Student student = new Student();
    student.setName("lisi");
    student.setSex("girl");
    student.setAge(18);
    //获取StudentService
    IStudentService studentService = (IStudentService)ac.getBean("studentService");
    //添加Student
    boolean b = studentService.addStudent(student);
    System.out.println(b);
} 

结果:


 查询数据库中数据:

利用Spring实现增删改_第3张图片

 删除方法:

@Override
public boolean delStudent(int studentId) {
    boolean flag = false;
    //删除数据的Sql语句
    String delSql = "delete from student where id = ?";
    //执行Sql语句
    int row = jdbcTemplate.update(delSql, studentId);
    if(row > 0){
        flag = true;
    }
    return flag;
}

 测试:

@Test
public void testDel(){
    //获取StudentService
    IStudentService studentService = (IStudentService)ac.getBean("studentService");
    //删除Student
    boolean b = studentService.delStudent(1);
    System.out.println(b);
}

 查询结果:

利用Spring实现增删改_第4张图片

 修改方法:

@Override
public boolean updStudent(Student student) {
    boolean flag = false;
    //修改数据的Sql语句
    String updSql = "update student set name = ?,sex = ?,age = ? where id = ?";
    //执行语句
    int row = jdbcTemplate.update(updSql, student.getName(), student.getSex(), student.getAge(), student.getId());
    if(row > 0){
        flag = true;
    }
    return flag;
}

 测试:

@Test
public void textUpd(){
    //创建Student类对象
    Student student = new Student();
    student.setId(2);
    student.setName("lisi");
    student.setSex("boy");
    student.setAge(18);
    //获取StudentService
    IStudentService studentService = (IStudentService)ac.getBean("studentService");
    //修改Student
    boolean b = studentService.updStudent(student);
    System.out.println(b);
}

 查询结果:

利用Spring实现增删改_第5张图片

OK,就到这里了

你可能感兴趣的:(利用Spring实现增删改)