八、Spring-jdbc

1.JDBC模板

  • Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。
  • Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
  • Spring提供了很多的模板用于简化开发,如JDBC、Springmvc

2.JDBC模板入门

  • 创建数据库和表
create table account(
	id int primary key auto_increment,
	name varchar(20),
	money double
);
  • 使用JDBC的模板
public class SpringJdbcTest {
    @Test
    public void test(){
        //1.创建数据库连接池
        DriverManagerDataSource source = new DriverManagerDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql:///spring");
        source.setUsername("root");
        source.setPassword("123");
        //2.创建JDBC模板
        JdbcTemplate jdbcTemplate = new JdbcTemplate(source);
        jdbcTemplate.update("insert into account value(null,?,?)","zs",1000d);
    }
}
  • 数据插入成功,显示多了一条记录!

3.Spring管理JDBC

  • 配置文件:
<?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
        http://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="source" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///spring"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    	<!--name是setDataSource的注入,是new JdbcTemplate(source)的注入的变形,类似setUrl-->
        <property name="dataSource" ref="source"/>
    </bean>
</beans>
  • 测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringJdbcTest2 {
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Test
    public void test() {
        jdbcTemplate.update("insert into account value(null,?,?)", "ls", 10000d);
    }
}

4.使用第三方连接池(只需要更改配置文件就行)

  • DBCP连接池
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">    
    <!--配置dbcp连接池-->
    <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///spring"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dbcp"/>
    </bean>
</beans>
  • C3P0
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">   
    <!--配置c3p0连接池-->
    <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///spring"/>
        <property name="user" value="root"/>
        <property name="password" value="123"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="c3p0"/>
    </bean>

</beans>
  • Druid
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"> 
    <!--配置druid连接池-->
    <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///spring"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="c3p0"/>
    </bean>
</beans>

5.加载属性文件(bean方式)

  • src/下新建jdbc.propertied
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///spring
username=root
password=123
  • 配置文件:
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--bean方式:加载属性文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="jdbc.properties"/>
    </bean>
    <!--配置druid连接池-->
    <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="druid"/>
    </bean>
</beans>
  • 运行测试类即可!

6.加载属性文件(context方式)

  • src/下新建jdbc.propertied
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///spring
username=root
password=123
  • 配置文件:
<?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"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--context方式:加载属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置druid连接池-->
    <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
        <!--key值不能和name一样,加前缀jdbc-->
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="druid"/>
    </bean>
</beans>

7.JDBC模板的CRUD操作

  • 插入操作

在这里插入图片描述

  • 删除操作

在这里插入图片描述

  • 更新操作

在这里插入图片描述

  • 查询单个字段值(以下忽略连接数据库细节)
@Test
public void test() {
    /*查询单个字段name*/
    String name = jdbcTemplate.queryForObject("select name from account where id=?", String.class, 1);
    System.out.println(name);
}
  • 查询一条记录
  1. 新建domain类
import lombok.Getter;
import lombok.Setter;

@Getter@Setter
public class Account {
    private Integer id;
    private String name;
    private Double money;
    
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
  1. 新建结果集处理器类
public class MyRowMap implements RowMapper<Account> {
    @Override
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setName(resultSet.getString("name"));
        account.setMoney(resultSet.getDouble("money"));
        return account;
    }
}
  1. 运行测试类
@Test
public void test() {
    Account account = jdbcTemplate.queryForObject("select * from account where id=?", new MyRowMap(), 1);
    System.out.println(account); //Account{id=1, name='zs', money=1000.0}
}
  • 查询所有记录
  1. domain类、结果集处理类不变
  2. 测试类
@Test
public void test() {
    List<Account> query = jdbcTemplate.query("select * from account", new MyRowMap());
    for(Account account:query){
        System.out.println(account);
    }
}
//将迭代输出每一个数据记录的信息

猜你喜欢

转载自blog.csdn.net/A15815635741/article/details/84348237