springMvc 通用mapper的使用

通用mapper的可以方便实现单表的操作,下面是具体的使用情况:

     1:导入依赖
重要提示:3.1.0及以后版本的groupId修改为tk.mybatis,artifactId为mapper

    tk.mybatis
    mapper
    3.3.9

3.1.0之前的使用的是:

    com.github.abel533
    mapper
    2.3.4



      2:在mybatis的全局配置文件中,注册通用mapper插件,MapperInterceptor _(plugin),详情,点这里,如何集成通用Mapper
 
   
   
   
   

 

    3:如何使用通用mapper? 

  继承通用的Mapper,必须指定泛型

例如下面的例子:

public interface UserInfoMapper extends Mapper<UserInfo> {
  //其他必须手写的接口...

}

一旦继承了Mapper,继承的Mapper就拥有了以下通用的方法:

//根据实体类不为null的字段进行查询,条件全部使用=号and条件
List<T> select(T record);

//根据实体类不为null的字段查询总数,条件全部使用=号and条件
int selectCount(T record);

//根据主键进行查询,必须保证结果唯一
//单个字段做主键时,可以直接写主键的值
//联合主键时,key可以是实体类,也可以是Map
T selectByPrimaryKey(Object key);

//插入一条数据
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
int insert(T record);

//插入一条数据,只插入不为null的字段,不会影响有默认值的字段
//支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
//优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
int insertSelective(T record);

//根据实体类中字段不为null的条件进行删除,条件全部使用=号and条件
int delete(T key);

//通过主键进行删除,这里最多只会删除一条数据
//单个字段做主键时,可以直接写主键的值
//联合主键时,key可以是实体类,也可以是Map
int deleteByPrimaryKey(Object key);

//根据主键进行更新,这里最多只会更新一条数据
//参数为实体类
int updateByPrimaryKey(T record);

//根据主键进行更新
//只会更新不是null的数据
int updateByPrimaryKeySelective(T record);

4. 泛型(实体类)的类型必须符合要求

实体类按照如下规则和数据库表进行转换,注解全部是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,而且无法消除,所以实体类中建议不要使用基本类型

     使用主键自增(自增主键的回显):

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

详情代码及测试用例:

public interface NewUserMapper extends Mapper<User>{
  //其他必须手写的接口...
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@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;
    private Integer sex;
    private Date birthday;
    private Date created;
    private Date updated;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

测试:

Public class NewUserMapperTest{
    private NewUserMapper newUserMapper;
    @Before
    public void setUp() throws Exception {
        ApplicationContext  applicationContext=new 
                                ClasspathXMLApplication("classpath:spring/applicationContext*.xml");
        this.newUserMapper = applicationContext.getBean(NewUserMapper.class);
    }

    @Test
    public void testSelectOne(){
        User record=new User();
        //设置查询条件
        record.setName("Zhangsan");
        User user=this.newUserMapper.selectOne(record);
        System.out.println(user);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

结果: 
这里写图片描述

    @Test
    public void testSelect(){   
        List users=this.newUserMapper.select(null);
        for(User user:users){
        System.out.println(user);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以下是稍微复杂的查询测试 Example 

//List selectByExample(Object example);
@Test
public void testSelectByExample(){
    Example example=new Example(User.class);
    List values=new Arraylist();
    values.add(1L);
    values.add(2L);
    values.add(3L);
    example.creatCriteria().addIn("id",values);
    List users=this.newUserMapper.selectByExample(example);
    for(User user:users){
        System.out.println(user);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

拼接条件为:

这里写图片描述

UserService中使用NewUserMapper按照时间desc排序: 
springMvc 通用mapper的使用_第1张图片

自动拼接的sql语句,只看条件为:

FROM tb_user order by create DESC limit ?,?
  • 1
  • 1









你可能感兴趣的:(spring,mvc,mapper,ssh)