Mybatis项目前期准备工作
-
创建maven项目
-
导入依赖 mysql,mybatis,junit
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <!-- <scope>test</scope>--> </dependency> </dependencies> <!-- 在build标签中配置resources,防止配置文件无法生效问题--> <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> </resources> </build>
-
在resources目录下编写Mybaties核心配置文件mybaties-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <!--Mybatis核心配置文件--> <configuration> <!-- 引入外部配置文件--> <properties resource="db.properties"/> <!-- MyBatis 中重要的调整设置--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 给实体类取别名--> <typeAliases> <package name="com.czc.pojo"/> </typeAliases> <environments default="test"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="test"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--每一个Mapper.ML都需要Mybatis核心配置文件中注册!--> <mappers> <mapper resource="com/czc/dao/mapper/UserMapper.xml"/> </mappers> </configuration>
-
在resources目录下数据库连接配置文件db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?userSSL=false&serverTimezone=Asia/Shanghai username=root password=123456
-
编写创建sqlSessionFactory工具类MybatisUtils
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { throw new RuntimeException(e); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(true); } }