首页 > 数据库技术 > 详细

小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_33、SpringBoot2.x整合Mybatis3.x注解实战

时间:2019-08-28 18:27:13      阅读:82      评论:0      收藏:0      [点我收藏+]

笔记


2、SpringBoot2.x整合Mybatis3.x注解实战
    简介:SpringBoot2.x整合Mybatis3.x注解配置实战

        1、使用starter, maven仓库地址:http://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter

        2、加入依赖(可以用 http://start.spring.io/ 下载)
                    
            <!-- 引入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>

        3、加入配置文件
            #mybatis.type-aliases-package=net.xdclass.base_project.domain
            #可以自动识别
            #spring.datasource.driver-class-name =com.mysql.jdbc.Driver

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

        加载配置,注入到sqlSessionFactory等都是springBoot帮我们完成

        4、启动类增加mapper扫描
            @MapperScan("net.xdclass.base_project.mapper")

             技巧:保存对象,获取数据库自增id 
             @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")

        4、开发mapper
            参考语法 http://www.mybatis.org/mybatis-3/zh/java-api.html

        5、sql脚本
            CREATE TABLE `user` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `name` varchar(128) DEFAULT NULL COMMENT ‘名称‘,
              `phone` varchar(16) DEFAULT NULL COMMENT ‘用户手机号‘,
              `create_time` datetime DEFAULT NULL COMMENT ‘创建时间‘,
              `age` int(4) DEFAULT NULL COMMENT ‘年龄‘,
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;


        相关资料:
        http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration

        https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples

        整合问题集合:
            https://my.oschina.net/hxflar1314520/blog/1800035
            https://blog.csdn.net/tingxuetage/article/details/80179772

开始

这里用注解的方式,因为比较简单,

mapper:数据库接口,访问数据库那一层的接口
service:业务逻辑
util:工具类

user类
技术分享图片
自动生成项目的形式
技术分享图片

一般不用最新版本 可能会有bug
技术分享图片
技术分享图片

数据源一般用阿里巴巴的druid数据源。把下面三个依赖都加进去
技术分享图片
技术分享图片
热部署也加进来了
技术分享图片
加入配置文件
技术分享图片
默认的数据库连接池。如果把Druid这个阿里巴巴的注释掉。就会用默认的了
这里连接的是movie数据库下的user表
技术分享图片

启动类加上扫描对应的包

扫描的是mapper这个包下 mapper相当于dao层
技术分享图片
复制mapper里面的类的完整包名
技术分享图片

技术分享图片

技术分享图片

mapper的开发可以参考官方文档
技术分享图片

技术分享图片

技术分享图片
这里的主键是自增的
技术分享图片
拿到自增的id
技术分享图片
service就是一个接口,里面定义方法
技术分享图片
service的实现类。getId就是增加后获取到的主键id
技术分享图片
Service的实现类记得用@Service的注解
技术分享图片

controller

技术分享图片
技术分享图片

测试

启动程序
code返回的是0。data是45,data是主键的id
技术分享图片
再保存一个就是46
技术分享图片
数据库内保存了46的数据
技术分享图片

相关的资料

相关资料:
        http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration

        https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples

        整合问题集合:
            https://my.oschina.net/hxflar1314520/blog/1800035
            https://blog.csdn.net/tingxuetage/article/details/80179772

小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_33、SpringBoot2.x整合Mybatis3.x注解实战

原文:https://www.cnblogs.com/wangjunwei/p/11425137.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!