<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
导入依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency>
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </build>
resource目录下资源文件
db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8 user=root port=3306 pwd=123456
mybatis.config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!-- 数据库连接配置文件 --> <properties resource="db.properties"/> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 数据库环境配置,可配置多套。default指出当前使用的是哪个环境。 --> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC" /> <!-- 配置数据库连接 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url2}" /> <property name="username" value="${user}" /> <property name="password" value="${pwd}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/iit/dao/GoodMapper.xml"/> <mapper resource="com/iit/dao/UserMapper.xml"/> <mapper resource="com/iit/dao/SqlMapper.xml"/> </mappers> </configuration>
连接池连接
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8 user=root port=3306 pwd=123456 initialSize=2 maxActive=3 minIdle=2 maxWait=2000 poolPreparedStatements=true
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!-- 数据库连接配置文件 --> <properties resource="db.properties"/> <settings> <!-- 打印日志--> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 字段名属性处理 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <!-- 别名设置--> <typeAlias type="com.iit.pojo.User" alias="user"/> </typeAliases> <!-- 数据库环境配置,可配置多套。default指出当前使用的是哪个环境。 --> <environments default="dev"> <!-- 连接环境信息 --> <environment id="dev"> <!-- mysql使用什么事物管理方式 --> <transactionManager type="JDBC"/> <!-- mybatis使用连接池方式来获取连接对象 --> <dataSource type="com.iit.utils.DruidDataSourceFactory"> <property name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="minIdle" value="${minIdle}"/> <property name="maxWait" value="${maxWait}"/> <property name="poolPreparedStatements" value="${poolPreparedStatements}"/> </dataSource> </environment> </environments> <mappers> <!-- 注册--> <mapper resource="com/iit/dao/UserMapper.xml"/> <mapper resource="com/iit/dao/SqlMapper.xml"/> </mappers> </configuration>
插入一个类
package com.iit.utils; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.datasource.pooled.PooledDataSourceFactory; public class DruidDataSourceFactory extends PooledDataSourceFactory { public DruidDataSourceFactory(){ this.dataSource = new DruidDataSource(); } }
标签:jdbc,配置,环境,3306,mysql,mybatis,org,com From: https://www.cnblogs.com/SB-PT/p/16600232.html