栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合tkmapper以及遇到的大坑:

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合tkmapper以及遇到的大坑:

第一步:先创建springboot项目 第二步:导入依赖:

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.1.5
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.22
        
        
            mysql
            mysql-connector-java
            8.0.17
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.projectlombok
            lombok
            1.18.22
        
        
            junit
            junit
        
    
第三步:创建相应的包(dao,entity,mapper等): 第四步:配置文件application.yml
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/shopmall?serverTimezone=GMT%2B8&useUnicode=true&&characterEncoding=UTF-8
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml
  type-aliases-package: com.abtu.entity

server:
  port: 8083
Dao层代码:需要继承(Mapper<>,MySqlMapper<>)
@Repository
public interface UserDao extends Mapper, MySqlMapper {

}
启动类需要添加MapperScan注解

注意MapeperScande注解一定要映入tkMapper的:tk.mybatis.spring.annotation.MapperScan;

import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.abtu.dao")
public class TkmapperDemoApplication {

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

}
编写测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
class TkmapperDemoApplicationTests {

    @Autowired
    UserDao userDao;

    @Test
    public void contextLoads() {
        Users user = new Users();
        user.setUsername("5654654654");
        user.setPassword("1111");
        user.setUserImg("img/default.png");
        user.setUserRegtime(new Date());
        user.setUserModtime(new Date());
        int i = userDao.insert(user);
        System.out.println(i);
    }
   
}

完成!!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358394.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号