通用mapper的使用

1.在pom中添加Maven依赖

<!-- 通用Mapper -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper</artifactId>
    <version>3.3.9</version>
</dependency>

2.SpringMVC配置

 <!-- spring与mybatis整合配置,扫描所有dao -->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 具体dao层包的位置在哪,视你新建的dao层java包而定 -->
        <property name="basePackage" value="com.gyt.persist.dao" />
        <!-- 通用 Mapper -->
        <property name="properties">
            <value>
                mappers=tk.mybatis.mapper.common.Mapper
            </value>
        </property>
    </bean>

*注意这里使用tk.mybatis.spring.mapper.MapperScannerConfigure替换原来Mybatis的org.mybatis.spring.mapper.MapperScannerConfigurer。

 

3.实体类的写法

@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private String id;

    @Column(name="name")
    private String name;

    @Column(name="sex")
    private String sex;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    
}

*说明:

 

1.表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info。

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

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

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

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

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

7.如果是MySQL的自增字段,加上@GeneratedValue(generator = "JDBC")即可。如果是其他数据库,可以参考官网文档。

 

4.Mapper接口的写法(DAO的写法)

public interface UserDao extends Mapper<User> {

}

1.在传统的Mybatis写法中,DAO接口需要与Mapper文件关联,即需要编写SQL来实现DAO接口中的方法。而在通用Mapper中,DAO只需要继承一个通用接口,即可拥有丰富的方法:

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

 

2.一旦继承了Mapper,继承的Mapper就拥有了Mapper所有的通用方法:

 

Select

方法:List<T> select(T record);

说明:根据实体中的属性值进行查询,查询条件使用等号

 

方法:T selectByPrimaryKey(Object key);

说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

 

方法:List<T> selectAll();

说明:查询全部结果,select(null)方法能达到同样的效果

 

方法:T selectOne(T record);

说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

 

方法:int selectCount(T record);

说明:根据实体中的属性查询总数,查询条件使用等号

 

Insert

方法:int insert(T record);

说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

 

方法:int insertSelective(T record);

说明:保存一个实体,null的属性不会保存,会使用数据库默认值

 

Update

方法:int updateByPrimaryKey(T record);

说明:根据主键更新实体全部字段,null值会被更新

 

方法:int updateByPrimaryKeySelective(T record);

说明:根据主键更新属性不为null的值

 

Delete

方法:int delete(T record);

说明:根据实体属性作为条件进行删除,查询条件使用等号

 

方法:int deleteByPrimaryKey(Object key);

说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

 

Example方法

方法:List<T> selectByExample(Object example);

说明:根据Example条件进行查询

重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

 

方法:int selectCountByExample(Object example);

说明:根据Example条件进行查询总数

 

方法:int updateByExample(@Param("record") T record, @Param("example") Object example);

说明:根据Example条件更新实体record包含的全部属性,null值会被更新

 

方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);

说明:根据Example条件更新实体record包含的不是null的属性值

 

方法:int deleteByExample(Object example);

说明:根据Example条件删除数据

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatis通用Mapper是一个基于Mybatis框架的插件,它可以帮助我们快速地进行数据库操作,避免了手写SQL的繁琐和容易出错的问题。使用Mybatis通用Mapper,我们只需要定义好实体类和Mapper接口,就可以直接调用通用的增删改查方法,非常方便。 具体使用方法如下: 1. 引入Mybatis通用Mapper的依赖包,可以通过Maven或者Gradle进行引入。 2. 定义实体类,需要注意的是实体类的属性名要和数据库表的字段名一致,或者使用@ColumnName注解进行映射。 3. 定义Mapper接口,继承通用Mapper接口,例如: public interface UserMapper extends Mapper<User> {} 4. 在Mybatis的配置文件中配置通用Mapper插件,例如: <plugins> <plugin interceptor="tk.mybatis.mapper.plugin.MapperInterceptor"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> </plugin> </plugins> 5. 在代码中调用通用Mapper的方法,例如: @Autowired private UserMapper userMapper; public void addUser(User user) { userMapper.insert(user); } 以上就是Mybatis通用Mapper使用方法,它可以大大简化我们的开发工作,提高开发效率。 ### 回答2: Mybatis通用Mapper是一个基于Mybatis的通用Mapper插件。它提供了单表的增删改查操作,同时还支持通用的批量操作和条件查询等功能。在使用Mybatis通用Mapper时,我们不需要编写繁琐的Mapper接口和对应的XML文件,只需要使用注解即可完成对数据库的操作。 首先,我们需要在pom.xml文件中添加Mybatis通用Mapper的依赖: ``` <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.5</version> </dependency> ``` 接下来,我们需要在Mybatis的配置文件中添加通用Mapper的配置: ``` <plugins> <plugin interceptor="tk.mybatis.mapper.common.MapperInterceptor"> <property name="mappers" value="tk.mybatis.mapper.common.BaseMapper"/> </plugin> </plugins> ``` 完成以上操作后,我们就可以在项目中使用Mybatis通用Mapper了。例如,我们要对一个用户表进行操作,可以创建一个User实体类: ``` public class User { private Integer id; private String username; private String password; private String email; // 省略getter和setter方法 } ``` 然后,我们通过注解在UserMapper接口中定义对用户表的操作: ``` public interface UserMapper extends Mapper<User> { } ``` 这里的Mapper是Mybatis通用Mapper提供的一个接口,通过继承该接口,我们可以直接使用其中定义好的单表操作方法。 接下来,我们就可以直接在代码中使用UserMapper了。例如,我们要插入一条用户记录,可以使用以下代码: ``` User user = new User(); user.setUsername("test"); user.setPassword("123456"); user.setEmail("test@example.com"); userMapper.insert(user); ``` 如果我们需要查询一条用户记录,可以使用以下代码: ``` User user = new User(); user.setId(1); User result = userMapper.selectOne(user); ``` 以上就是使用Mybatis通用Mapper的简单示例。在实际使用中,我们还可以通过注解实现复杂的批量操作和条件查询等功能,具体请参考Mybatis通用Mapper的官方文档。 ### 回答3: Mybatis通用mapper是基于Mybatis的一个工具,简化了Mybatis中XML配置的工作量,提供了一些通用的方法,能够简化我们的增删改查操作。下面简单介绍一下Mybatis通用mapper使用方法。 一、引入依赖和配置 在pom文件中引入mybatis和通用mapper的依赖 ``` <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mybatis-mapper.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> ``` 在application.yml中进行相关配置 ``` mybatis: #配置别名 type-aliases-package: com.xxxx.entity #配置xml映射文件路径 mapper-locations: classpath:mapper/*.xml mapper: #配置通用mapper的包路径 mappers: tk.mybatis.mapper.common.Mapper #配置实体类主键的生成策略 identity: mysql #配置mysql的方言,不配置通用mapper的分页插件会出错 dialect: mysql ``` 二、编写实体类和Mapper接口 在编写实体类时,需要给实体类的属性加上@TableId和@Column注解,用来指明主键和列名。例如: ``` @Data @Table(name = "user") public class User { @Id @GeneratedValue(generator = "JDBC") @Column(name = "id") private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; @Column(name = "age") private Integer age; @Column(name = "email") private String email; } ``` 在编写Mapper接口时,直接继承Mapper<T>接口即可,T为对应的实体类。例如: ``` @Repository public interface UserMapper extends Mapper<User> { } ``` 三、编写通用方法 Mybatis通用mapper提供了一些通用方法,例如插入数据、更新数据、删除数据和查询数据等。这些方法都可以直接调用或者根据需要进行封装。 (1)插入数据 使用通用mapper的插入方法,代码如下: ``` User user = new User(); user.setUsername("testUser"); user.setPassword("123456"); user.setAge(20); user.setEmail("testUser@xx.com"); userMapper.insert(user); ``` 使用通用mapper的批量插入方法,代码如下: ``` List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setUsername("testUser1"); user1.setPassword("123456"); user1.setAge(20); user1.setEmail("testUser@xx.com"); userList.add(user1); User user2 = new User(); user2.setUsername("testUser2"); user2.setPassword("123456"); user2.setAge(21); user2.setEmail("testUser@xx.com"); userList.add(user2); userMapper.insertList(userList); ``` (2)更新数据 使用通用mapper的更新方法,代码如下: ``` User user = new User(); user.setId(1L); user.setUsername("newTestUser"); userMapper.updateByPrimaryKey(user); ``` (3)删除数据 使用通用mapper的删除方法,代码如下: ``` userMapper.deleteByPrimaryKey(1L); ``` (4)查询数据 使用通用mapper的查询方法,代码如下: ``` User user = userMapper.selectByPrimaryKey(1L); ``` 使用通用mapper的查询所有数据方法,代码如下: ``` List<User> userList = userMapper.selectAll(); ``` 使用通用mapper的条件查询方法,代码如下: ``` Condition condition = new Condition(User.class); condition.createCriteria().andEqualTo("username", "testUser"); List<User> userList = userMapper.selectByExample(condition); ``` 以上就是使用Mybatis通用mapper的一些常用方法,它们都能够简化我们的开发工作。当然,如果有需要,我们还可以自己封装一些方法来实现更加灵活和高效的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值