Spring 中JdbcTemplate的最基本用法

1.Maven的配置

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.2.RELEASE</version>
          </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

2.bean.xml

<bean id="accountDao" class="com.joyTop.dao.impl.AccountDaoImpl2">
        <!--<property name="dataSource" ref="dataSource"></property>-->
        <property name="jdbcTemloate" ref="jdbcTemloate"></property>
    </bean>

    <!-- 配置jdbcTemplate -->
    <bean id="jdbcTemloate" 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/spring4_day3?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123.com"></property>
    </bean>

3.dao

public class AccountDaoImpl2 implements IAccountDao {
    private JdbcTemplate jdbcTemloate;
    public void setJdbcTemloate(JdbcTemplate jdbcTemloate) {
        this.jdbcTemloate = jdbcTemloate;
    }
    public Account findAccountById(float id) {
        List<Account> list = jdbcTemloate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), id);
        return list.isEmpty() ? null : list.get(0);
    }
    public Account findAccountByName(String accountName) {
        List<Account> list = jdbcTemloate.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), accountName);
        if (list.isEmpty()) {
            return null;
        }
        if (list.size() > 1){
            throw new RuntimeException("结果不唯一");
        }
        return list.get(0);
    }
    public void updateAccount(Account account) {
    }
}

4.JdbcTemplate的最基本用法

 public static void main(String[] args) {
        //获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //
        JdbcTemplate jt = ac.getBean("jdbcTemloate", JdbcTemplate.class);
        //2.执行操作
        //jt.execute("insert into account(name,money) values('jj2',1000)");
        //3.执行操作
        //保存
        //jt.update("insert into account(name,money) values(?,?)", "lj", 999999f);
        //更新
        //jt.update("uptate account set money=? where id=?",1000f,6);
        //删除
        //jt.update("delete from account where id =?", 5);
        //查询所有
        //List<Account> query = jt.query("select * from account where money > ?", new AccountRowMapper(), 1000f);
        List<Account> query = jt.query("select * from account where money > ?", new BeanPropertyRowMapper<Account>(Account.class), 1000f);
        for(Account list:query){
            //System.out.println(list);
        }
        //查询一个
        List<Account> queryid = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 6);
        for(Account list:queryid){
            System.out.println(list);
        }
        //查询返回一行一列(使用聚合函数,但不如group by 子句)
        List<Long> longs = jt.queryForList("select count(*) from account where money >?", Long.class, 1000f);
         System.out.println(longs.get(0));
    }
//----------自定义封装  也可以用Spring提供的 new BeanPropertyRowMapper<Account>(Account.class)指定返回值类型(无需自己封装)
    static class AccountRowMapper implements RowMapper<Account>{
        //把结果集中的数据封装到Account中,然后由spring 把每个Account加到集合中
        public Account mapRow(ResultSet rs, int i) throws SQLException {
            Account account = new Account();
            account.setId(rs.getFloat("id"));
            account.setName(rs.getString("name"));
            account.setMoney(rs.getDouble("money"));
            return account;
        }
    }

5.结果

Account{id=6.0, name=‘张小龙’, money=20.0}
4

6.注意当dao 继承了:JdbcDaoSupport ,Bean.xml中可以直接配置dataSource

<bean id="accountDao" class="com.joyTop.dao.impl.AccountDaoImpl2">
       <property name="dataSource" ref="dataSource"></property>
 </bean>

 <!-- 配置jdbcTemplate -->
    <bean id="jdbcTemloate" 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/spring4_day3?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123.com"></property>
    </bean>
发布了4 篇原创文章 · 获赞 0 · 访问量 33

猜你喜欢

转载自blog.csdn.net/qq_42882477/article/details/104408761
今日推荐