package com.example.expriment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
@ComponentScan(basePackages = "com.example.dao")
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = true)
/**
* @author join
* @date 2022/12/6 - 12:30
*/
public class SpringJDBCConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUserName;
@Value("${jdbc.password}")
private String jdbcPassword;
/**
*配置数据源
*/
@Bean
public DriverManagerDataSource dataSource(){
DriverManagerDataSource myDataSourse = new DriverManagerDataSource();
//数据库驱动
myDataSourse.setDriverClassName(jdbcDriverClassName);;
//相应驱动的jdbcUrl
myDataSourse.setUrl(jdbcUrl);
//数据库的用户名
myDataSourse.setUsername(jdbcUserName);
//数据库的密码
myDataSourse.setPassword(jdbcPassword);
return myDataSourse;
}
/**
*配置JDbcTemplate
*/
@Bean(value = "jdbcTemplate")
public JdbcTemplate getJdbcTemplate(){
return new JdbcTemplate(dataSource());
}
}
dao层
package com.example.dao;
import java.util.List;
import com.example.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
/**
* @author join
* @date 2022/12/8 - 15:37
*/
@Repository
public class testDaoImpl implements testDao {
@Autowired
//使用配置类中的JDBC模板
private JdbcTemplate jdbcTemplate;
/**
* 更新方法,包括添加、修改、删除
*/
public int update(String sql,Object[] param){
return jdbcTemplate.update(sql,param);
}
/**
* 查询方法
*/
public List<MyUser> query(String sql,Object[] param){
RowMapper<MyUser> rowMapper = new BeanPropertyRowMapper<MyUser>(MyUser.class);
return jdbcTemplate.query(sql,rowMapper);
}
}
server层
package com.example.service;
import java.util.List;
import com.example.dao.testDao;
import com.example.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author join
* @date 2022/12/8 - 16:09
*/
@Service
public class testServiceImpl implements testService{
@Autowired
public testDao testdao;
@Override
public void testJDBC(){
String inserSql ="insert into user(主键,姓名,年龄,性别) values(?,?,?,?)";
//数组param的值与insertSql语句中的?一 一对应
Object[] param1 = {"1","一","19","男"};
Object[] param2 = {"2","二","19","男"};
Object[] param3 = {"3","三","19","男"};
Object[] param4 = {"4","四","19","男"};
Object[] param5 = {"5","五","19","男"};
Object[] param6 = {"6","六","19","男"};
Object[] param7 = {"7","七","19","男"};
Object[] param8 = {"8","八","19","男"};
Object[] param9 = {"9","九","19","男"};
Object[] param10 = {"10","十","19","男"};
//添加用户
testdao.update(inserSql,param1);
testdao.update(inserSql,param2);
testdao.update(inserSql,param3);
testdao.update(inserSql,param4);
testdao.update(inserSql,param5);
testdao.update(inserSql,param6);
testdao.update(inserSql,param7);
testdao.update(inserSql,param8);
testdao.update(inserSql,param9);
testdao.update(inserSql,param10);
//查询用户
String selectSql = "select * from user";
List<MyUser> list = testdao.query(selectSql,null);
for (MyUser mu:list){
System.out.println(mu);
}
}
}
启动类
package com.example.expriment;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.service.testService;
import org.springframework.context.annotation.ComponentScan;
/**
* @author join
* @date 2022/12/8 - 16:26
*/
@ComponentScan(basePackages = {"com.example.service.testService"})
public class testJDBC {
public static void main(String[] args) {
//初始化Spring容器ApplicationcationContext
AnnotationConfigApplicationContext appcon =
new AnnotationConfigApplicationContext(SpringJDBCConfig.class);
testService ts = appcon.getBean(testService.class);
ts.testJDBC();
appcon.close();
}
}

总是会报扫不到包,但是注释啥的都有;问了别人说是不知道啥要写到配置类中去,求解啊!
你这个自动扫描,加在main方法这个类上,框架 很明显 不能 自动 识别的吧, 你要用 SpringBootTest 或 SpringTest的方式,进行单元测试
可以像这样测试:
如果硬要 用 main方法跑,AnnotationConfigApplicationContext 要用 这个 scan(String)进行类路径扫描
如有帮助,欢迎点赞+采纳哈!
测试类上的包扫描写到 com.example.service 这里就行了
尝试使用 springboot的单元测试跑一下