MyBatis文件配置学习笔记

mybatis环境配置

1 创建数据库

2 在maven中导入jar包

<!--过滤静态资源-->
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>

</build>
<modules>
    <module>mybatis-02</module>
    <module>mybatis-01</module>
    <module>mybatis-03</module>
</modules>

<dependencies>

    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>

    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>

    <!--junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    
     <!--log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
    </dependencies>
    
     <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok
		偷懒神器
-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

3 在 resource下配置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"/>

    <settings>
        <!--标准的日志工厂实现-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--可以给实体类起别名-->
    <typeAliases>
        <package name="com.kuang.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="${pwd}"/>
            </dataSource>
        </environment>
    </environments>

	<!-- 映射资源十分重要-->
    <mappers>
       <!-- 写入要映射的文件 有四种方法映射-->
    </mappers>
</configuration>

要在写一个外部配置文件,加载property的属性

value的值一般为:jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"

4 在Java下写工具类utils下MyBatisUtils类

基本固定配置

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static{
        try {
            //使用Mybatis第一步:获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。
    // SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。
    public static SqlSession  getSqlSession(){
        return sqlSessionFactory.openSession();
    }

}

5 写实体层 pojo下写一个实体类

 

6 在dao层下写一个interf接口和.xml文件

interface接口中:

//根据ID查询用户
User getUserById(int id);

写一个方法就行

mapper.xml文件中:

<?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">

<!-- 
		namespace要绑定对应的mapper接口
		例如:com.xxx.dao.StudentMapper
-->
<mapper namespace="">

    <!-- 要写的内容-->
    
</mapper>

7 在测试路径下写Test测试类

@Test
public void studentTest(){

    <!-- 获取session对象-->
    SqlSession session = MybatisUtils.getSession();

    TeacherMapper mapper = session.getMapper(TeacherMapper.class);
    List<Student> teachers = mapper.selectTeacher(1);
    for(Teacher teacher:teachers) {
        System.out.println(teacher);
    }

    session.close();
}

 

posted @ 2021-11-15 21:30  _图南  阅读(13)  评论(0编辑  收藏  举报