天天看點

mybatis的基本配置config配置mapper配置

mybatis的配置

  • config配置
  • mapper配置

mybatis的配置主要有兩個,一個是config配置,一個是mapper配置檔案也就是映射檔案。

config配置

配置檔案具體如下

<?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>
    <settings>
    //lazyLoadingEnabled是用來配置延遲加載的,預設值是false也就死關閉
        <setting name="lazyLoadingEnabled" value="false"/>
        /*aggressiveLazyLoading是用來設定侵入式加載的,預設值是true
        侵入式加載指的是将關聯查詢和主查詢綁定到了一起,即主查詢執行則關聯查
        詢就執行*/
        <setting name="aggressiveLazyLoading" value="true"/>
       //logImpl是用來配置日志的設定,配置它在運作是可以看到組合好的sql語句
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    //typeAliases是用來起别名的标簽
    <typeAliases>
    //使用package标簽就可以讓本包下的所有類重命名,且重命名的名稱為此類的類名
    不區分首字母大小寫
        <package name="priv.ze.mybatis.domain"/>
    </typeAliases>
    //environments标簽的作用是來配置資料庫的,其中可以存放多個資料庫的連接配接
    <environments default="development">
    //environment主要就是用來配置單個資料庫的連接配接
        <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/db?serverTimezone=GMT%2B8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="00312000"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
    //mapper标簽是用來配置映射檔案的
   <mapper resource="config/mapper/StudentMapper.xml"/>
   //誰用package标簽來進行配置的話,必須要讓xml檔案,也就是mapper檔案與接口寫在同一目錄下,且名稱必須相同,且必須使用mapper代理
        <package name="priv.ze.mybatis.mapper"/>
    </mappers>
</configuration>
           

mapper配置

mapper中主要寫了sql語句以及一些調用sql語句的屬性

<?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="priv.ze.mybatis.mapper.StudentMapper">
<resultMap  type="student" id="studentResultMap">
    <id column="ID" property="stuID"/>
    <result column="name" property="stuName"/>
</resultMap>
<select id="getByStuID"  resultType="student">
    select * from student
    <where>
        <if test="stuID!=null and stuID!=''">
            and stuID=#{stuID}
        </if>
    </where>
</select>
<select id="getAll" resultType="priv.ze.mybatis.domain.Student">
    select stuName,tel from student
</select>
<insert id="save" parameterType="priv.ze.mybatis.domain.Student">
    insert into student (stuName,stuID,tel,major,direction,state) values (#{stuName},#{stuID},#{tel},#{major},#{direction},#{state})
</insert>
<delete id="deleteone">
    delete from administrator where tel=#{d}
</delete>
<update id="updateone" parameterType="priv.ze.mybatis.domain.Student1" >
    update administrator set tel = #{tel} where stuID = #{stuID}
</update>
<!--對resultMap的使用-->
<select id="findByMajor" resultMap="studentResultMap">
    select stuID ID,stuName name from student where major=#{major}
</select>
</mapper>
           

mapper配置中主要牽扯到了一些屬性,例如resultMap,resultType,parameterType。

resultMap主要是用來定義傳回的包裝類型的。

resultType相對于resulrMap功能稍微少一些,不能傳回集合。

parameterType主要就是傳參數。