Spring Data_学习记录_03

本文介绍了一个使用SpringJDBC进行数据库操作的例子,展示了如何通过JdbcTemplate获取数据库中的Circle对象及记录数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Using Jdbc Template

  • org.zcs.springjdbc - JdbcDemo.java
package org.zcs.springjdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zcs.springjdbc.dao.JdbcDaoImpl;
import org.zcs.springjdbc.model.Circle;

public class JdbcDemo {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		JdbcDaoImpl dao = ctx.getBean("jdbcDaoImpl", JdbcDaoImpl.class);
		//Circle circle = dao.getCircle(1);
		//System.out.println(circle.getName());
		System.out.println(dao.getCircleCount());
	}
}


  • org.zcs.springjdbc.dao-JdbcDaoImpl.java
package org.zcs.springjdbc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.zcs.springjdbc.model.Circle;

import com.mysql.jdbc.Statement;
@Component
public class JdbcDaoImpl {	

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();
	
	
	public Circle getCircle(int circleId) {
	
			
	        try {
 
	        Connection con = (Connection) dataSource.getConnection(); //连接数据库
 
	        String sql = "Select * from springdata where id = ?";//对表的操作语句
	        PreparedStatement ps = con.prepareStatement(sql);
	        ps.setInt(1, circleId);
	        Circle circle = null;
	        ResultSet rs = ps.executeQuery();
	        if(rs.next()) {
	        	circle = new Circle(circleId, rs.getString("name"));
	        }
	       
	        rs.close();
	        
	        ps.close();
	        con.close();
	        return circle;
	        
	        }catch (Exception e) {
	        	throw new RuntimeException(e);
	        } 
	}

	 
	public int getCircleCount() {
		String sql = "SELECT COUNT(*) FROM springdata";
		jdbcTemplate.setDataSource(getDataSource());
		return jdbcTemplate.queryForObject(sql, Integer.class);
	}
	public DataSource getDataSource() {
		return dataSource;
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}


  • org.zcs.springjdbc.model-Circle.java
package org.zcs.springjdbc.model;

public class Circle {
	public int id;
	public String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Circle(int circleId, String name) {
		setId(circleId);
		setName(name);
}
}
  • spring.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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:annotation-config></context:annotation-config>

 	<context:component-scan base-package="org.zcs.springjdbc"></context:component-scan>

    <!-- other <bean/> definitions here -->
	<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
			<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
			<property name="url" value="jdbc:mysql://172.21.3.210:3306/data1?useSSL=false"></property>
			<property name="username" value="MHadoop"></property>
			<property name="password" value="12345678"></property>
	</bean>
</beans>

2.学习记录

  • 本次学习了spring jdbc的 Template 的使用方法
  • 遇到一个过期的问题jdbcTemplate.queryForInt(sql);改成这个就可以了jdbcTemplate.queryForObject(sql, Integer.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值