spring JdbcTemplate的CRUD操作

六月ゝ 毕业季﹏ 提交于 2019-12-04 20:57:49
package com.com.jdbctemplate;

import com.com.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 java.util.List;

/**
 * spirng  JdbcTemplate在spring中的ioc中使用
 */
public class JDBCTemplateDemo3 {
    public static void main(String[] args) {
        //1.获取spirng的核心容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取对象
        JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);
        //3.执行操作
        //保存
        //jt.update("insert into account(name,money)values('fff',333)");
        //更新
        //jt.update("update account set money = ? where id = ?",3333,6);
        //删除
        //jt.update("delete from account where id = ?",7);
        //查询所有
        /*List<Account> accounts = jt.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
        for(Account account: accounts){
            System.out.println(account);
        }*/
        //查询一个
        /*List<Account> accounts = jt.query("select * from account where id = ?" ,new BeanPropertyRowMapper<Account>(Account.class),3);
        System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));*/
        //查询一行一列(使用聚合函数,但不加group by 子句)
        Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000);
        System.out.println(count);
    }
}

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>
<!--配置数据源 使用spring的内置数据源-->
<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="root"></property>
</bean>
</beans>

 

package com.com.jdbctemplate;import com.com.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 java.util.List;/** * spirng  JdbcTemplate在spring中的ioc中使用*/public class JDBCTemplateDemo3 {    public static void main(String[] args) {        //1.获取spirng的核心容器ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        //2.获取对象JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);        //3.执行操作//保存//jt.update("insert into account(name,money)values('fff',333)");        //更新//jt.update("update account set money = ? where id = ?",3333,6);        //删除//jt.update("delete from account where id = ?",7);        //查询所有/*List<Account> accounts = jt.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));        for(Account account: accounts){            System.out.println(account);        }*/        //查询一个/*List<Account> accounts = jt.query("select * from account where id = ?" ,new BeanPropertyRowMapper<Account>(Account.class),3);        System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));*/        //查询一行一列(使用聚合函数,但不加group by 子句)Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000);        System.out.println(count);    }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!