Mybatis笔记(三):逆向工程

2018-07-27  本文已影响0人  喵了个星

[toc]

概述

  1. 官网提供的mapper自动生成工具mybatis-generator-core-1.3.6.jar
  2. 可以生成 po类,mapper映射文件,mapper接口
  3. 支持单表查询(简单查询,条件查询,1.3.6提供动态sql)
  4. 官网MyBatis Generator使用文档:http://www.mybatis.org/generator/index.html

mybatis-generator逆向工程

如何搭建

附录:我的代码地址

https://github.com/Machine4869/MyCode/tree/master/Mybatis逆向工程/

一些映射 生成规则

Mapper接口测试与使用

(法一)采用Example进行条件查询

配置:targetRuntime="MyBatis3"

<context id="testTables" targetRuntime="MyBatis3">

常用接口

//按id 查询
UserselectByPrimaryKey(String id);
//按id 删除
int deleteByPrimaryKey(String id);
//按id 更新:对象中所有字段
int updateByPrimaryKey(User record);
//按id 更新:对象中非空字段
int updateByPrimaryKeySelective(User record);

//插入对象 所有字段
//  :insert into user(id,username,sex....) values..
int insert(User record);
//插入对象 非空字段
//  :insert into user(username) values..
int insertSelective(User record);

//按条件 删除
int deleteByExample(UserExample example);

//按条件 查询 结果集
List<User> selectByExample(UserExample example);

条件查询:

@Test
public void test3(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    //查询所有
    /*
    UserExample userExample = null;
    List<User> userList = userMapper.selectByExample(userExample);
    */

    //单条件查询
    /*
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andUsernameLike("%m%");
    List<User> userList = userMapper.selectByExample(userExample);
    */

    //多条件查询and
    /*
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andUsernameLike("%m%");
    criteria.andSexEqualTo("女");
    List<User> userList = userMapper.selectByExample(userExample);
    */

    //多条件查询or
    /*
    UserExample userExample = new UserExample();

    UserExample.Criteria criteria1 = userExample.createCriteria();
    criteria1.andUsernameLike("%m%");

    UserExample.Criteria criteria2 = userExample.createCriteria();
    criteria2.andSexEqualTo("女");

    userExample.or(criteria1);
    userExample.or(criteria2);

    List<User> userList = userMapper.selectByExample(userExample);
    */

    //排序
    /*
    UserExample userExample = new UserExample();

    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andUsernameLike("%m%");

    //userExample.setOrderByClause("id asc");//asc:正序排 desc:逆序排
    //userExample.setOrderByClause("id desc");
    userExample.setOrderByClause("sex asc,username asc");

    List<User> userList = userMapper.selectByExample(userExample);
    */

    //统计
    /*
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();

    criteria.andUsernameLike("%m%");
    criteria.andIdBetween(1,5);//包括1和5

    long count = userMapper.countByExample(userExample);
    */
    
}

(法二)MyBatis Dynamic SQL(用where子句进行条件查询)

  1. 概述:

    • generator 使用为MyBatis3DynamicSQL生成代码,这些类依赖于MyBatis Dynamic SQL
    • MyBatis Dynamic SQL 是生成动态 SQL 语句的框架,可以配合为MyBatis Generator使用
    • MyBatis Dynamic SQL 使用WHERE子句(可以用任意组合的and和or来创建)进行条件查询
    • MyBatis Dynamic SQL lib下载地址
  2. 准备工作

使用:

前提:import静态支持类

import static com.machine.pro.mapper.UserDynamicSqlSupport.*;  // import 自动生成的 "support" 类
import static org.mybatis.dynamic.sql.SqlBuilder.*;  // import MyBatis Dynamic SQL where support

使用案例:

@Test
public void test2(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    /**
     * MyBatis Dynamic SQL
     * build():所有构建器都通过调用build()方法完成
     * execute():调用execute方法执行语句
     */
    //按主键查询 仍然可用
    /*
    User user = userMapper.selectByPrimaryKey(3);
    */

    //查询所有(不用where子句)
    /*
    List<User> userList = userMapper.selectByExample()
            .build().execute();
    */

    //单条件查询
    /*
    List<User> userList = userMapper.selectByExample()
            .where(sex, isEqualTo("女"))
            .build().execute();
            //如sex属性就来自import static 的支持类
    */

    //多条件查询and
    /*
    List<User> userList = userMapper.selectByExample()
            .where(sex, isEqualTo("女"))
            .and(username, isLike("%m%"))
            .build().execute();
    */

    //多条件查询or
    /*
    List<User> userList = userMapper.selectByExample()
            .where(sex, isEqualTo("女"))
            .or(username, isLike("%m%"))
            .build().execute();
    */

    //排序:正序
    /*
    List<User> userList = userMapper.selectByExample()
            .where(username, isLike("%m%"))
            .orderBy(id)
            .build().execute();
    */

    //排序:逆序
    /*
    List<User> userList = userMapper.selectByExample()
            .where(username, isLike("%m%"))
            .orderBy(id.descending())
            .build().execute();
    */

    //排序:多字段
    /*
    List<User> userList = userMapper.selectByExample()
            .where(username, isLike("%m%"))
            .orderBy(sex.descending(),username)
            .build().execute();
    */

    //统计
    /*
    Long count = userMapper.countByExample()
            .build().execute();
    */
}

注意事项

Mapper文件内容不覆盖而是追加

针对oracle数据库的 Table schema问题

上一篇 下一篇

猜你喜欢

热点阅读