欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring框架+jdbcTemplate实现增删改查功能

程序员文章站 2022-03-15 21:21:58
springmvc架构(model(实体类),service,controller层)controller(接收参数调用业务层)–>service(调用持久层,处理业务逻辑)–>dao(与...

springmvc架构(model(实体类),service,controller层)

controller(接收参数调用业务层)–>service(调用持久层,处理业务逻辑)–>dao(与数据库交互)

1. ioc(控制反转是一种设计思想而不是技术)

di(依赖注入):是ioc思想的一种技术实现

ioc容器是spring提供的保存bean对象的容器

bean管理操作

1.xml + 注解

2.javaconfig + 注解

通过xml配置bean:todo:

通过javaconfig 配置bean:todo:

通过注解配置bean:todo:

2. aop(面向切面)

面向切面的程序设计思想。横向的调用。

eg:一个日志的功能,很多的功能模块都需要去使用,可以写一个切面去做这个事情。

使用@aspect来标记一个普通类为切面。

连接点:比如说日志需要作用的方法。

目标对象:日志需要使用的对象。

1. 添加依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupid>org.springframework</groupid>
    <artifactid>spring-aop</artifactid>
    <version>5.3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupid>org.aspectj</groupid>
    <artifactid>aspectjweaver</artifactid>
    <version>1.9.7</version>
    <scope>runtime</scope>
</dependency>

2.demo练习

需求:springioc + jdbctemplate实现简单的数据库操作

1.新建maven项目并引入spring核心4依赖

<!--spring的4个基础jar包(容器包)-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-expression</artifactid>
            <version>5.3.1</version>
        </dependency>

jdbc依赖

<!--spring整合jdbc-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>5.3.6</version>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.47</version>
        </dependency>

junit5

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupid>org.junit.jupiter</groupid>
            <artifactid>junit-jupiter-api</artifactid>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

2. 创建springconfig配置文件(通过javaconfig方式注入bean)

创建springconfig类,添加@configuration标记为配置类。

配置数据源和jdbctemplatebean

/**
 * @author yonc
 * @date 2021/9/2
 */
@configuration
public class springconfig {
    @bean
    public datasource datasource() {
        mysqldatasource datasource = new mysqldatasource();
        datasource.seturl("jdbc:mysql://localhost:3306/test?useunicode=ture&charactorencoding=utf-8&servertimezone=utc");
        datasource.setuser("root");
        datasource.setpassword("123456");
        return datasource;
    }

    @bean
    public jdbctemplate jdbctemplate(datasource datasource) {
        return new jdbctemplate(datasource);
    }
}

3.创建mvc架构并创建与数据库字段对应的实体类对象

实体类:studentpo

public class studentpo {
    private long id;
    private string name;
    private string age;

    public studentpo() {
    }

    public studentpo(string name, string age) {
        this.name = name;
        this.age = age;
    }

    public studentpo(long id, string name, string age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public long getid() {
        return id;
    }

    public void setid(long id) {
        this.id = id;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getage() {
        return age;
    }

    public void setage(string age) {
        this.age = age;
    }

    @override
    public string tostring() {
        return "studentpo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4. 编写dao层

面向接口编程,首先定义dao层的规范接口,定义了增删改查4种方法

/**
 * @author yonc
 * @date 2021/9/2
 */
public interface studentdao {
    void addstudent(studentpo student);

    void delstudentbyid(long id);

    void updatestudent(studentpo student);

    list<studentpo> selectstudent();
}

接口的实现

@repository注解将studentdao注入ioc容器

@autowired自动装配jdbctemplate对象,jdbctemplate对象已经在springconfig文件中实例化

/**
 * @author yonc
 * @date 2021/9/2
 */
@repository
public class studentdaoimpl implements studentdao {


    @autowired
    jdbctemplate jdbctemplate;

    /*
     * 增加student
     * */
    @override
    public void addstudent(studentpo student) {
        jdbctemplate.update("insert into student (name,age) values (?,?)", student.getname(), student.getage());
    }

    /*
     * 删除student
     * */
    @override
    public void delstudentbyid(long id) {
        jdbctemplate.update("delete from student where id=?", id);
    }

    /*
     * 修改student
     * */
    @override
    public void updatestudent(studentpo student) {
        string sql = "update student set name=?,age=? where id = ? ";
        object[] args = {student.getname(), student.getage(), student.getid()};
        jdbctemplate.update(sql, args);
    }

    /*
     * 查询
     * */
    @override
    public list<studentpo> selectstudent() {
        string sql = "select id,name,age from student";
        return this.jdbctemplate.query(sql, (rs, index) -> {
            studentpo student = new studentpo();
            student.setid(rs.getlong("id"));
            student.setname(rs.getstring("name"));
            student.setage(rs.getstring("age"));
            return student;
        });
    }
}

5. dao与数据库的增删改查已经实现,使用service层去调用dao层的方法。

首先定义service层的接口

/**
 * @author yonc
 * @date 2021/9/2
 */
public interface studentservice {

    void addstudent(studentpo student);

    void delstudentbyid(long id);

    void updatestudent(studentpo student);

    list<studentpo> selectstudent();
}

接口实现

@service将对象声明ioc容器中

@autowired自动装配ioc容器中的studentdaostudentdao对象初始化

/**
 * @author yonc
 * @date 2021/9/2
 */
@service
public class studentserviceimpl implements studentservice {

    @autowired
    studentdao studentdao;

    @override
    public void addstudent(studentpo student) {
        studentdao.addstudent(student);
    }

    @override
    public void delstudentbyid(long id) {
       studentdao.delstudentbyid(id);
    }

    @override
    public void updatestudent(studentpo student) {
       studentdao.updatestudent(student);
    }

    @override
    public list<studentpo> selectstudent() {
        return studentdao.selectstudent();
    }
}

6. 使用junit5单元测试测试

首先通过ioc容器拿到studentservice对象

private annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(springconfig.class);
    // 通过spring的ioc容器
    private studentservice studentservice = applicationcontext.getbean(studentservice.class);

测试

/**
 * @author yonc
 * @date 2021/9/2
 */
class studentserviceimpltest {

    private annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(springconfig.class);
    // 通过spring的ioc容器
    private studentservice studentservice = applicationcontext.getbean(studentservice.class);
    @test
    public void testaddstudent() {

        studentservice.addstudent(new studentpo("zahngsna", "999"));
        system.out.println("添加成功!");
    }

    @test
    public void testdelstudent() {
        studentservice.delstudentbyid(3l);
        system.out.println("删除成功!");
    }

    @test
    public void testupdatestudent() {
        //将id为3的student的name修改为"wang",age修改为21
        studentservice.updatestudent(new studentpo(1l,"wang","28"));
        system.out.println("修改成功!");
    }

    @test
    public void testselectstudent() {
        studentservice.selectstudent().foreach(system.out::println);
    }

}

到此这篇关于spring框架+jdbctemplate实现增删改查功能的文章就介绍到这了,更多相关spring jdbctemplate增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!