zoukankan      html  css  js  c++  java
  • 通用Mapper

    原理是:拦截器

    1、假设:使用MyBatis只需要定义Mapper接口,无需编写Mapper.xml文件

      如果实现无需编写Mapper.xml文件,我们必须要实现动态拼接SQL

    如何实现动态拼接SQL语句?

    思路:编写Mybatis的插件,在执行过程中动态生成SQL语句

    2、简介:

      Mybatis通用 Mapper极其方便的使用Mybatis单表的增删改查,支持单表操作,不支持通用的多表联合查询。

    3、在Mybatis的配置文件中进行配置

      3.1 分页助手也是拦截器技术,所以注意顺序,分页助手配置在通用mapper的前面

    <?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>
    
        <plugins>
            <!-- 配置分页助手 -->
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <property name="dialect" value="mysql" />
                <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
                <property name="rowBoundsWithCount" value="true" />
            </plugin>
            <!-- 配置通用Mapper -->
            <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
                <!--主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
                <property name="IDENTITY" value="HSQLDB" />
                <!--通用Mapper接口,多个通用接口用逗号隔开 -->
                <property name="mappers" value="com.github.abel533.mapper.Mapper" />
            </plugin>
    
        </plugins>
    
    </configuration>

      3.2  mapper继承Mapper<?>

    public interface NewUserMapper extends Mapper<User>{
        
    }

      3.3 给实体对象 User进行注解设置

    @Table(name="tb_user")
    public class User {
        
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        // 用户名
        private String userName;
    
        // 密码
        private String password;
    
        // 姓名
        private String name;
    
        // 年龄
        private Integer age;
    
        // 性别,1男性,2女性
        private Integer sex;
    
        // 出生日期
        private Date birthday;

     3.3 service 使用

    @Service
    public class NewUserService {
        
        @Autowired
        private NewUserMapper newUsermapper;
    
        public EasyUIResult queryUserList(Integer page, Integer rows) {
            //设置分页参数
            PageHelper.startPage(page,rows);
            //設置查詢條件
            Example example=new Example(User.class);
            example.setOrderByClause("created DESC");
            List<User> users = this.newUsermapper.selectByExample(example);
            PageInfo<User> pageinfo=new PageInfo<User>(users);
            return new EasyUIResult(pageinfo.getTotal(),pageinfo.getList());
            
        }
        
    }
  • 相关阅读:
    微信输入文字和更多切换时不改变下面layout大小
    bitmap1 = bitmap2导致bitmap1不能使用一致报回收错误解决
    Mac下命令行打开Sublime
    android.content.res.Resources$NotFoundException:String resource ID #0x86
    SEGGER RTT STOP/SLEEP 模式下使用
    STM32中用 stop 模式 配合低功耗模式下的自动唤醒(AWU) 能否实现FreeRTOS tickless 模式
    NRF52832 能烧写代码 但是不运行 ,是因为没有烧写协议栈
    NRF52832 Logger module 设置
    jlink RTT 打印 BUG , FreeRTOS 在开启 tickless 模式下 无法使用的问题
    gattAttribute_t 含义 中文解释
  • 原文地址:https://www.cnblogs.com/axu521/p/10229318.html
Copyright © 2011-2022 走看看