mybatis-config.xml 和 mapper基本设置
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>