• springboot2整合mybatis实例


    本文主要讲解sb2和mybatis的整合要点。本文以表user为例。

    步骤:

    一、首先的前提:(共三步,只做一次)

    1.在pom.xml文件引入应用对mybatis的依赖:

    <!-- 引入starter -->
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
    <scope>runtime</scope>
    </dependency>

    <!-- MySQL的JDBC驱动包 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <!-- 引入第三方数据源 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
    </dependency>

    2.编写配置文件application.properties相关信息:

    spring.datasource.url=jdbc:mysql://localhost:3306/userinfo?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username =root
    spring.datasource.password =root
    #如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
    spring.datasource.type =com.alibaba.druid.pool.DruidDataSource

    3.在应用的启动类上添加注解@MapperScan("xx.xx.mapper") 让系统可以扫描到mapper;

    二、接下来就是具体的操作:(共6步)

    1.在domain包编写user的对象类文件:

    2.在mapper包编写mapper文件:

    3.在controller包编写控制器:

      需要自动注入UserService,以自动生成userService bean。

    4.在service包编写UserService接口:

    5.在service.impl包编写实现类UserServiceImpl:

      实现类上要添加注解@Service,否则系统不能生成UserService的bean,内部需要自动注入mapper来实现对数据库的访问。

  • 相关阅读:
    单点登录
    Found conflicts between different versions of the same dependent assembly that could not be resolved
    在Visual Studio Code中使用C#以及.net core
    GitBlit中出现 error: remote unpack failed: error Missing tree
    net user
    SwitchyOmega
    What's the difference between Unicode and UTF-8?
    2>&1
    [C++基金会]位计算 游戏开发中的应用
    Broadcast Receiver注意事项
  • 原文地址:https://www.cnblogs.com/zhangxj/p/9757956.html
Copyright © 2020-2023  润新知