springBoot使用通用mapper

1、添加依赖

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

2、配置bean

package com.canteen.adminService.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import  tk.mybatis.spring.annotation.MapperScannerRegistrar;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.util.Properties;

@Configuration
public class AdminConfiguration {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.canteen.adminService.dao");
        Properties properties=new Properties();
        properties.setProperty("mappers","tk.mybatis.mapper.common.Mapper");
        mapperScannerConfigurer.setProperties(properties);
        return  mapperScannerConfigurer;
    }


}

三、配置mapper文件

package com.canteen.adminService.dao;
import com.canteen.common.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface UserMapper extends Mapper<User> {
    public Integer batchDel(@Param("userList") String[] userList);
}

4、应用启动项配置,注意MapperScan包是tk的

package com.canteen.adminService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.canteen.adminService.dao")
public class adminServiceApp {
    public static void main(String[] args) {
        SpringApplication.run(adminServiceApp.class,args);
    }
}

现在就可以开始使用了

Spring Boot可以很方便地整合通用Mapper,只需要在pom.xml中添加通用Mapper的依赖,然后在配置文件中配置数据源和通用Mapper的相关属性即可。 具体步骤如下: 1. 在pom.xml中添加通用Mapper的依赖: ```xml <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> ``` 2. 在配置文件中配置数据源和通用Mapper的相关属性: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true mapper: mappers: - tk.mybatis.mapper.common.Mapper not-empty: false identity: MYSQL ``` 其中,mapper.mappers指定了要使用Mapper接口,这里使用通用MapperMapper接口;mapper.identity指定了主键生成策略,这里使用了MySQL的自增长主键。 3. 在Mapper接口中继承通用MapperMapper接口即可使用通用Mapper提供的方法: ```java public interface UserMapper extends Mapper<User> { } ``` 这样就可以使用通用Mapper提供的方法来进行数据库操作了,例如: ```java @Autowired private UserMapper userMapper; public void addUser(User user) { userMapper.insert(user); } public void updateUser(User user) { userMapper.updateByPrimaryKeySelective(user); } public void deleteUser(Long id) { userMapper.deleteByPrimaryKey(id); } public User getUser(Long id) { return userMapper.selectByPrimaryKey(id); } public List<User> getUsers() { return userMapper.selectAll(); } ``` 以上就是Spring Boot整合通用Mapper的基本步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值