zoukankan      html  css  js  c++  java
  • spring Boot学习笔记(通用Mapper的使用)

    使用Mybatis框架编写持久层代码时,有时我们只需要单表的操作,这样就会产生一些机械重复的工作,而通用Mapper的出现帮我们解决了这个问题。
    我使用的通用Mapper是abel533写的Github这个框架提供了极其方便的MyBatis单表的增删改查,可以让我们不必在单表操作的编写上浪费多少时间。
    通用Mapper的使用步骤
    1.要使用通用Mapper需要先导入依赖

    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.3</version>
    </dependency>
    

    2.编写配置文件
    在resource目录下新建application.properties文件

    #服务器相关配置
    server.port=80
    server.servlet.context-path=/learn
    #数据库连接信息
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/数据库名
    spring.datasource.username=root
    spring.datasource.password=****
    

    3.编写启动类

    package com.learn;
    //声明该注解是springboot的引导类
    @SpringBootApplication
    @MapperScan("com.learn.mapper")//注册通用mapper
    public class MySpringBootApplication {
    
        //main方法是程序的入口
        public static void main(String[] args) {
            //run方法,表示运行SpringBoot的引导类,参数就是SpringBoot的引导类字节码对象
            SpringApplication.run(MySpringBootApplication.class);
    
        }
    }
    
    

    4.编写实体类

    package com.learn.domain;
    /**
     * User实体类
     */
    //告知通用Mapper要对哪张表进行操作
    @Table(name = "user")
    public class User implements Serializable {
        //告知通用Mapper此属性对应表中的主键
        @Id
        //告知通用Mapper此属性是自增长的
        @KeySql(useGeneratedKeys = true)
        private Integer id;
        private String username;
        private Date birthday;
        private String sex;
        private String address;
        //忽略此属性,不作为查询条件
        @Transient
        private String test;
    }
    

    5.新建UserMapper

    package com.learn.mapper;
    public interface UserMapper extends Mapper<User> {
        //Mapper只需要继承tk.mybatis.mapper.common.Mapper<T>这个接口,通用Mapper就会自动帮我们生成Mybatis的单表的增删改查。
    }
    

    到这里通用Mapper就编写完了,是不是特别方便快捷,下面我们来测试一下这些方法。
    5.编写测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MySpringBootApplication.class)
    public class UserMapper2Test {
    
        @Autowired
        private UserMapper2 userMapper2;
    
        //因为我懒,这里只写了一个根据主键查询,和一个根据User对象中的非空属性查询
        @Test
        public void TestMethod(){
            int id = 41;
            String username = "老王";
            User user = userMapper2.selectByPrimaryKey(id);
            User user2 = new User();
            user2.setUsername(username);
            //根据User的非空属性查询
            User user1 = userMapper2.selectOne(user2);
            System.out.println(user1);
            System.out.println(user);
        }
    }
    

    我自己还编写了service来测试事务,事务的使用方式和之前一样,只需要再方法上加@Transactional注解即可。

  • 相关阅读:
    Service Name Port Number Transport Protocol tcp udp 端口号16bit
    linux linux 互传文件 win 不通过 ftp sftp 往linux 传文件(文件夹)
    soft deletion Google SRE 保障数据完整性的手段
    Taylor series
    Taylor's theorem
    Moving average
    REQUEST
    Unix file types
    mysqld.sock
    Tunneling protocol
  • 原文地址:https://www.cnblogs.com/lazy-brain/p/12774237.html
Copyright © 2011-2022 走看看