SpringBoot2.x整合Mybatis3.x注解、MySQL简单的增删改查存在01

第一步:下载Mybatis3.x和MySQL依赖

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、加入在application.properties配置文件

#mybatis.type-aliases-package=net.xdclass.base_project.domain
#可以自动识别
#spring.datasource.driver-class-name =com.mysql.jdbc.Driver

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

#增加打印sql语句(访问接口在控制台中d打印),一般用于本地开发测试
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

加载配置,注入到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
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值