1.在pom.xml中添加依赖,有两种方式
- 第一种:直接在pom.xml中添加
mybatis-spring-boot-starter
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 集成druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
- 第二种:在pom.xml中添加物理分页插件
pagehelper-spring-boot-starter
依赖,该jar包底层依赖mybatis-spring-boot-starter
,同时可以非常方便的实现分页功能。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!-- 集成druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- mybatis生成器-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.在application.properties中配置数据库链接信息与mapper-locations
spring.datasource.url=jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml,classpath*:com/**/mapper/*.xml
logging.level.com.mybatis.gathermybatis.mbg.mapper=DEBUG
mybatis两种打印sql语句配置的区别:
第一种:不显示类信息,显示结果集
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4df43477] was not registered for synchronization because synchronization is not active
Cache Hit Ratio [SQL_CACHE]: 0.0
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@244a932d] will not be managed by Spring
==> Preparing: SELECT count(0) FROM pms_brand
==> Parameters:
<== Columns: count(0)
<== Row: 11
<== Total: 1
==> Preparing: select id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic from pms_brand LIMIT ?
==> Parameters: 6(Integer)
<== Columns: id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic
<== Row: 1, 万和, W, 0, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg,
<== Row: 2, 三星, S, 100, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (1).jpg, null
<== Row: 3, 华为, H, 100, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg, null
<== Row: 4, 格力, G, 30, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/dc794e7e74121272bbe3ce9bc41ec8c3_222_222.jpg, null
<== Row: 5, 方太, F, 20, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (4).jpg, null
<== Row: 6, 小米, M, 500, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180518/5afd7778Nf7800b75.jpg
<== Total: 6
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4df43477]
第二种:显示类信息,不显示结果集
2019-12-16 18:57:35.384 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT : ==> Preparing: SELECT count(0) FROM pms_brand
2019-12-16 18:57:35.398 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT : ==> Parameters:
2019-12-16 18:57:35.407 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT : <== Total: 1
2019-12-16 18:57:35.409 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample : ==> Preparing: select id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic from pms_brand LIMIT ?
2019-12-16 18:57:35.410 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample : ==> Parameters: 6(Integer)
2019-12-16 18:57:35.413 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample : <== Total: 6
3.在启动类或者自定义mybatis配置类(@Configuration注解)
上添加mapper接口扫描@MapperScan("mapper包路径")
注意:@MapperScan()为mybaitis的。
@MapperScan() import org.mybatis.spring.annotation.MapperScan;
@Configuration import org.springframework.context.annotation.Configuration;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)