c3p0-0.9.2.1.jar、mchange-commons-java-0.2.3.4.jar1.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=l123452.在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
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);
}



