Spring学习笔记(七)

假装没事ソ 提交于 2019-11-30 09:18:53

JdbcTemplate

作用:用于和数据库交互,实现对表的CRUD操作。
Account.java

package com.how2java.domain;

import java.io.Serializable;

/**
 * 账户的实体类
 */
public class Account implements Serializable{

    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

JdbcTemplateDemo1.java

package com.how2java.jdbcTemplate;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * JdbcTemplate的最基本用法
 */
public class JdbcTemplateDemo1 {
    public static void main(String[] args) {
        //1.准备数据源,Spring内置数据源
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/spring");
        ds.setUsername("root");
        ds.setPassword("admin");
        //2.创建JdbcTemplate对象
        JdbcTemplate jt = new JdbcTemplate();
        //3.给JdbcTemplate对象设置数据源
        jt.setDataSource(ds);
        //4.执行操作
        jt.execute("insert into account(name, money) values('ccc', 1000)");
    }
}

JdbcTemplateDemo2.java

package com.how2java.jdbcTemplate;

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

public class JdbcTemplateDemo2 {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.获取对象
        JdbcTemplate jt = (JdbcTemplate)ac.getBean("jdbcTemplate");
        //3.执行操作
        jt.execute("insert into account(name, money) values('ddd', 1000)");
    }
}

JdbcTemplateDemo3.java

package com.how2java.jdbcTemplate;

import com.how2java.domain.Account;
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.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * JdbcTemplate的CRUD操作
 */
public class JdbcTemplateDemo3 {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.获取对象
        JdbcTemplate jt = (JdbcTemplate)ac.getBean("jdbcTemplate");
        //3.执行操作
        //3.1保存
//        jt.update("insert into account(name, money) values('eee', 1000)");
        //3.2更新
//        jt.update("update account set name = ?, money = ? where id = ?", "test", "900", "3");
        //3.3删除
//        jt.update("delete from account where id = ?", "3");
        //3.4查询所有
//        List<Account> accounts = jt.query("select * from account where money > ?", new AccountRowMapper(), 1000F);
//        List<Account> accounts = jt.query("select * from account where money > ?", new BeanPropertyRowMapper<Account>(Account.class), 1000F);
//        for(Account account : accounts) {
//            System.out.println(account);
//        }
        //3.5查询一个
//        List<Account> accounts = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 1);
//        System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));
        //3.6查询返回一行一列(使用聚合函数,但不加group by子句)
        Long count = jt.queryForObject("select count(*) from account where money > ?", Long.class, 1000F);
        System.out.println(count);
    }
}

/**
 * 定义Account对象的封装策略
 */
class AccountRowMapper implements RowMapper<Account> {
    /**
     * 把结果集的数据封装到Account对象中,然后由Spring把每个Account对象加到集合中
     * @param resultSet
     * @param i
     * @return
     * @throws SQLException
     */
    @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.getFloat("money"));
        return account;
    }
}

bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置JdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="username" value="root"></property>
        <property name="password" value="admin"></property>
     </bean>
</beans>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!