发表于: 2018-09-10 21:06:36

1 521


今天完成了spring框架使用JDBC Tempalte

配置bean文件

凡是子类及带属性、方法的类都注册Bean到Spring中,交给它管理

完成bean1之后 创建一个testjdbc 测试类 进行测试


package entity;

import dao.TestDaoImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestJdbc {
TestDaoImpl testDaoImpl;

   @Before
   public void init() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
       TestDaoImpl testDaoImpl = (TestDaoImpl) applicationContext.getBean("testDaoImpl");
       this.testDaoImpl = testDaoImpl;
   }

@Test
   public void runTest1Add() {
Test1 test = new Test1();
       test.setName("spring-jdbc");
       test.setSchool("spring-password");
       test.setQq("453456465");
       testDaoImpl.add(test);
   }
@Test
   public void runUserGetCountNum() {
int count = testDaoImpl.getCountNum();
       System.out.println("TestJdbc Test1 Count: " + count);
   }
@Test
   public void runTest1Get() {
int id = 4;
       Test1 test = testDaoImpl.get(id);
       System.out.println(test);
   }
}


通过以上简单案例,我们使用Spring中的JdbcTemplate(即对JDBC提供的封装)实现了添加操作。但在该案例中实际上存在着一个问题,即DriverManagerDataSource对象在UserDaoImpl类中被创建,无法为Dao层中其他的操作数据库的实现使用,除此之外,DriverManagerDataSource底层获取数据库连接是通过DriverManager.getConnection获取,每次调用DriverManagerDataSourcegetConnection获取对数据库的连接,都相当于创建一个新的连接,这种方式下耗费内存和时间,实用性低。我们需要一个数据库连接池,能有效地负责创建、管理和分配数据库连接。为了方便对Spring的JdbcTemplate进行讲解,仍采用这种形式创建数据源。后续将介绍c3p0连接池的使用(c3p0实现了DataSource接口,维护了数据库连接,负责创建、管理和分配数据库连接)。


查询操作

返回一个值

案例:获取Test1表中的记录数。 
实现:调用JdbcTemplate对象的queryForObject方法。 

public int add(Test1 test) {
System.out.println("start add method");
   String sql = "insert into xzyuan(name,qq,school) values(?,?,?)";
   int rows = jdbcTemplate.update(sql, test.getName(), test.getQq(), test.getSchool());
   System.out.println("method result: " + rows);
   return rows;

返回对象(返回一行数据)

案例:根据用户ID获取对应的用户记录信息。 
实现:调用JdbcTemplate对象的queryForObject方法,结果查询结果为一个对象,要求queryForObject方法的第二个参数传入一个实现了RowMapper接口的实现类(实现自己数据的封装)

@Override
public Test1 get(int id) {
String sql = "SELECT * FROM xzyuan WHERE id = ?";
   Test1 test = jdbcTemplate.queryForObject(sql, new Test1RowMapper(), id);
   return test;
}

定义一个RowMapper接口的内部实现类,其作用是将查询结果封装为某一自定义对象后返回,如下:

public class Test1RowMapper implements RowMapper<Test1> {

@Override
   public Test1 mapRow(ResultSet resultSet, int i) throws SQLException {
// 1. 从结果集中取出数据
       int id = resultSet.getInt("id");
       String name = resultSet.getString("name");
       String qq = resultSet.getString("qq");
       String school = resultSet.getString("school");


       // 2. 将数据封装到对象中
       Test1 test = new Test1();
       test.setId(id);
       test.setName(name);
       test.setQq(qq);
       test.setSchool(school);

       return test;
   }

在TestJdbc测试类中添加测试方法,


@Test
public void runUserGetCountNum() {
int count = testDaoImpl.getCountNum();
   System.out.println("TestJdbc Test1 Count: " + count);
}
@Test
public void runTest1Get() {
int id = 4;
   Test1 test = testDaoImpl.get(id);
   System.out.println(test);
}

Spring配置c3p0连接池

c3p0连接池介绍

  在上述中提及了使用DriverManagerDataSource存在的问题,即DriverManagerDataSource未对创建的数据库连接进行有效管理,对于每一次获取数据库连接(即DriverManager.getConnection)都会新建新的数据库连接,这样的做法对耗费内存和时间,实用性低且这种方式获取的连接需要手动关闭,不然会大量的占用内存。 
  那么为对数据库连接进行有效管理,可以使用c3p0连接池,即它会帮我们考虑初始创建的数据库连接数,如何分配数据库连接,以及关闭数据库连接后connection对象是放回池内,还是close销毁等问题。对于连接池的实现,均要求实现了DataSource接口(DriverManagerDataSource也是实现了该接口)。DataSource接口,内容如下:

package entity;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.TestService;

public class TestC3p0 {
@Test
   public void runC3p0() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
       TestService testService = (TestService) context.getBean("testService");
       Test1 test = new Test1();
       test.setName("spring-c3p0");
       test.setQq("c3p0-233333");
       test.setSchool("affefa");
       testService.add(test);
   }
@Test
   public void runC3p02() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
      TestService testService = (TestService) context.getBean("testService");
       Test1 test = new Test1();
       test.setName("spring-c3p03");
       test.setQq("c3p0-233333-2");
       test.setSchool("dffsfad");
       testService.add(test);
   }
}

使用c3p0连接池

  这里以添加用户信息到数据库中为例。 
(1)首先导入jar包(除上述提到的jar包,还需导入),即c3p0-0.9.2.1.jarmchange-commons-java-0.2.3.4.jar(c3p0 jar包的下载,可以到这里进行搜索下载。) 
创建IUserDao类

等等等

https://blog.csdn.net/qq_15096707/article/details/79677714

今天的学习有几个步骤熟悉了 然后新认识了一个c3p0连接池

beans 和c3p0连接池在之前原生的jdbc中是没有的

我下去得好好领悟一下

顺手看了看Mybatis连接数据库,相比于jdbc 步骤要少一点,需要配置的xml文件也在官网会有例子

今天的收获是基本完成了task17,还差点感想

今天在JDBC Template 测试的时候 因为bean的配置不当

导致了很多错误

明天了解Junite

并明白注意使用JDBCTemplate的时候分离Interface和Imple,使用Mybatis的时候注意理解为什么不需要Impl。



返回列表 返回列表
评论

    分享到