使用JDBCTemplate
首先创建一个Dao接口和实现类,添加注解创建对象,然后创建一个数据库表和实体类
创建service层代码,并注入Dao接口的bean对象,调用接口方法
配置文件中【有错】
Dao中
@Component
public interface UserDao {
void add(User user);
}
DaoImpl中
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(User user) {
String sql = "insert into o_User values (?,?,?)";
int update = jdbcTemplate.update(sql, null, user.getUsername(), user.getPassword());
System.out.println(update);
}
}
Service中【没建立接口】
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void addUser(User user){
userDao.add(user);
}
}
随便建一个测试类
ApplicationContext context = new ClassPathXmlApplicationContext("hao.xml");
@Test
public void testJDBCTemplate(){
UserService userService = context.getBean("userService", UserService.class);
User user = new User();
user.setUsername("老王");
user.setPassword("123abc");
System.out.println(user.getPassword());
userService.addUser(user);
}
问题出现了,本来会输出一个影响行数。 但是报错了
Could not process key 'url' in PropertyOverrideConfigurer; nested exception is org.springframework.beans.factory.BeanInitializationException: Invalid key 'url': expected 'beanName.property'
这种的错误,还有另一种说是jdbcTemplate建立的bean找不到,或者创建DaoImpl的bean出错,还有就是说表达式指向的内容找不到。其实这都是借口,问题是这个【DruidDataSource】类的bean对象的id他在上面叫jdbc,现在我改成了druid,然后在JDBCTemplate中注入这个bean就行,他就行了。神奇吧,神奇死了。
他半天一直在叫找不到jdbc这个bean在哪里,过一会又叫找不到druid这个bean在哪里。我一想就大胆改。烂了重建,他就成了
这时jdbc.properties文件【名字就叫这个】,mysql是8.0.21版本,可以自己去找对应的8.0.21的驱动jar包
druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3308/o_user?characterEncoding=utf8&useSSL=true&serverTimezone=UTC
druid.username=root
druid.password=123456
这个命名是我看那个DruidDataSource里面他就这么找的,也许版本不一样里面写的也不一样
DruidDataSource里面这么说的,这是一部分不全,如果出现字段名什么的狗屁问题就去这个类【这是druid的jar包里面的类,去下jar包,导包就能看】里面 C V ,还不行再说别的问题
这是一部分DruidDataSource的源码,里面就可以看见命名是druid.name,我的druid版本是1.2.1,可能版本不一样命名也不一样吧。
public void configFromPropety(Properties properties) {
String property = properties.getProperty("druid.name");
if (property != null) {
this.setName(property);
}
property = properties.getProperty("druid.url");
if (property != null) {
this.setUrl(property);
}
property = properties.getProperty("druid.username");
if (property != null) {
this.setUsername(property);
}
property = properties.getProperty("druid.password");
if (property != null) {
this.setPassword(property);
}
Boolean value = Utils.getBoolean(properties, "druid.testWhileIdle");
if (value != null) {
this.testWhileIdle = value;
}
value = Utils.getBoolean(properties, "druid.testOnBorrow");
if (value != null) {
this.testOnBorrow = value;
}
property = properties.getProperty("druid.validationQuery");
if (property != null && property.length() > 0) {
this.setValidationQuery(property);
}
value = Utils.getBoolean(properties, "druid.useGlobalDataSourceStat");
if (value != null) {
this.setUseGlobalDataSourceStat(value);
}
value = Utils.getBoolean(properties, "druid.useGloalDataSourceStat");
if (value != null) {
this.setUseGlobalDataSourceStat(value);
}
value = Utils.getBoolean(properties, "druid.asyncInit");
if (value != null) {
this.setAsyncInit(value);
}
property = properties.getProperty("druid.filters");
if (property != null && property.length() > 0) {
try {
this.setFilters(property);
} catch (SQLException var21) {
LOG.error("setFilters error", var21);
}
}
property = properties.getProperty("druid.timeBetweenLogStatsMillis");
long value;
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setTimeBetweenLogStatsMillis(value);
} catch (NumberFormatException var20) {
LOG.error("illegal property 'druid.timeBetweenLogStatsMillis'", var20);
}
}
property = properties.getProperty("druid.stat.sql.MaxSize");
int value;
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
if (this.dataSourceStat != null) {
this.dataSourceStat.setMaxSqlSize(value);
}
} catch (NumberFormatException var19) {
LOG.error("illegal property 'druid.stat.sql.MaxSize'", var19);
}
}
value = Utils.getBoolean(properties, "druid.clearFiltersEnable");
if (value != null) {
this.setClearFiltersEnable(value);
}
value = Utils.getBoolean(properties, "druid.resetStatEnable");
if (value != null) {
this.setResetStatEnable(value);
}
property = properties.getProperty("druid.notFullTimeoutRetryCount");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setNotFullTimeoutRetryCount(value);
} catch (NumberFormatException var18) {
LOG.error("illegal property 'druid.notFullTimeoutRetryCount'", var18);
}
}
property = properties.getProperty("druid.timeBetweenEvictionRunsMillis");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setTimeBetweenEvictionRunsMillis(value);
} catch (NumberFormatException var17) {
LOG.error("illegal property 'druid.timeBetweenEvictionRunsMillis'", var17);
}
}
property = properties.getProperty("druid.maxWaitThreadCount");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setMaxWaitThreadCount(value);
} catch (NumberFormatException var16) {
LOG.error("illegal property 'druid.maxWaitThreadCount'", var16);
}
}
property = properties.getProperty("druid.maxWait");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setMaxWait((long)value);
} catch (NumberFormatException var15) {
LOG.error("illegal property 'druid.maxWait'", var15);
}
}
value = Utils.getBoolean(properties, "druid.failFast");
if (value != null) {
this.setFailFast(value);
}
property = properties.getProperty("druid.phyTimeoutMillis");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setPhyTimeoutMillis(value);
} catch (NumberFormatException var14) {
LOG.error("illegal property 'druid.phyTimeoutMillis'", var14);
}
}
property = properties.getProperty("druid.phyMaxUseCount");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setPhyMaxUseCount(value);
} catch (NumberFormatException var13) {
LOG.error("illegal property 'druid.phyMaxUseCount'", var13);
}
}
property = properties.getProperty("druid.minEvictableIdleTimeMillis");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setMinEvictableIdleTimeMillis(value);
} catch (NumberFormatException var12) {
LOG.error("illegal property 'druid.minEvictableIdleTimeMillis'", var12);
}
}
property = properties.getProperty("druid.maxEvictableIdleTimeMillis");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setMaxEvictableIdleTimeMillis(value);
} catch (NumberFormatException var11) {
LOG.error("illegal property 'druid.maxEvictableIdleTimeMillis'", var11);
}
}
value = Utils.getBoolean(properties, "druid.keepAlive");
if (value != null) {
this.setKeepAlive(value);
}
property = properties.getProperty("druid.keepAliveBetweenTimeMillis");
if (property != null && property.length() > 0) {
try {
value = Long.parseLong(property);
this.setKeepAliveBetweenTimeMillis(value);
} catch (NumberFormatException var10) {
LOG.error("illegal property 'druid.keepAliveBetweenTimeMillis'", var10);
}
}
value = Utils.getBoolean(properties, "druid.poolPreparedStatements");
if (value != null) {
this.setPoolPreparedStatements0(value);
}
value = Utils.getBoolean(properties, "druid.initVariants");
if (value != null) {
this.setInitVariants(value);
}
value = Utils.getBoolean(properties, "druid.initGlobalVariants");
if (value != null) {
this.setInitGlobalVariants(value);
}
value = Utils.getBoolean(properties, "druid.useUnfairLock");
if (value != null) {
this.setUseUnfairLock(value);
}
property = properties.getProperty("druid.driverClassName");
if (property != null) {
this.setDriverClassName(property);
}
property = properties.getProperty("druid.initialSize");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setInitialSize(value);
} catch (NumberFormatException var9) {
LOG.error("illegal property 'druid.initialSize'", var9);
}
}
property = properties.getProperty("druid.minIdle");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setMinIdle(value);
} catch (NumberFormatException var8) {
LOG.error("illegal property 'druid.minIdle'", var8);
}
}
property = properties.getProperty("druid.maxActive");
if (property != null && property.length() > 0) {
try {
value = Integer.parseInt(property);
this.setMaxActive(value);
} catch (NumberFormatException var7) {
LOG.error("illegal property 'druid.maxActive'", var7);
}
}
value = Utils.getBoolean(properties, "druid.killWhenSocketReadTimeout");
if (value != null) {
this.setKillWhenSocketReadTimeout(value);
}
property = properties.getProperty("druid.connectProperties");
if (property != null) {
this.setConnectionProperties(property);
}