如何用spring创建ComboPooledDataSource和JdbcTemplate对象-成都快上网建站

如何用spring创建ComboPooledDataSource和JdbcTemplate对象

这篇文章主要介绍“如何用spring创建ComboPooledDataSource和JdbcTemplate对象”,在日常操作中,相信很多人在如何用spring创建ComboPooledDataSource和JdbcTemplate对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用spring创建ComboPooledDataSource和JdbcTemplate对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联是一家专业提供罗江企业网站建设,专注与做网站、成都网站建设、H5技术、小程序制作等业务。10年已为罗江众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

用spring 创建ComboPooledDataSource和JdbcTemplate对象
3.1添加ioc相关jar包


  org.springframework
  spring-core
  4.3.18.RELEASE


  org.springframework
  spring-beans
  4.3.18.RELEASE


  org.springframework
  spring-context
  4.3.18.RELEASE


  org.springframework
  spring-context-support
  4.3.18.RELEASE


  org.springframework
  spring-expression
  4.3.18.RELEASE

3.2创建db.properties文件

driverClass=com.MySQL.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
user=root
password=123

3.3在applicationContext.xml文件中创建对象





   
    
    
    



    

3.4 修改dao 层

@Component
public class UsersDao implements IUsersDao {

    //注入jdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //update方法 (增 删 改)
    @Override
    public void add(Users user) {

        String sql = "insert into users values(null,?,?)";

        jdbcTemplate.update(sql, user.getUname(), user.getPassword());

    }

3.5测试

@Test
public void test()
{
    ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

    IUsersDao usersDao=  applicationContext.getBean("usersDao",IUsersDao.class);

    Users user=new Users("xiaowanglaoshi","123");

    usersDao.add(user);
}

到此,关于“如何用spring创建ComboPooledDataSource和JdbcTemplate对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享题目:如何用spring创建ComboPooledDataSource和JdbcTemplate对象
标题来源:http://kswjz.com/article/goosdg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流