MyBatis項目所引用的一切依賴jar包和自定義設置


Maven的依賴jar包:

<!--導入依賴-->
    <dependencies>
        <!--mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--Log4j日志輸出功能-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--Lombok實體POJO類簡寫支持-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <!--緩存機制-->
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

配置文件導出問題:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Maven的java版本設置問題:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
</properties>

MyBatis操作的工具類MybatisUtils.java:

package com.kuang.utils;

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;

//sqlSessionFactory--->SessionFactory
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 命令所需的所有方法。
    // 你可以通過 SqlSession 實例來直接執行已映射的 SQL 語句。
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

工具類在測試時如何正確使用:

@Test
    public void test(){
        //第一步:獲得SqlSession對象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //執行SQL  方式一:getMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //關閉SqlSession
        sqlSession.close();

    }

***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=綁定一個對應的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.StudentMapper">


<!--思路:
            1.查詢所有的學生信息
            2.根據查詢出來的tid,尋找對應的老師-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis.student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <!--復雜的屬性,我們需要單獨處理
            對象:association
            集合:collection
        -->
        <association column="tid" property="teacher" javaType="Teacher" select="getTeacher"/>

    </resultMap>


    <select id="getTeacher" resultType="Teacher">
        select * from mybatis.teacher where id=#{tid}
    </select>


    <!--按照結果嵌套處理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
        from mybatis.student s,mybatis.teacher t
        where s.tid=t.id;
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>
        <association property="teacher" javaType="Teacher">
            <result column="tname" property="name"/>
        </association>
    </resultMap>

</mapper>

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核心配置文件-->
<configuration>

    <!--引入外部配置文件-->
    <properties resource="db.properties"/>

    <settings>
        <!--標准的日志工廠實現-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--是否自動開啟駝峰命名規則映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--顯示的開啟全局緩存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>

    <!--可以給實體類起別名-->
<!--    <typeAliases>-->
<!--        <typeAlias alias="Student" type="com.kuang.pojo.Student"/>-->
<!--        <typeAlias alias="Teacher" type="com.kuang.pojo.Teacher"/>-->
<!--    </typeAliases>-->


    <!--environments配置環境組-->
    <!--default默認環境-->
    <environments default="development">
        <!--environment單個環境-->
        <environment id="development">
            <!--transactionManager配置事務管理器-->
            <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="com/kuang/dao/UserMapper.xml"/>
    </mappers>

</configuration>

db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username=root
password=a1b2c3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM