SpringBoot集成Mybatis框架

SpringBoot 集成Mybatis框架

1.添加依赖

除了常规依赖外,需要加入Mybatis和MySQL依赖。

 1     <!-- Spring Boot Mybatis 依赖 -->
 2         <dependency>
 3             <groupId>org.mybatis.spring.boot</groupId>
 4             <artifactId>mybatis-spring-boot-starter</artifactId>
 5             <version>1.2.0</version>
 6         </dependency>
 7         <!-- MySQL 连接驱动依赖 -->
 8         <dependency>
 9             <groupId>mysql</groupId>
10             <artifactId>mysql-connector-java</artifactId>
11         </dependency>

完整pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.sss.package</groupId>
 4   <artifactId>spMyBatis</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   
 7   <parent>
 8         <groupId>org.springframework.boot</groupId>
 9         <artifactId>spring-boot-starter-parent</artifactId>
10         <version>2.0.4.RELEASE</version>
11         <relativePath/> <!-- lookup parent from repository -->
12     </parent>
13     <!-- 项目设置:编码格式UTF-8 -->
14     <properties>
15         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17         <java.version>1.8</java.version>
18     </properties>
19     <dependencies>
20         
21         <!-- Spring Boot SpringMVC框架依赖 -->
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26         
27         <!-- 热部署 -->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-devtools</artifactId>
31             <optional>true</optional>
32             <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用
33             devtools,需要重新引入 -->
34             <scope>true</scope><!-- 热部署 -->
35         </dependency>
36         <!-- Spring Boot Mybatis 依赖 -->
37         <dependency>
38             <groupId>org.mybatis.spring.boot</groupId>
39             <artifactId>mybatis-spring-boot-starter</artifactId>
40             <version>1.2.0</version>
41         </dependency>
42         <!-- MySQL 连接驱动依赖 -->
43         <dependency>
44             <groupId>mysql</groupId>
45             <artifactId>mysql-connector-java</artifactId>
46         </dependency>
48     </dependencies>
49     
50   
51 </project>

 

2. 在application.properties配置文件中,配置数据源、Mybatis的配置及映射文件。

 1 ## 数据源配置
 2 spring.datasource.url=jdbc:mysql://localhost:3306/库名
 3 spring.datasource.username=用户名
 4 spring.datasource.password=密码
 5 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 6 ## Mybatis 配置 8 mybatis.typeAliasesPackage=实体所在包,起别名
 9 # 映射文件所在路径
10 mybatis.mapperLocations=classpath:mapper/*.xml

3. 在主模块上注解扫描接口包,使用@MapperScan("包名")。

 1 @SpringBootApplication // Spring Boot 应用的标识
 2 @MapperScan("org.spring.springboot.dao") // mapper 接口类扫描包配置
 3 //如果要显示Sql细节还需要在logback配置<logger name="接口类所在包" level="debug" />
 4 public class Application {
 5   public static void main(String[] args) {
 6     // 程序启动入口
 7     // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
 8     SpringApplication.run(Application.class,args);
 9   }
10 }

注:Controller层、Dao层、Service层自动注入和原来Spring框架用法的使用完全一样

 

完整案例:

1.数据库(参考的数据库):

 1 /*创建数据库 springbootdb*/
 2 CREATE DATABASE /*!32312 IF NOT EXISTS*/`springbootdb` /*!40100 DEFAULT CHARACTER SET utf8
 3 */;
 4 USE `springbootdb`;
 5 /*创建表 city*/
 6 DROP TABLE IF EXISTS `city`;
 7 CREATE TABLE `city` (
 8 `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',
 9 `provinceId` int(10) unsigned DEFAULT NULL COMMENT '省份编号',
10 `cityName` varchar(20) DEFAULT NULL COMMENT '城市名称',
11 `description` text COMMENT '城市描述',
12 PRIMARY KEY (`id`)
13 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
14 /*插入测试数据 */
15 insert into `city`(`id`,`provinceId`,`cityName`,`description`) values (1,1,'北京','这是北京
16 市的描述信息,北京这家伙是中国首都,百年帝都,政治经济文化中心,也将是世界的中心.'),(2,2,'郑州','这是
17 郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中心,也是中国的中心城市.'),
18 (3,3,'ZhengZhou','这是郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中
19 心,也是中国的中心城市.');

2.创建maven项目,添加pom.xml依赖

除了常规依赖外,需要加入Mybatis和MySQL依赖。

 1     <!-- Spring Boot Mybatis 依赖 -->
 2         <dependency>
 3             <groupId>org.mybatis.spring.boot</groupId>
 4             <artifactId>mybatis-spring-boot-starter</artifactId>
 5             <version>1.2.0</version>
 6         </dependency>
 7         <!-- MySQL 连接驱动依赖 -->
 8         <dependency>
 9             <groupId>mysql</groupId>
10             <artifactId>mysql-connector-java</artifactId>
11         </dependency>

完整pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.sss.package</groupId>
 4   <artifactId>spMyBatis</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   
 7   <parent>
 8         <groupId>org.springframework.boot</groupId>
 9         <artifactId>spring-boot-starter-parent</artifactId>
10         <version>2.0.4.RELEASE</version>
11         <relativePath/> <!-- lookup parent from repository -->
12     </parent>
13     <!-- 项目设置:编码格式UTF-8 -->
14     <properties>
15         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17         <java.version>1.8</java.version>
18     </properties>
19     <dependencies>
20         
21         <!-- Spring Boot SpringMVC框架依赖 -->
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26         
27         <!-- 热部署 -->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-devtools</artifactId>
31             <optional>true</optional>
32             <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用
33             devtools,需要重新引入 -->
34             <scope>true</scope><!-- 热部署 -->
35         </dependency>
36         <!-- Spring Boot Mybatis 依赖 -->
37         <dependency>
38             <groupId>org.mybatis.spring.boot</groupId>
39             <artifactId>mybatis-spring-boot-starter</artifactId>
40             <version>1.2.0</version>
41         </dependency>
42         <!-- MySQL 连接驱动依赖 -->
43         <dependency>
44             <groupId>mysql</groupId>
45             <artifactId>mysql-connector-java</artifactId>
46         </dependency>
48     </dependencies>
49     
50   
51 </project>

创建位置

2. 在application.properties配置文件中,配置数据源、Mybatis的配置及映射文件。

 1 ## 数据源配置
 2 spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb
 3 spring.datasource.username=root
 4 spring.datasource.password=admin
 5 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 6 ## Mybatis 配置
 7 # 实体所在包,起别名
 8 mybatis.typeAliasesPackage=com.sqy.start.mapper
 9 # 映射文件所在路径
10 mybatis.mapperLocations=classpath:mapper/*.xml

3.主模块,启动类

 1 package com.sqy.start;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 @MapperScan("com.sqy.start.mapper")
 9 public class Start {
10     public static void main(String[] args) {
11         SpringApplication application = new SpringApplication(Start.class);
12         application.setAddCommandLineProperties(false);
13         application.run(args);
14     }
15 }

4.mapper和mapper.xml文件

1 package com.sqy.start.mapper;
2 
3 import org.apache.ibatis.annotations.Mapper;
4 
5 @Mapper
6 public interface CityMapper {
7     int getCount();
8 }
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3 <mapper namespace="com.sqy.start.mapper.CityMapper">
4     <select id="getCount" resultType="int">
5         select count(*) from city
6     </select>
7 </mapper>

5.service和service的实现类

1 package com.sqy.start.service;
2 
3 public interface CityService {
4     int findCount();
5 }
 1 package com.sqy.start.service.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.sqy.start.mapper.CityMapper;
 7 import com.sqy.start.service.CityService;
 8 @Service
 9 public class CityServiceImpl implements CityService {
10     @Autowired
11     private CityMapper cMapper;
12     public int findCount() {
13         // TODO Auto-generated method stub
14         return cMapper.getCount();
15     }
16 
17 }

6.controller层

 1 package com.sqy.start.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import com.sqy.start.service.CityService;
 8 
 9 @RestController
10 public class StartController {
11     @Autowired
12     private CityService cService;
13     @RequestMapping("/getCount")
14     public int getCount() {
15         return cService.findCount();
16     }
17     
18 }

7.启动项目,在浏览器输入http://localhost:8080/getCount

浏览器显示:

ok了

 

posted @ 2018-11-27 14:06  闹闹S  阅读(324)  评论(0编辑  收藏  举报