栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring JdbcTemplate的配置使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring JdbcTemplate的配置使用

需要的jar包支持-
c3p0-0.9.2.1.jar、mchange-commons-java-0.2.3.4.jar
1.jdbc.properties文件
jdbc.mysql.className=com.mysql.cj.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:3306/company?serverTimezone=GMT%2B8
jdbc.mysql.user=root
jdbc.mysql.password=l12345
2.在spring.xml配置中获取数据源资源文件(方法对web.xml引入.properties文件无效。) 1)以下方法只能在一个.xml处引入.properties文件,其他地方再引入将报错
2)(本条转载自https://blog.csdn.net/miaomiaosky/article/details/81567676)

         


        
            
                classpath:lts-client.properties
            
        

本方法可用于在多处.xml文件引入.properties文件
————————————————
版权声明:本文为CSDN博主「miaomiaosky」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/miaomiaosky/article/details/81567676

3.配置c3p0的连接参数(需要c3p0的jar)
	
		
		
		
		
	
4.配置JdbcTemplate的Bean
	
		
	

id记得加,否则会报以下错

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empDao': Unsatisfied dependency expressed through field 'jt'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=jdbcTemplate)}

数据源已经配置好


5.注入使用jdbc

    //byName
    @Resource
    private JdbcTemplate jt;

    public User getByUsername(String username) {
        //创建sql语句
        String sql = "select * from user where username = ?";
        //通过jbbc模板的方法,对数据库进行操作。
        return jt.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489981.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号