spring中配置MySql数据源,怎样配置数据库信息
1个回答
展开全部
spring中配置数据源的几种常见方式:
#mysql 数据库配置(jdbc.properties)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
1.使用spring自带的数据源org.springframework.jdbc.datasource.DriverManagerDataSource;
方式一:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
p标记需要:xmlns:p="http://www.springframework.org/schema/p"
DriverManagerDataSource源码实现:
public class DriverManagerDataSource extends AbstractDriverBasedDataSource {
public DriverManagerDataSource() {
}
public DriverManagerDataSource(String url) {
}
public DriverManagerDataSource(String url, String username, String password) {
}
public DriverManagerDataSource(String url, Properties conProps) {
}
public void setDriverClassName(String driverClassName) {
}
protected Connection getConnectionFromDriver(Properties props) throws SQLException {
}
protected Connection getConnectionFromDriverManager(String url, Properties props) throws SQLException {
}
}
#mysql 数据库配置(jdbc.properties)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
1.使用spring自带的数据源org.springframework.jdbc.datasource.DriverManagerDataSource;
方式一:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
p标记需要:xmlns:p="http://www.springframework.org/schema/p"
DriverManagerDataSource源码实现:
public class DriverManagerDataSource extends AbstractDriverBasedDataSource {
public DriverManagerDataSource() {
}
public DriverManagerDataSource(String url) {
}
public DriverManagerDataSource(String url, String username, String password) {
}
public DriverManagerDataSource(String url, Properties conProps) {
}
public void setDriverClassName(String driverClassName) {
}
protected Connection getConnectionFromDriver(Properties props) throws SQLException {
}
protected Connection getConnectionFromDriverManager(String url, Properties props) throws SQLException {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询