Spring整合JDBC的xml配置版本

前言

Spring框架中提供了一个可以操作数据库的对象,这个对象封装了对JDBC实现的细节,提供了一套模版,这个模版类是jdbcTemplate,该类在spring-jdbc.jar包中

演示JdbcTemplate模版类对数据库的操作

开发步骤:

  • 导包:Spring的核心包+spring-jdbc+数据库驱动jar包+连接池jar包(c3p0连接池)+spring-test测试包+Junit
  • 准备数据库user表
  • 书写dao层接口(书写CRUD增删该查方法)
  • 书写dao层接口实现类(书写CRUD增删该查方法)
  • 配置Spring核心主配置文件
  • 使用Junit进行单元测试

xml配置版本

1、导包:pom配置文件中添加jar包所需依赖

<dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.0.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.0.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>

        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.2version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.0.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.1version>
            <scope>testscope>
        dependency>
    dependencies>

2、 准备数据库user表(实体类同数据库字段一致即可)

@Component("user")/*此注释  将该类交给Spring容器管理*/
public class User implements Serializable {
    @Value("1")
    private Integer u_id;
    @Value("小孙")
    private String u_name;
    @Value("20")
    private Integer u_age;
    @Value("10000")
    private Double u_salary;

    public User(Integer u_id, String u_name, Integer u_age, Double u_salary) {
        this.u_id = u_id;
        this.u_name = u_name;
        this.u_age = u_age;
        this.u_salary = u_salary;
    }

    public User() {
    }

    public Integer getU_id() {
        return u_id;
    }

    public void setU_id(Integer u_id) {
        this.u_id = u_id;
    }

    public String getU_name() {
        return u_name;
    }

    public void setU_name(String u_name) {
        this.u_name = u_name;
    }

    public Integer getU_age() {
        return u_age;
    }

    public void setU_age(Integer u_age) {
        this.u_age = u_age;
    }

    public Double getU_salary() {
        return u_salary;
    }

    public void setU_salary(Double u_salary) {
        this.u_salary = u_salary;
    }

    @Override
    public String toString() {
        return "User{" +
                "u_id=" + u_id +
                ", u_name='" + u_name + '\'' +
                ", u_age=" + u_age +
                ", u_salary=" + u_salary +
                '}';
    }
}

3、书写dao层接口

/*
	dao层接口
*/
public interface UserDao {
    //CRUD
    //添加用户
    void addUser(User user);
}

4、书写dao层接口实现类

/**
	dao层接口实现类
*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Autowired//前提是Spring容器中含有该对象
    private JdbcTemplate jdbcTemplate;
    @Override//对于数据库来说  增删改  都是更新
    public void addUser(User user) {
       //添加用户
        int num = jdbcTemplate.update("insert into user values(null,?,?,?)", user.getU_name(), user.getU_age(), user.getU_salary());
        if(num > 0){
            System.out.println("数据添加成功!");
        }else {
            System.out.println("数据添加失败!");
        }
    }
}

5、数据库屏连接配置信息(db.properties)与Spring主配置文件(applicationContext.xml)如下:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/db_spring?characterEncoding=UTF-8
jdbc.user=root
jdbc.password=root

<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.zhiyou100"/>
    
    
    <context:property-placeholder location="db.properties"/>
    
    
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="dataSource"/>
    bean>
beans>

6、使用Junit单元进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
//@Configuration   配置Spring   一个主配置类
//@ContextConfiguration  读取applicationContext.xml
public class JdbcTemplateTest {
    //进行dao接口的属性注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private User user;//此时user值就从容器中注入进来

    /**
     * 测试   addUser
     */
    @Test
    public void testAddUser(){
        userDao.addUser(user);
    }
}

结果如下:
Spring整合JDBC的xml配置版本_第1张图片
在这里插入图片描述

你可能感兴趣的:(Spring,spring)