JavaEE简单示例——Spring的数据库编程

简单介绍:

在之前我们介绍Spring架构的模块中,我们之前介绍过Spring的jdbc模块 对于Spring来说,它可以集成MyBatis框架来完成SQL语句的操作,所以在JDBC的这个模块 我们更关心的是Spring的事务管理,而不是SQL语句的操作,所以对于SQL语句的操作方法,我们就 简单的介绍一下如何使用就可以了。 首先,我们需要介绍jdbcTemplate类,也就是模板类,我们需要现在Spring的Bean管理XML配置文件中 使用<bean>标签创建这个模板类,然后这个类中有一个属性是DataSource,这个属性需要我们使用 依赖注入的方式输入另一个类的兑现,所以我们还需要使用一个<bean>标签来创建这个DataSource类 最后,我们在自己创建的数据库操作类中,声明一个jdbcTemplate属性,类型也是jdbcTemplate 然后将我们之前创建的jdbcTemplate属性使用依赖注入的方式注入到我们的自定义类的属性中 也就是说,我们需要一个这样的过程:DataSource -> jdbcTemplate -> 自定义数据库操作类 而这个传递的过程就是通过依赖注入的方式,就像是套娃一样。

使用方法:

用官方的话来说,就是配置JdbcTemplate时,需要将dataSource注入JdbcTemplate中,而其它类 需要使用JdbcTemplate的Bean,也需要将JdbcTemplate注入到该Bean中(通常注入到Dao类中, 在Dao类中进行与数据库的相关操作。

我们直接来看XML配置文件会更好理解一些:

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    创建数据源对象-->
    <bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/jdbc"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
<!--    创建模板类对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dateSource"/>
    </bean>
<!--    创建自定义数据库操作类的对象,最终我们只需要或者这个对象就可以了-->
    <bean id="ManipulatingDatabase" class="Semester_4.SpringJDBC.jdbcTemplate.ManipulatingDatabase">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="student" class="Semester_4.SpringJDBC.jdbcTemplate.student"/>
</beans>

然后这个jdbcTemplate类提供了非常多的更新和查询数据库的方法,可以用这些方法来操作数据库。

execute()方法:用来执行DDL的SQL语句,比如创建数据库,创建数据表

update()方法:用来执行更新数据的SQL语句,包括增删都可以用这个方法执行

query()方法:用来执行查询数据的SQL语句。

关于方法返回的查询结果集的处理,使用的是RowMapper接口将结果集映射到类中,具体映射到哪个类 就是通过将RowMapper的泛型来控制的。 通过创建RowMapper接口的实现类,BeanPropertyRowMapper类的对象来返回结果集处理接口的对象 然后接收来自于查询方法的返回值将查询结果映射到类中,从而操作类的对象来获取查询到的结果集。

我们直接来看代码的实现,会更好的理解我们之前说的理论

代码实现:

XML配置文件我们之前已经展示过了,所以我们直接来看获取对象的类:

package Semester_4.SpringJDBC.jdbcTemplate;

import org.springframework.jdbc.core.JdbcTemplate;

public class ManipulatingDatabase {
    private JdbcTemplate jdbcTemplate;

    @Override
    public String toString() {
        return "ManipulatingDatabase{" +
                "jdbcTemplate=" + jdbcTemplate +
                '}';
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

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

    public ManipulatingDatabase(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public ManipulatingDatabase() {
    }
}

然后我们定义一个类用来处理查询到的结果:

package Semester_4.SpringJDBC.jdbcTemplate;

public class student {
    private int id;
    private String name;
    private String password;
    private int id_card;
    private int order_id;

    @Override
    public String toString() {
        return "student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", id_card=" + id_card +
                ", order_id=" + order_id +
                '}';
    }

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getId_card() {
        return id_card;
    }

    public void setId_card(int id_card) {
        this.id_card = id_card;
    }

    public int getOrder_id() {
        return order_id;
    }

    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }

    public student(int id, String name, String password, int id_card, int order_id) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.id_card = id_card;
        this.order_id = order_id;
    }

    public student() {
    }
}

 最后我们在测试类中继续测试:

package Semester_4.SpringJDBC.jdbcTemplate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class text {
    public static void main(String[] args) {
//        创建IoC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("JdbcTemplate.xml");
//        从容器中获取自定义的数据库操作类的对象,通过这个类的jdbcTemplate属性来执行数据库的操作方法
//        这个类中的属性的值就来自于我们之前在配置文件中配置的三个Bean并进行依赖注入之后获取的值
        ManipulatingDatabase md = (ManipulatingDatabase) ac.getBean("ManipulatingDatabase");
        JdbcTemplate jdbcTemplate = md.getJdbcTemplate();
//        调用执行SQL的方法,创建一个数据库,这个方法没有返回值,如果没有报错,就说明已经创建成功了
        jdbcTemplate.execute("create table if not exists user_info(id int)");
//        调用更新数据的方法,这个方法可以执行对于数据的增删改的操作
        jdbcTemplate.update("insert into user_info values (1)");
//        获取结果集的映射类,这个类的泛型就是我们的结果集映射的Java类
//        由于这个类会自动的寻找数据表的列和属性的映射,所以我们在创建封装结果集的类的时候需要遵循一定的规则
//        主要就是我们的类中的属性的类型和属性名要和数据表的列名和属性要写的一致
//        其次就是BeanPropertyRowMapper这个类的创建需要一个参数,就是我们的结果集封装类的class对象
        RowMapper<student> rm = new BeanPropertyRowMapper<student>(student.class);
        student s = new student();
        s.setId(1);
//        然后就是调用jdbcTemplate的query()方法来进行查询语句的执行,这个方法的参数就是我们的
//        SQL语句和我们的结果集处理类,方法的返回值是一个List集合,这个集合的元素是student,每一个student都封装了一行查询结果
//        将List集合进行遍历之后我们就可以得到我们的查询结果了
        List<student> query = jdbcTemplate.query("select * from student", rm);
        for(student s2 : query){
            System.out.println(s2.toString());
        }
    }
}

运行结果:

运行比较慢,因为底层的封装比较差,但是无所谓,我们在之后的环节中对数据库做持久化操作时使用MyBatis,所以这一块的知识我们仅作了解就可以了 

注意点:

在jdbcTemplate中,我们主要就是知道如何使用RowMapper结果集处理类来处理我们获取到的结果集 而在我们的SQL语句的执行中,我们可以使用maybatis来帮助我们去执行,在JDBC的模块中,最重要的内容还是 事务的管理方面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaEE实验三中的Spring AOP是指Spring框架中的面向切面编程。它可以在不修改原有代码的情况下,通过在程序运行时动态地将代码织入到类的指定方法中,实现对方法的增强。具体来说,Spring AOP主要包括两种代理方式:JDK动态代理和CGLIB动态代理。其中,JDK动态代理只能代理实现接口的类,而CGLIB动态代理则可以代理任意类。在Spring AOP中,我们可以通过XML配置或注解来定义切面和通知,实现对目标方法的增强。 以下是JavaEE实验三中Spring AOP的基本步骤: 1. 定义切面类,即包含通知的类。 2. 定义通知,即增强的代码逻辑。 3. 配置切面和通知,可以通过XML配置或注解来实现。 4. 在需要增强的方法上添加切点,即指定需要增强的方法。 5. 运行程序,观察增强效果。 下面是一个使用XML配置的Spring AOP的例子: 1. 定义切面类和通知: ```java public class LogAspect { public void before() { System.out.println("方法执行前"); } public void after() { System.out.println("方法执行后"); } } ``` 2. 在XML配置文件中定义切面和通知: ```xml <bean id="logAspect" class="com.example.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:before method="before" pointcut="execution(* com.example.Service.*(..))"/> <aop:after method="after" pointcut="execution(* com.example.Service.*(..))"/> </aop:aspect> </aop:config> ``` 3. 在需要增强的方法上添加切点: ```java @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { System.out.println("添加用户:" + user.getName()); } } ``` 4. 运行程序,观察增强效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值