第一步,写依赖,导入jar包。提供技术支持。
创建模块,导入坐标,在创建好的模块中的 pom.xml 配置文件中添加依赖的坐标
- <dependencies>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.5version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.16version>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.13version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>1.7.20version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-coreartifactId>
- <version>1.2.3version>
- dependency>
- dependencies>
第二部编写Mybatis的配置文件

在resource文件夹下,新建一个mybatis-config.xml的文件

然后编写xml文件,首先呢,我们写一下约束
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">

然后再configuration里写上那啥。
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <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 resource="org/mybatis/example/BlogMapper.xml"/>
- mappers>
- configuration>
看看官方文档:
-
default="development"> //配置数据源,里面可以有很多environment - <environment id="development"> //其中一个environment
- <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>
-
- <mappers>
-
- <mapper resource="UserMapper.xml"/>
- mappers>
这里的是啥呢?这就是第三步,在resource文件夹下面创建sql的文件mapper,专门写sql的文件。为了高内聚,低耦合


文件名必须与 这里的文件名一致!!!!!!!
- <mappers>
-
- <mapper resource="Mapper/UserMapper.xml"/>
- mappers>
然后我们编写xml文件

首先是添加约束:
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="test">
- <select id="selectAll" resultType="com.itheima.User">
- select * from tb_user;
- select>
- mapper>
id是给这次查询取个名字,而 resultType是返回的结果,要赋值给实体类。
resultType="com.itheima.User"> 因此这边要创建一个实体类

这里面的私有字段要和你在数据库里面的一样。

在 com.itheima 包下编写 MybatisDemo 测试类
- public class MyBatisDemo {
-
- public static void main(String[] args) throws IOException {
- //1. 加载mybatis的核心配置文件
- String resource = "mybatis-config.xml";
- //读取配置文件
- InputStream inputStream = Resources.getResourceAsStream(resource);
- //通过配置文件生产sqlSessionFactory
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
-
- //2. 获取SqlSession对象,用它来执行sql
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //3. 执行sql
- List
users = sqlSession.selectList("test.selectAll"); //参数是一个字符串,该字符串必须是映射配置文件的namespace.id - System.out.println(users);
- //4. 释放资源
- sqlSession.close();
- }
- }
最后在configuration里添加这句话
-
- <typeAliases>
- <package name="com.itheima.Domain">
- typeAliases>