通用Mapper的使用

本文介绍MyBatis通用Mapper插件的使用方法,包括依赖导入、配置、自定义接口继承、实体类注解及各种通用方法的详细应用,如selectOne、select、insertSelective等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自[link]https://blog.youkuaiyun.com/jinYwuM/article/details/80535538)

1、导入依赖


 
  1. <dependency>
  2. <groupId>com.github.abel533</groupId>
  3. <artifactId>mapper</artifactId>
  4. <version> 2.3.4</version>
  5. </dependency>

2、配置plugins(Mapper拦截器)

Mybatis-config.xml的plugins下新增plugin配置,配置通用Mapper的接口。


 
  1. <plugins>
  2. <!-- 分页插件 -->
  3. <plugin interceptor= "com.github.pagehelper.PageHelper">
  4. <property name= "dialect" value= "mysql" />
  5. </plugin>
  6. <plugin interceptor= "com.github.abel533.mapperhelper.MapperInterceptor">
  7. <!--通用Mapper接口 -->
  8. <property name= "mappers" value= "com.github.abel533.mapper.Mapper" />
  9. </plugin>
  10. </plugins>

注意:通用Mapper的插件必须配置在分页插件下

 

3、自定义接口继承通用mapper的接口


 
  1. public interface NewMapper extends Mapper<User>{
  2. }

注意:继承通用Mapper<T>的接口,必须指定泛型<T>

 

4、泛型实体类<T>的属性的注解

实体类按照如下规则和数据库表进行转换,注解全部是JPA中的注解:

  1. 表名默认使用类名,驼峰转下划线,如UserInfo默认对应的表名为user_info.

  2. 表名可以使用@Table(name = "tableName")进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.

  3. 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.

  4. 可以使用@Column(name = "fieldName")指定不符合第3条规则的字段名

  5. 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.

  6. 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.

  7. 默认情况下,实体类中如果不存在包含@Id注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).

  8. 实体类可以继承使用,可以参考测试代码中的com.github.abel533.model.UserLogin2类.

  9. 由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.

除了上面提到的这些,Mapper还提供了序列(支持Oracle)、UUID(任意数据库,字段长度32)、主键自增(类似Mysql,Hsqldb)三种方式,其中序列和UUID可以配置多个,主键自增只能配置一个。

这三种方式不能同时使用,同时存在时按照 序列>UUID>主键自增的优先级进行选择.下面是具体配置方法:

  1. 使用序列可以添加如下的注解:

    <pre class="has" name="code" onclick="hljs.copyCode(event)"><code class="language-html hljs xml"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">//可以用于数字类型,字符串类型(需数据库支持自动转型)的字段</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">@SequenceGenerator(name="Any",sequenceName="seq_userid")</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">@Id</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">private Integer id;</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
    </li>
    <li>
    <p>使用UUID时:</p>
    
    <pre class="has" name="code" onclick="hljs.copyCode(event)"><code class="language-html hljs xml"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">//可以用于任意字符串类型长度超过32位的字段</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">@GeneratedValue(generator = "UUID")</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">private String countryname;</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
    </li>
    <li>
    <p>使用主键自增:</p>
    
    <pre class="has" name="code" onclick="hljs.copyCode(event)"><code class="language-html hljs xml"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">//不限于@Id注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">@Id</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">@GeneratedValue(strategy = GenerationType.IDENTITY)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">private Integer id;</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
    </li>
    

为了可以回写主键的值,使用注解:@GeneratedValue(generator ="JDBC"

为了可以使用通用mapper中通过主键查询的方法:一定要配置主键@Id

 

5、通用方法的使用

    1、selectOne():查询单个对象,返回值为实体类对象


 
  1. public void testSelectOne() {
  2. User user = new User();
  3. // 设置一个唯一的属性用来查询
  4. user.setId( 2L);
  5. User selectOne = newMapper.selectOne(user);
  6. System.out.println(selectOne);
  7. }

注意:

        a、初始化的user对象中封装的查询条件必须是唯一的,否则查询会报ToManyResultsException异常。

        b、若实体类中的属性与表对应不了,也会报错,可以使用注解@Transient忽略字段


    2、select():查询多个对象,返回值为List<T>


 
  1.     /**
  2. * 条件为null或者是未初始化的实体类对象,会查询所有数据
  3. * user中设置了数据:就按user中封装的数据进行条件查询
  4. * 条件只能按照=来查询,不能按照>或者<等方式来查询
  5. */
  6. public void testSelect() {
  7. User user = new User();
  8. List<User> select = newMapper.select(user);
  9. for (User user2 : select) {
  10. System.out.println(user2);
  11. }
  12. }

3、selectCount():统计查询,返回值为int类型


 
  1. /**
  2. * 统计查询:
  3. * 1、赋值为null或未初始化的实体类对象:统计所有记录数
  4. * 2、若设置属性,则按照属性查询符合的记录数
  5. */
  6. public void testSelectCount() {
  7. User user = new User();
  8. user.setAge( 20);
  9. int selectCount = newMapper.selectCount(user);
  10. System.out.println(selectCount);
  11. }

 

4、selectByPrimaryKey():按照主键查询单个,返回返回值为实体类对象


 
  1. /**
  2. * 通过主键进行查询:
  3. * 1、参数类型一定要主键一致
  4. * 2、必须在对象中通过@Id指定主键
  5. * 如果满足前两点,但没有查询到,返回null,若不满足前两点,报ClassCastException
  6. */
  7. public void testSelectByPrimaryKey() {
  8. User user = newMapper.selectByPrimaryKey( 2l);
  9. System.out.println(user);
  10. }

5、insert():返回值为int类型


 
  1.        /**
  2. * 新增方法:按全属性插入数据,如果不设置属性,自动设置为null
  3. */
  4. public void testInsert() {
  5. }

6、insertSelective()(推荐使用):返回值为int类型


 
  1. /**
  2. * 按属性插入数据:如果不设置属性,自动设置为null
  3. */
  4. public void testInsertSelective() {
  5. User user = new User();
  6. user.setUserName( "huahu");
  7. user.setPassword( "123456");
  8. user.setName( "花花");
  9. newMapper.insertSelective(user);
  10. }

注意:虽然从结果来说,insert()与insertSelective()执行效果都是一样的,但是从效率上来说insertSelective()更高。前者按照全属性插入,后者按照给定属性插入, 不设置属性,则自动设置为null。

 

7、delete():返回值为int类型


 
  1.          // 条件删除:如果条件为null或未初始化的实体类对象,删除全部数据
  2. public void testDelete() {
  3. User user = new User();
  4. user.setUserName( "admin1");
  5. newMapper.delete(user);
  6. }

注意:如果有外键约束,并且主键被某个外键所引用,无法删除。

8、deleteByPrimaryKey():返回值为int类型


 
  1. // 按主键删除
  2. public void testDeleteByPrimaryKey() {
  3. newMapper.deleteByPrimaryKey( 18L);
  4. }

9、updateByPrimaryKey()(不推荐):返回值为int类型


 
  1. /**
  2. * 通过主键更新:
  3. * 将设置的属性进行更新,如果不设置属性,自动更新为null。
  4. * 必须设置主键
  5. */
  6. public void testUpdateByPrimaryKey() {
  7. }

10、updateByPrimaryKeySelective():返回值为int类型


 
  1. /**
  2. * 通过主键按属性进行选择更新:
  3. * 必须设置主键
  4. * 将设置的属性进行更新,没有设置的属性不更新
  5. */
  6. public void testUpdateByPrimaryKeySelective() {
  7. User user = new User();
  8. user.setId( 1L);
  9. user.setUserName( "admin");
  10. user.setPassword( "123");
  11. newMapper.updateByPrimaryKeySelective(user);
  12. }

11、selectCountByExample():返回值为int类型


 
  1. /**
  2. * 通过条件统计记录数
  3. * 1、需要初始化Example对象
  4. * 2、通过example获取criteria来设置条件
  5. */
  6. public void testSelectCountByExample() {
  7. Example example = new Example(User.class);
  8. Criteria criteria = example.createCriteria();
  9. criteria.andBetween( "age", 15, 30);
  10. int count = newMapper.selectCountByExample(example);
  11. System.out.println(count);
  12. }

12、selectByExample():返回值类型为List<T>

通过Example对象设置查询条件的步骤:

  1. 初始化Example对象
  2. 通过Example对象获取多个Criteria对象来设置查询条件
  3. 通过example设置并集查询or
  4. 通过example设置排序查询

 
  1.          /**
  2. * 通过条件进行查询:
  3. * 查询年龄在15-30之间的并且用户名中有zhang的用户,或者密码是123456的用户。
  4. * 显示的时候通过age倒序排序,如果age一样那么根据id正序执行。
  5. */
  6. public void testSelectByExample() {
  7. Example example = new Example(User.class);
  8. Criteria criteria = example.createCriteria();
  9. criteria.andBetween( "age", 15, 30);
  10. criteria.andLike( "userName", "%zhang%");
  11. //添加or的条件
  12. Criteria criteria2 = example.createCriteria();
  13. criteria2.andEqualTo( "password", "123456");
  14. example.or(criteria2);
  15. //按年龄倒序排序,如果年龄相同,按id正序排序
  16. example.setOrderByClause( "age desc,id asc");
  17. List<User> list = newMapper.selectByExample(example);
  18. for (User user : list) {
  19. System.out.println(user);
  20. }
  21. }

13、deleteByExample():返回值为int类型


 
  1. // 条件查询
  2. @Test
  3. public void testDeleteByExample() {
  4. Example example = new Example(User.class);
  5. Criteria criteria = example.createCriteria();
  6. criteria.andLike( "userName", "admin");
  7. newUserMapper.deleteByExample(example);
  8. }

14、updateByExampleSelective():返回值为int类型


 
  1. /**
  2. * 按条件选择更新:没设置的属性就不做修改
  3. * 将年龄>22的用户的密码修改成123
  4. */
  5. public void testUpdateByExampleSelective() {
  6. Example example = new Example(User.class);
  7. Criteria criteria = example.createCriteria();
  8. criteria.andGreaterThan( "age", 22);
  9. User user = new User();
  10. user.setPassword( "123");
  11. newMapper.updateByExampleSelective(user, example);
  12. }

15、updateByExample():返回值为int类型


 
  1. /**
  2. * 将年龄大于22的用的密码修改为321
  3. * 条件更新:没有设置属性的都更新为null.
  4. */
  5. public void testUpdateByExample() {
  6. Example example = new Example(User.class);
  7. Criteria criteria = example.createCriteria();
  8. criteria.andGreaterThan( "age", 22);
  9. User user = new User();
  10. user.setPassword( "321");
  11. newMapper.updateByExample(user, example);
  12. }

附上Criteria类中设置条件的方法

 

 










 




 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值