配置Druid数据源
1、导入相关依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
2、创建获取druid数据源的类
public class DruidDataSourceFactory implements DataSourceFactory {
private Properties properties;
@Override
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public DataSource getDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.configFromPropety(properties);
return druidDataSource;
}
}
3、mybatis-config文件修改
<properties>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
<typeAliases>
<typeAlias type="com.ldxy.DataSource.DruidDataSourceFactory" alias="DRUID"/>
</typeAliases>
<environments default="test">
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="DRUID">
<property name="druid.driverClassName" value="${driver}"/>
<property name="druid.url" value="${url}"/>
<property name="druid.username" value="${username}"/>
<property name="druid.password" value="${password}"/>
</dataSource>
</environment>
</environments>