要导入的包 pom.xml
<properties>
<mybatis.version>3.5.2</mybatis.version>
<mysql.version>8.0.20</mysql.version>
</properties>
<dependencies>
<!--mybatis start-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis end-->
<!--mysql driver start-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mysql driver end-->
</dependencies>
<!--资源文件编译到classes-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true<filtering/>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true<filtering/>
</resource>
</resources>
</build>
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>
<!--加载JDBC配置文件-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/learn?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--注册mapper映射 -->
<mappers>
<!--方式1:单例指定一个xml文件-->
<!--<mapper resource="org/example/mapper/BankDao.xml"/>-->
<!--方式2:直接指定一个包去扫描-内保包含多个Mapper配置
文件名都必须一样和在一个包下 -->
<package name="org.example.mapper"/>
</mappers>
</configuration>
映射文件 mapper 与 接口 与 bean类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--绑定一个对应的接口-->
<mapper namespace="org.example.mapper.BankDao">
<!--
resultType 查询单个表时使用
resultMap 查询多表连接时使用
-->
<!--返回一个List 泛形为Bank-->
<select id="getBankList" resultType="org.example.bean.Bank">
select * from bank;
</select>
</mapper>
public interface BankDao {
public List<Bank> getBankList();
}
public class Bank {
private int bank_id;
private String bank_name;
private char bank_gender;
private double bank_balance;
}
工具类
/*获取SqlSession工具类*/
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
String resource = "resources/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
我们可以从中获得 SqlSession 的实例。
SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
*/
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
测试类
public class TestJdbc {
public static void main(String[] args){
/*获取 SqlSession*/
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
/*获取对应的映射*/
BankDao mapper = sqlSession.getMapper(BankDao.class);
/*调用方法 返回结果*/
List<Bank> bankList = mapper.getBankList();
for(Bank bank:bankList){
System.out.println(bank);
}
}
}