日期:2014-05-16  浏览次数:20453 次

转:在Red5中使用Spring-JDBC配置MySql连接池
对没有集成TOMCAT的RED5一般可使用Hibernate和Spring配置MySql数据库的连接池.这里是以Spring-JDBC来完成的.

一、配置需要以下的包:

spring-jdbc.jar
spring-dao.jar
commons-dbcp-1.2.1.jar
mysql-connector-java-5.0.5-bin.jar

将他们放到%RED5_HOME%\lib下即可

二、配置文件:

1、red5-web.properties

加入如下几行:根据你的数据库环境自行修改其值

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=root

2、red5-web.xml

<!-- Database connection pool bean -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName"><value>${db.driver}</value></property>
   <property name="url"><value>${db.url}</value></property>
   <property name="username"><value>${db.username}</value></property>
   <property name="password"><value>${db.password}</value></property>
   <property name="poolPreparedStatements"><value>true</value></property>
   <property name="maxActive"><value>10</value></property>
   <property name="maxIdle"><value>10</value></property>
</bean>

3、Red5中调用:

         1)示例方法,RowMapper方式:

   public String getSampleString()
    {
       //Getting the datasource bean
       Object o=scope.getContext().getBean("myDataSource");
       JdbcTemplate t=new JdbcTemplate((BasicDataSource)o);
     
       //我的测试数据库为test,数据表为users;
       final List l=t.query("SELECT * FROM users;",new RowMapper(){
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
         return new MappedRow(rs.getInt(1),rs.getString(2));
        }
       });
     
       //循环读取
       final Iterator<MappedRow> i=l.iterator();
       String s="";
       while(i.hasNext()){
        s+=i.next().getName()+";";
       }
       //return the result for an function
       logger.info("------------------------");
       System.out.println("------------------------");
       System.out.println("Spring jdbc pool worked.");
       return s;
     
    }

     2)MappedRow:

public class MappedRow {
protected int id;
protected String name;

public MappedRow(int _id,String _name) {
   id=_id;
   name=_name;
}
public int getId() {
   return id;
}
public String getName() {
   return name;
}
}

以上。