2.2Maven仓库
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency>
<dependencies> <!--dependency> <groupId>org.wyl</groupId> <artifactId>[the artifact id of the block to be mounted]</artifactId> <version>1.0-SNAPSHOT</version> </dependency--> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!-- 整合log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <!-- 测试junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
右击pom.xml->meave 重新加载项目。包下载完即可。
将 mybatis-x.x.x.jar 文件置于 classpath 中即可。
3.2.2.全局配置文件 mybatis-config
<?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标签 => 声明MyBatis核心配置 --> <configuration> <!-- environments标签 => 设置MyBatis选用的环境信息 --> <environments default="development"> <environment id="development"> <!-- transactionManager标签 => 事务管理 --> <transactionManager type="JDBC"/> <!-- dataSource标签 => 配置数据源属性 --> <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映射文件注册到全局配置文件中--> <mappers> <mapper resource="org/mybatis/example/WylMapper.xml"/> </mappers> </configuration>
<?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标签 => 声明MyBatis核心配置 --> <configuration> <!-- environments标签 => 设置MyBatis选用的环境信息 --> <environments default="development"> <environment id="development"> <!-- transactionManager标签 => 事务管理 --> <transactionManager type="JDBC"/> <!-- dataSource标签 => 配置数据源属性 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/wyl?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=CST"/> <property name="username" value="nps"/> <property name="password" value="123.nps@zst"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/userMapper.xml"/> </mappers> </configuration>
3.2.3.SqlSessionFactory 会话工厂
-
通过Mybatis的配置信息,使用
SqlSessionFactoryBuilder
构建器,来构建会话工厂对象 -
SqlSessionFactory 创建了 Configuration对象,使用 Configuration对象来构建SqlSession会话工厂
-
SqlSessionFactoryBuilder构建器使用了 Builder构建者设计模式
-
SqlSessionFactory 会话工厂,使用了工厂设计模式
public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { // 定义XML核心配置文件路径信息 String resource = "mybatis-config.xml"; // 读取XML核心配置文件路径信息 InputStream inputStream = Resources.getResourceAsStream(resource); // 获得实例化SQLSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //调用SqlSessionFactory.openSession()方法,返回SqlSession对象 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
https://www.cnblogs.com/wyl1924/p/15188487.html