Druid是阿里開源的數據庫鏈接池,做爲後起之秀,性能比dbcp、c3p0更高,使用也愈來愈普遍。java
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency>
//數據源配置 DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC"); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); //這個能夠缺省的,會根據url自動識別 dataSource.setUsername("root"); dataSource.setPassword("abcd"); //下面都是可選的配置 dataSource.setInitialSize(10); //初始鏈接數,默認0 dataSource.setMaxActive(30); //最大鏈接數,默認8 dataSource.setMinIdle(10); //最小閒置數 dataSource.setMaxWait(2000); //獲取鏈接的最大等待時間,單位毫秒 dataSource.setPoolPreparedStatements(true); //緩存PreparedStatement,默認false dataSource.setMaxOpenPreparedStatements(20); //緩存PreparedStatement的最大數量,默認-1(不緩存)。大於0時會自動開啓緩存PreparedStatement,因此能夠省略上一句代碼 //獲取鏈接 Connection connection = dataSource.getConnection(); //Statement接口 Statement statement = connection.createStatement(); String sql1 = "insert into tb_student (name,age) values ('chy',20)"; statement.executeUpdate(sql1); //PreparedStatement接口 String sql2 = "insert into tb_student (name,age) values ('chy',21)"; PreparedStatement preparedStatement = connection.prepareStatement(sql2); preparedStatement.execute(); //關閉鏈接 connection.close();
一、在sources下新建druid.propertiesmysql
url=jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC
#這個能夠缺省的,會根據url自動識別
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=abcd
##初始鏈接數,默認0
initialSize=10
#最大鏈接數,默認8
maxActive=30
#最小閒置數
minIdle=10
#獲取鏈接的最大等待時間,單位毫秒
maxWait=2000
#緩存PreparedStatement,默認false
poolPreparedStatements=true
#緩存PreparedStatement的最大數量,默認-1(不緩存)。大於0時會自動開啓緩存PreparedStatement,因此能夠省略上一句設置
maxOpenPreparedStatements=20
二、使用web
public class Test { public static void main(String[] args) throws Exception { //數據源配置 Properties properties=new Properties(); //經過當前類的class對象獲取資源文件 InputStream is = Test.class.getResourceAsStream("/druid.properties"); properties.load(is); //返回的是DataSource,不是DruidDataSource DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); //獲取鏈接 Connection connection = dataSource.getConnection(); //Statement接口 Statement statement = connection.createStatement(); String sql1 = "insert into tb_student (name,age) values ('chy',20)"; statement.executeUpdate(sql1); //PreparedStatement接口 String sql2 = "insert into tb_student (name,age) values ('chy',22)"; PreparedStatement preparedStatement = connection.prepareStatement(sql2); preparedStatement.execute(); //關閉鏈接 connection.close(); } }
這種方式對properties的key有嚴格要求,必須值指定的字符串,容易寫錯。spring
若是以爲老調很差,能夠DruidDataSource dataSource = new DruidDataSource(); 再調用setter方法使用配置文件的值,不過很麻煩。sql
一、resources下新建druid.properties數據庫
druid.url=jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC
#這個能夠缺省的,會根據url自動識別
druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.username=root
druid.password=abcd
##初始鏈接數,默認0
druid.initialSize=10
#最大鏈接數,默認8
druid.maxActive=30
#最小閒置數
druid.minIdle=10
#獲取鏈接的最大等待時間,單位毫秒
druid.maxWait=2000
#緩存PreparedStatement,默認false
druid.poolPreparedStatements=true
#緩存PreparedStatement的最大數量,默認-1(不緩存)。大於0時會自動開啓緩存PreparedStatement,因此能夠省略上一句設置
druid.maxOpenPreparedStatements=20
二、spring配置文件緩存
<!--引入druid配置文件--> <context:property-placeholder location="classpath:druid.properties" /> <!--druid鏈接池--> <bean name="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${druid.url}" /> <property name="driverClassName" value="${druid.driverClassName}" /> <property name="username" value="${druid.username}" /> <property name="password" value="${druid.password}" /> <property name="initialSize" value="${druid.initialSize}"/> <property name="maxActive" value="${druid.maxActive}" /> <property name="minIdle" value="${druid.minIdle}" /> <property name="maxWait" value="${druid.maxWait}" /> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /> <property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}" /> </bean>
注意要配置包掃描,掃描註解。oracle
三、使用app
@Repository public class Xxx { @Resource private DruidDataSource dataSource; public void xxx() throws SQLException { //獲取鏈接 Connection connection = dataSource.getConnection(); //Statement接口 Statement statement = connection.createStatement(); String sql1 = "insert into tb_student (name,age) values ('chy',20)"; statement.executeUpdate(sql1); //PreparedStatement接口 String sql2 = "insert into tb_student (name,age) values ('chy',22)"; PreparedStatement preparedStatement = connection.prepareStatement(sql2); preparedStatement.execute(); //關閉鏈接 connection.close(); }
}
一、在druid數據源裏啓用stat過濾器性能
<property name="filters" value="stat" />
二、在web.xml中配置StatViewServlet
<servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <!--<!– 是否容許清空統計數據,不寫時默認true –>--> <!--<init-param>--> <!-- <param-name>resetEnable</param-name>--> <!-- <param-value>false</param-value>--> <!--</init-param>--> <!--<!– 用戶名。用戶名和密碼能夠不配置,不配置時進入監控頁面時不須要輸入用戶名、密碼 –>--> <!--<init-param>--> <!-- <param-name>loginUsername</param-name>--> <!-- <param-value>chy</param-value>--> <!--</init-param>--> <!--<!– 密碼 –>--> <!--<init-param>--> <!-- <param-name>loginPassword</param-name>--> <!-- <param-value>abcd</param-value>--> <!-- </init-param>--> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
啓動web應用,在 localhost:8080/druid 可看到sql的執行狀況統計。(注意不是項目下)
druid默認會清空統計數據,因此只能看到當時的狀況,應用須要持續操做數據庫纔好看到效果。