美文网首页
通用Mapper

通用Mapper

作者: 爪蛙程序猿 | 来源:发表于2020-04-26 22:49 被阅读0次

    1.引入坐标

    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper</artifactId>
        <version>3.1.2</version>
    </dependency> 
    

    2.sqlMapConfig.xml中配置插件类

    <plugins>
        <plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperInterceptor">
            <!--通用Mapper接口,多个通用接口用逗号隔开-->
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
        </plugin>
    </plugins> 
    

    3.使用注解配置实体类

    @Table(name = "t_user")//表名
        public class User {
        @Id//主键
        @GeneratedValue(strategy = GenerationType.IDENTITY)//主键生成策略
        private Integer id;
        private String username;
    } 
    

    4.定义通用mapper接口

    public interface UserMapper extends Mapper<User> {
    } 
    

    5.使用

    //select
    
    User user = userMapper.selectOne(user);  //只返回一条
    
    List<User> users = userMapper.select(null); //查询全部
    
    //insert
    
    int insert = userMapper.insert(user); //null值会保存
    
    int i = userMapper.insertSelective(user); //null值不会保存
    

    相关文章

      网友评论

          本文标题:通用Mapper

          本文链接:https://www.haomeiwen.com/subject/bubswhtx.html