SpringBoot:集成通用Mapper

一:通用Mapper简介

  • 通用Mapper就像是一个mybatis的辅助工具,能很好地帮我我们进行对数据库的操作,简化操作,并且通用Mapper中提供了很多通用的方法供我们选择.

二:项目构建

  • 导包
    访问仓库地址查询版本:https://mvnrepository.com/
  <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>选择合适的版本version>
   dependency>
  • 实体类
@Entity
@Table(name = "person")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
}
  • mapper类
public interface PersonMapper extends Mapper<Person> {
}

  • 配置
spring:
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo
  //自动建表
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
@SpringBootApplication
@MapperScan(value = "com.lll.mybatis.mapper")
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

}
  • 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestControllerTest {

    @Autowired
    private PersonMapper personMapper;
    
    @Test
    public void test() {
        personMapper.insert(new Person(null,"茉莉"));
    }
}

三:通用Mapper的其他方法

  • 通用Mapper文档:https://mapperhelper.github.io/docs/

你可能感兴趣的:(Spring,Boot)