项目结构
$ tree . ├── README.md ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── mouday │ │ ├── mapper │ │ │ ├── PersonMapper.java │ │ │ └── PersonMapper.xml │ │ ├── pojo │ │ │ └── Person.java │ │ └── util │ │ └── MyBatisUtil.java │ └── resources │ ├── db.properties │ ├── mybatis-config.xml │ └── sql │ └── person.sql └── test └── java └── com └── mouday └── PersonTest.java
依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.mouday</groupId> <artifactId>mybatis-demo</artifactId> <version>1.0-SNAPSHOT</version> <build> <resources> <!--编译src/main/java目录下的xml文件--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
数据库配置
db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/data username=root password=123456
MyBatis配置 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" /> <!-- 打印sql日志 --> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 指定整个包下的类都是别名 --> <typeAliases> <package name="com.mouday.pojo"/> </typeAliases> <environments default="development"> <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> </environments> <mappers> <mapper class="com.mouday.mapper.PersonMapper"/> </mappers> </configuration>
建表插入数据 person.sql
-- 建表 create table person( id int not null primary key auto_increment, name varchar(20), age int ); -- 插入数据 insert into person(name, age) values('刘备', 25); insert into person(name, age) values('关羽', 24); insert into person(name, age) values('张飞', 23); insert into person(name, age) values('曹操', 25); insert into person(name, age) values('许褚', 24); insert into person(name, age) values('孙权', 25); insert into person(name, age) values('周瑜', 24);
工具类 MyBatisUtil.java
package com.mouday.util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MyBatisUtil { private static SqlSessionFactory factory = null; // 使用static静态代码块,随着类的加载而加载,只执行一次 static { try { String resource = "mybatis-config.xml"; // 加载MyBatis的主配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); // 通过构建器(SqlSessionFactoryBuilder)构建一个SqlSessionFactory工厂对象 factory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } public static SqlSession getSqlSession() throws IOException { return factory.openSession(); } }