【springboot2--6】数据访问-springboot整合mybatis和mybatis-plus

【springboot2--6】数据访问-springboot整合mybatis和mybatis-plus,第1张

【springboot2--6】数据访问-springboot整合mybatis和mybatis-plus

目录

1.整合mybatis

1.在pom文件中添加start

2.在yml文件中进行配置

3.创建mapper

4.在mapper文件中写sql

5.在service层测试

2.整合mybatis-plus

1、什么是MyBatis-Plus

2.修改pom文件

3.创建mapper

4.创建service层

5.实现功能

6.实现分页功能的插件


1.整合mybatis

        详细 *** 作可以参考mybatis官网https://github.com/mybatis

1.在pom文件中添加start
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        
2.在yml文件中进行配置

在springboot中可用不写mybatis的主配置文件,而是把需要配置的信息在yml文件中进行配置

在configuration:下进行配置,注意不要再写主配置文件的位置

(config-location: classpath:mybatis/mybatis-config.xml),否则会报异常

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: lmz0521
    driver-class-name: com.mysql.cj.jdbc.Driver



# 配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名法
    
#可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可
3.创建mapper

必须在每个mapper文件上表明@mapper注解,或在主程序文件中表明@MapperScan(basePackages = "=mapper")注解,并表明扫描的包路径

@Mapper
public interface AccountMapper {
    Account getAcct(Integer id);
}
4.在mapper文件中写sql




    
    
        select *
        from goods
        where id = #{id}
    
5.在service层测试
@Service
public class AccountService {

    @Autowired()
    AccountMapper accountMapper;

    public Account getByAcctById(int id){
        return accountMapper.getAcct(id);
    }
}
2.整合mybatis-plus 1、什么是MyBatis-Plus

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

mybatis plus 官网

建议安装 MybatisX 插件

2.修改pom文件
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        

优点:

  • 只需要我们的Mapper继承 baseMapper 就可以拥有crud能力 3.创建mapper
    @Mapper
    public interface UserMapper extends baseMapper {
    }
    4.创建service层
    //接口实现IService接口
    public interface UserService extends IService {
    }
    //实现类,继承ServiceImpl
    @Service
    public class UserServiceImpl extends ServiceImpl implements UserService {
    
    }
    5.实现功能
    @Controller
    public class tableController {
        @Autowired
        UserService userService;
    
        @GetMapping("/dynamic_table")
        public String dynamicTable(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
            List list = userService.list();
            model.addAttribute("users", list);
            Page page = new Page<>(pn, 2);
            //page是分页查询的结果
            Page userPage = userService.page(page, null);
            model.addAttribute("users", userPage);
            long current = page.getCurrent();
            long pages = page.getPages();
            long total = page.getTotal();
            List records = page.getRecords();
    
            return "/table/dynamic_table";
        }
    }
    6.实现分页功能的插件
    @Configuration
    public class MybatisConfig {
        // 旧版
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 设置请求的页面大于最大页后 *** 作, true调回到首页,false 继续请求  默认false
            // paginationInterceptor.setOverflow(false);
            // 设置最大单页限制数量,默认 500 条,-1 不受限制
            // paginationInterceptor.setLimit(500);
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    
        // 最新版
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
            return interceptor;
        }
    }

    更多功能可以参照mybatis-plus官网

    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://outofmemory.cn/zaji/5710358.html

  • (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-12-17
    下一篇 2022-12-17

    发表评论

    登录后才能评论

    评论列表(0条)

    保存