1. 程式人生 > >mybatis-config.xml 和 mapper基本設置

mybatis-config.xml 和 mapper基本設置

agg prop order by jdbc type 加載 cti ali fault

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>
    
    <!-- 讀取db.properties文件 -->
    <properties resource="db.properties" />
    <!--  全局配置參數,需要時再設置 -->
    <settings>
        <!-- 開啟全局二級緩存 -->
        <!-- <setting name="cacheEnabled" value="true" /> -->
        <!-- 開啟全局延遲加載 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    
    <typeAliases>
        <typeAlias type="com.Mybaits.pojo.ctAniamlPojo"
            alias
="ctAniaml" /> <typeAlias type="com.Mybaits.pojo.animalPojo" alias="Aniaml" /> <typeAlias type="com.Mybaits.pojo.thirdExtendsPojo" alias="ThirdExtends" /> </typeAliases> <!-- 配置數據源 --> <environments default="development"> <environment id="development"> <!-- 配置事務 --> <transactionManager type="JDBC" /> <!-- 配置連接池 --> <dataSource type="POOLED"> <property name="driver" value="${dataSource.driverClass}" /> <property name="url" value="${dataSource.jdbcUrl}" /> <property name="username" value="${dataSource.user}" /> <property name="password" value="${dataSource.password}" /> </dataSource> </environment> </environments> <!-- 映射Sql.xml文件 --> <mappers> <mapper resource="com/Mybaits/mapper/ctAniamlMapper.xml" /> <mapper resource="com/Mybaits/mapper/animalMapper.xml" /> </mappers> </configuration>

SQL語句映射mapper:

<?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="com.Mybaits.mapper.animalmapperInteger">
    <!-- 嵌套查詢 -->
    <resultMap type="Aniaml" id="SelectAnimalResultMap">
        <id column="id" property="id" />
        <result column="animal" property="animal" />
        <association column="Species_id" property="ctAniaml"
            javaType
="ctAniaml" select="com.Mybaits.mapper.ctAnimalmapperInteger.SelectCtAnimalById" /> </resultMap> <select id="SelectAnimalById" parameterType="int" resultMap="SelectAnimalResultMap"> select *from animal where id=#{id} </select> <!-- 嵌套結果 --> <resultMap type="Aniaml" id="AniamlResultMapNest"> <id column="an_id" property="id" /> <result column="animal" property="animal" /> <!-- <result column="Species_id" property="speciesid" /> --> <association column="Species_id" property="ctAniaml" javaType="ctAniaml"> <id column="id" property="id" /> <result column="species" property="species" /> </association> </resultMap> <select id="SelectContent" resultMap="AniamlResultMapNest"> SELECT a.id , a.Species , b.id as an_id, b.animal FROM cttable a LEFT JOIN animal b ON a.id=b.Species_id ORDER BY a.id </select> <!-- 擴展結果集 --> <resultMap type="ThirdExtends" id="SelectAnimalExtendsResultMap"> <id column="a_id" property="Extend_id" /> <result column="a_animal" property="animal" /> <result column="b_species" property="species" /> </resultMap> <select id="SelectAnimalByIdExtends" parameterType="int" resultMap="SelectAnimalExtendsResultMap"> SELECT a.id as a_id, a.animal as a_animal, b.Species as b_species FROM animal a LEFT JOIN cttable b ON a.Species_id=b.id WHERE a.id=#{id}; </select> </mapper>

mybatis-config.xml 和 mapper基本設置