Mybatis通用Mapper

极其方便的使用Mybatis单表的增删改查

1. 引入通用Mapper的代码

项目依赖于JPA的注解,需要引入persistence-api-1.0.jar或者添加Maven依赖:

<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>persistence-api</artifactId>
  <version>1.0</version>
</dependency>


  1. <!-- 通用Mapper -->  
  2.         <dependency>  
  3.             <groupId>com.github.abel533</groupId>  
  4.             <artifactId>mapper</artifactId>  
  5.             <version>2.3.4</version>  
  6.        </dependency> 

在mybatis中的配置文件中添加

  1. <configuration>  
  2.   
  3.     <!--Mybatis的拦截器 -->  
  4.     <plugins>  
  5.         <!--Mybatis分页助手 -->  
  6.         <plugin interceptor="com.github.pagehelper.PageHelper">  
  7.             <property name="dialect" value="mysql" />  
  8.             <!-- 该参数默认为false -->  
  9.             <!-- 设置为true时,使用RowBounds分页会进行count查询,也就是是否查询数据总条数 -->  
  10.             <property name="rowBoundsWithCount" value="true" />  
  11.         </plugin>  
  12.         <!--Mybatis通用Mapper -->  
  13.         <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">  
  14.             <!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->  
  15.             <property name="IDENTITY" value="MYSQL" />  
  16.             <!-- 通用Mapper接口,多个通用接口用逗号隔开 -->  
  17.             <property name="mappers" value="com.github.abel533.mapper.Mapper" />  
  18.         </plugin>  
  19.     </plugins>  
  20. </configuration> 


或者在spring的mybatis配置的地方添加

<!-- Mybatis分页插件-pagehelper -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=sqlserver
                            reasonable=true
                        </value>
                    </property>
                </bean>
                <!--Mybatis通用Mapper -->  
                <bean class="com.github.abel533.mapperhelper.MapperInterceptor">  
                    <property name="properties">
                        <value>
                            IDENTITY=sqlserver
                            mappers=com.github.abel533.mapper.Mapper
                        </value>
                    </property>
                    
                    <!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->  
                    <!-- <property name="IDENTITY" value="sqlserver" /> -->  
                    <!-- 通用Mapper接口,多个通用接口用逗号隔开 -->  
                    <!-- <property name="mappers" value="com.github.abel533.mapper.Mapper" /> -->  
                </bean>  
            </array>
        </property>

dao层:


service层:


controller层:



一旦继承了Mapper<T>,继承的Mapper就拥有了基本的通用的方法:

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. 使用序列可以添加如下的注解:

    //可以用于数字类型,字符串类型(需数据库支持自动转型)的字段
    @SequenceGenerator(name="Any",sequenceName="seq_userid")
    @Id
    private Integer id;
    
  2. 使用UUID时:

    //可以用于任意字符串类型长度超过32位的字段
    @GeneratedValue(generator = "UUID")
    private String countryname;
    
  3. 使用主键自增:

    //不限于@Id注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    

5. 将继承的Mapper接口添加到Mybatis配置中

测试代码:
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import org.junit.Before;  
  5. import org.junit.Test;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import com.github.abel533.entity.Example;  
  10.   
  11. import cn.itcast.user.mapper.NewUserMapper;  
  12. import cn.itcast.user.pojo.User;  
  13.   
  14. public class NewUserMapperTest {  
  15.   
  16.     private NewUserMapper newUserMapper;  
  17.   
  18.     @Before  
  19.     public void setUp() throws Exception {  
  20.         // 完成newUserMapper的初始化  
  21.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  22.                 "spring/app*.xml");  
  23.         newUserMapper = context.getBean("newUserMapper", NewUserMapper.class);  
  24.     }  
  25.   
  26.     @Test  
  27.     public void testSelectOne() {  
  28.         User user = new User();  
  29.         user.setId(1L);  
  30.         user.setAge(30);  
  31.         User selectOne = newUserMapper.selectOne(user);  
  32.         System.out.println(selectOne);  
  33.     }  
  34.   
  35.     @Test  
  36.     public void testSelect() {  
  37.   
  38.         List<User> lists = newUserMapper.select(null);  
  39.         for (User user : lists) {  
  40.             System.out.println(user);  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     @Test  
  46.     public void testSelectCount() {  
  47.         System.out.println(newUserMapper.selectCount(null));  
  48.     }  
  49.   
  50.     @Test  
  51.     public void testSelectByPrimaryKey() {  
  52.         System.out.println(newUserMapper.selectByPrimaryKey(1L));  
  53.     }  
  54.   
  55.     @Test  
  56.     public void testInsert() {  
  57.     }  
  58.   
  59.     @Test  
  60.     public void testInsertSelective() {  
  61.     }  
  62.   
  63.     @Test  
  64.     public void testDelete() {  
  65.     }  
  66.   
  67.     @Test  
  68.     public void testDeleteByPrimaryKey() {  
  69.     }  
  70.   
  71.     @Test  
  72.     public void testUpdateByPrimaryKey() {  
  73.     }  
  74.   
  75.     @Test  
  76.     public void testUpdateByPrimaryKeySelective() {  
  77.     }  
  78.   
  79.     @Test  
  80.     public void testSelectCountByExample() {  
  81.         // 根据多个id查询用户信息  
  82.         List<Object> ids = new ArrayList<Object>();  
  83.         ids.add(1);  
  84.         ids.add(2);  
  85.         ids.add(3);  
  86.         Example example = new Example(User.class);  
  87.         example.createCriteria().andIn("id", ids);  
  88.         List<User> list = this.newUserMapper.selectByExample(example);  
  89.         for (User user : list) {  
  90.             System.out.println(user);  
  91.         }  
  92.   
  93.     }  
  94.   
  95.     @Test  
  96.     public void testDeleteByExample() {  
  97.         Example example = new Example(User.class);  
  98.         example.createCriteria().andBetween("age"8090);  
  99.         newUserMapper.deleteByExample(example);  
  100.     }  
  101.   
  102.     @Test  
  103.     public void testSelectByExample() {  
  104.     }  
  105.   
  106.     @Test  
  107.     public void testUpdateByExampleSelective() {  
  108.     }  
  109.   
  110.     @Test  
  111.     public void testUpdateByExample() {  
  112.     }  
  113.   
  114. }  

8、集成到项目中

      






猜你喜欢

转载自blog.csdn.net/qi923701/article/details/78973656