3.MyBatis级联查询

MyBatis级联

  • MyBatis级联查询
  • 逆向工程

一.级联查询 N-1

1.接口
  • 旅游景点为N,对应的城市为1
//级联查询 N-1  
  Toursinfo selectById(int id);
  • 以多的一方为主表
  • 返回值是Toursinfo 参数是 Toursinfo 的id
2.映射文件
<resultMap id="tourCity" type="Toursinfo">
        <id column="id" property="id" ></id>
        <result column="introduce" property="introduce"></result>
        <result column="pubTime" property="pubTime"></result>
        <result column="price" property="price"></result>
        <result column="cityId" property="cityId"></result>
        <association property="city" javaType="city">
            <id column="cityId" property="cityId"></id>
            <result column="cityName" property="cityName"></result>
        </association>
    </resultMap>
    <select id="selectById" parameterType="int" resultMap="tourCity">
        select * from toursinfo t inner join city c on t.cityId=c.cityId where t.id=#{id}
    </select>
  • 一的一方的数据封装使用association标签

  • 使用javaType固定搭配

  • 对应javabean中的属性

      //关联 城市 表
        private City city;
    
3.测试类
@Test//通过旅游景点的id查询对应城市的名字 N-1
    public void selectById(){
        Toursinfo t = session.getMapper(ToursMapper.class).selectById(5);
        System.out.println(t.getCity().getCityName());
    }

二. 级联查询1-N

1. 接口
//级联查询 1-N  
   City selectByCityId(int cityId);
  • 以一的一方为主表
  • 返回值类型是City
  • 参数是城市的id
2. 映射文件
<resultMap id="tours" type="city">
      <id column="cityId" property="cityId"/>
      <result column="cityName" property="cityName"/>
      <collection property="tours" ofType="Toursinfo">
            <id column="id" property="id" />
            <result column="introduce" property="introduce"/>
            <result column="pubTime" property="pubTime"/>
            <result column="price" property="price"/>
            <result column="cityId" property="cityId"/>
     </collection>
</resultMap>
    <select id="selectByCityId" parameterType="int" resultMap="tours">
        select * from city c  inner join toursinfo t  on t.cityId=c.cityId where c.cityId=#{cityId}
    </select>
  • 多的一方的数据使用collection标签封装

  • 固定搭配 ofType

  • 对应javabean中的属性

    //城市关联景点 1-N
     private List<Toursinfo> tours;
    
3. 测试类
@Test//通过城市的id查询对应的所有景点 1-N
 public void selectByCityId(){
     City city = session.getMapper(ToursMapper.class).selectByCityId(1);
     System.out.println(city+"**");
 }
总结
  • 一对多 以一的一方为主表; 多对一 以多的一方为主表
  • Association 封装一的一方的数据,固定类型搭配 javaType。
  • collection封装多的一方的数据,固定类型搭配 ofType。

MyBatis逆向生成

一. 准备工作
1.Pom.xml中的依赖
<dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
 </dependency>

<!--逆向生成带分页的-->
        <dependency>
            <groupId>com.itfsw</groupId>
            <artifactId>mybatis-generator-plugin</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>

2. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 生成配置文件 -->
<generatorConfiguration>
  <context id="DB2Tables" targetRuntime="MyBatis3">
      
      <!--分页插件-->
        <plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin"/>
      
    <commentGenerator>
    	<!-- 是否去除注释,true表示是,false否 -->
			<property name="suppressAllComments" value="true" />
	</commentGenerator>
	<!-- 1.连接数据库信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/k8514?characterEncoding=UTF-8"
        userId="root"
        password="ROOT">
    </jdbcConnection>
 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
	<!-- 2.pojo类的生成配置  targetPackage表示目标文件夹
		targetProject表示当前目标文件夹所放置的目标地址
	 -->
    <javaModelGenerator targetPackage="com.kgc.pojo" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	
	<!-- 3.sql映射文件生成配置 -->
    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
	<!-- 4.mapper接口配置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.kgc.mapper"  targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
	<!-- 5.数据库表和实体类映射 -->
    <table  tableName="toursinfo" domainObjectName="Toursinfo" >
    </table>
    <table  tableName="city" domainObjectName="City" >
    </table>
  </context>
</generatorConfiguration>

  • 可能需要修改的部分:

    1. 数据库连接信息

    2. 实体类的生成路径

    3. Sql映射文件的生成路径

    4. Mapper接口的生成路径

    5. 数据库表 表名 和 关联的 实体类的类名

3. 运行自动生成的测试方法
@Test
public void create() throws Exception{
	 List<String> warnings = new ArrayList<String>();
	 boolean overwrite = true;
	 File configFile = new File("mbg.xml");
	 ConfigurationParser cp = new ConfigurationParser(warnings);
	 Configuration config = cp.parseConfiguration(configFile);
	 DefaultShellCallback callback = new DefaultShellCallback(overwrite);
	 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback,warnings);
	 myBatisGenerator.generate(null);
	}

  • 运行完刷新工程即可自动生成javabean 和 接口 以及 映射文件
4. 注意
  1. 核心配置文件 sqlMapConfig.xml的导入 db.properties 数据库四大参数配置文件的导入
  2. 核心配置文件中 对映射文件的关联
二.相关文件的介绍
  • 在生成的pojo中还有一个Example类 该类封装了所有属性的各种条件查询
  • 通过example对象创建criteria对象,该对象通过sql拼接的方式 实现条件的封装,所以开发人员只需要调用相应方法即可完成带条件的增删改查。
  • 逆向工程没有提供 分页查询 和两个表的 级联查询 这一块需要我们自己去补充添加。
三.测试
 @Test
    public void test1(){
        ToursinfoExample example = new ToursinfoExample();
        ToursinfoExample.Criteria c = example.createCriteria();
        c.andIntroduceLike("%武汉%");
        List<Toursinfo> ts =                    		session.getMapper(ToursinfoMapper.class).selectByExample(example);
        System.out.println(ts);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值