首先是配置JdbcTemplate

<!-- 为IOC容器配置一个JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
     <property name="dataSource" ref="myDateSource"/>
</bean>

引用数据库

<bean class="com.jdbcTemplate_datasource_test.datasource.MyDataSource" id="myDateSource"scope="singleton">
	   	<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> 

其次在Dao的实现层调用方法getDataSource()

   @PostConstruct
   public void dataSourceInit() {
	System.out.println("Dao中正在使用DataSource属性,对象是"+jdbcTemplate.getDataSource());

可以使用@PostConstruct注释的方法进行初始化输出,不可以使用静态代码块,因为其执行先于Bean的注入,故无法使用到IOC容器的对象。

如果是自己编写的类可以重写toString方法

public class MyDataSource implements DataSource{
	private String url;
	private String username;
	private String password;
	 * 设置DriverClassName
	 * @param driverClassName
	public void setDriverClassName(String driverClassName) {
		Assert.hasText(driverClassName, "Property 'driverClassName' must not be empty");
		String driverClassNameToUse = driverClassName.trim();
		try {
			Class.forName(driverClassNameToUse, true, ClassUtils.getDefaultClassLoader());
		catch (ClassNotFoundException ex) {
			throw new IllegalStateException("Could not load JDBC driver class [" + driverClassNameToUse + "]", ex);
	public void setUrl(String url) {
		this.url = url;
	public void setUsername(String username) {
		this.username = username;
	public void setPassword(String password) {
		this.password = password;
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	@Override
	public Connection getConnection() throws SQLException {
		// TODO Auto-generated method stub
		return DriverManager.getConnection(url, username, password);
	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return DriverManager.getConnection(url, username, password);
	@Override
	public String toString() {
		return "MyDataSource [url=" + url + ", username=" + username + ", password=" + password + "]";

现在我们用控制台输出一下结果

Dao中正在使用DataSource属性,对象是MyDataSource [url=jdbc:mysql:///carcheck?serverTimezone=GMT%2B8&charsetEncoding=utf-8, username=root, password=root]
                                    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springLogging3Application': Unsatisfied dependency expressed through field 'factory'; nested exception ...
                                    今天我来说一下我spring和mybatis的整合,我也是才学这个只能给大家讲一些粗浅的,有大神路过请指点一二 ,谢谢 好现在我们开始讲解首先 我们还是需要导入jar包,这次我们需要的jar包比较多第二步搭建整体的框架 
    包括:dao接口层,mapper.xml 
        pojo层,User 
        service层 service接口 
        serviceI
                                    前几天写到了Spring的一些理论,今天想到了Spring里封装的jdbcTemplate,所以写了这篇博客,spring里的jdbcTemplate做了深层次的封装,使用spring的注入功能,可以把DataSource注册道JdbcTemplate里。结尾还穿插了一点Spring事务的概念。
    Resource resource = new ClassPathResource("dev.properties");
    Properties properties = null;
    try {
                                    Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。我们可以借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库检索数据,并且有效避免直接使用jdbc带来的繁琐编码以上就是SpringBoot整合JdbcTemplate基本内容,其它内容以后慢慢研究。.........
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use th...