spring-boot整合Mybatis案例

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 3.2.5

2.Maven Plugin管理

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>spring-boot-mybaits-xml</groupId>
 8     <artifactId>spring-boot-mybaits-xml</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.5.6.RELEASE</version>
15     </parent>
16 
17     <dependencies>
18         <!-- spring-boot的web启动的jar包 -->
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-web</artifactId>
22         </dependency>
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-test</artifactId>
26         </dependency>
27         <!-- Spring Boot 集成MyBatis -->
28         <dependency>
29             <groupId>org.mybatis.spring.boot</groupId>
30             <artifactId>mybatis-spring-boot-starter</artifactId>
31             <version>1.3.1</version>
32         </dependency>
33         <!-- 数据库驱动 -->
34         <dependency>
35             <groupId>mysql</groupId>
36             <artifactId>mysql-connector-java</artifactId>
37             <version>5.1.35</version>
38         </dependency>
39         <!-- druid-->
40         <dependency>
41             <groupId>com.alibaba</groupId>
42             <artifactId>druid</artifactId>
43             <version>1.0.27</version>
44         </dependency>
45     </dependencies>
46 
47     <build>
48         <plugins>
49             <plugin>
50                 <groupId>org.mybatis.generator</groupId>
51                 <artifactId>mybatis-generator-maven-plugin</artifactId>
52                 <version>1.3.5</version>
53                 <executions>
54                     <execution>
55                         <id>Generate MyBatis Artifacts</id>
56                         <goals>
57                             <goal>generate</goal>
58                         </goals>
59                     </execution>
60                 </executions>
61             </plugin>
62             <plugin>
63                 <groupId>org.apache.maven.plugins</groupId>
64                 <artifactId>maven-compiler-plugin</artifactId>
65                 <configuration>
66                     <source>1.7</source>
67                     <target>1.7</target>
68                 </configuration>
69             </plugin>
70         </plugins>
71     </build>
72 
73 
74 
75 </project>
View Code

3.application.properties编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 # 驱动配置信息  
 2 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource  
 3 spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springbootdb
 4 spring.datasource.username = root
 5 spring.datasource.password = root
 6 spring.datasource.driverClassName = com.mysql.jdbc.Driver
 7 
 8 # mybatis
 9 mybatis.type-aliases-package=com.goku.demo.model
10 mybatis.mapper-locations=classpath:mapping/**/*.xml
View Code

4.mybatis.generator代码生成器

generatorConfig.xml修改具体表内容

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
1         <!-- !!!! Table Configurations !!!! -->
2         <table tableName="user_" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
3                enableUpdateByExample="false"/>
View Code

执行代码生成

spring-boot整合Mybatis案例

 

 查看生成代码 model,mapper,mapping

spring-boot整合Mybatis案例

 

5.service层

UserService接口类编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 package com.goku.demo.service;
 2 
 3 import com.goku.demo.model.UserWithBLOBs;
 4 
 5 /**
 6  * Created by nbfujx on 2017-12-01.
 7  */
 8 public interface UserService {
 9     UserWithBLOBs selectByPrimaryKey(String id);
10 }
View Code

UserServiceImpl接口实现类编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 package com.goku.demo.service.impl;
 2 
 3 import com.goku.demo.mapper.UserMapper;
 4 import com.goku.demo.model.UserWithBLOBs;
 5 import com.goku.demo.service.UserService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 /**
10  * Created by nbfujx on 2017-12-01.
11  */
12 @Service("UserService_one")
13 public class UserServiceImpl implements UserService {
14 
15     @Autowired
16     UserMapper userMapper;
17 
18     @Override
19     public UserWithBLOBs selectByPrimaryKey(String id) {
20         return userMapper.selectByPrimaryKey(id);
21     }
22 }
View Code

6.controller层

UserController接口类编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 package com.goku.demo.controller;
 2 
 3 import com.goku.demo.model.UserWithBLOBs;
 4 
 5 /**
 6  * Created by nbfujx on 2017-12-01.
 7  */
 8 public interface UserController {
 9     UserWithBLOBs selectByPrimaryKey(String id);
10 }
View Code

UserControllerImpl接口实现类编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 package com.goku.demo.controller.impl;
 2 
 3 import com.goku.demo.controller.UserController;
 4 import com.goku.demo.model.UserWithBLOBs;
 5 import com.goku.demo.service.UserService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.beans.factory.annotation.Qualifier;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.RestController;
12 
13 /**
14  * Created by nbfujx on 2017-12-01.
15  */
16 @RestController
17 @RequestMapping("/User")
18 public class UserControllerImpl implements UserController {
19 
20     @Autowired
21     @Qualifier("UserService_one")
22     UserService userservice;
23 
24     @Override
25     @RequestMapping("/{id}")
26     public UserWithBLOBs selectByPrimaryKey(@PathVariable String id) {
27         return userservice.selectByPrimaryKey(id);
28     }
29 }
View Code

7.Application启动类编写

spring-boot整合Mybatis案例spring-boot整合Mybatis案例
 1 package com.goku.demo;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 7 import org.springframework.boot.web.servlet.ServletComponentScan;
 8 
 9 /**
10  * Created by nbfujx on 2017/11/20.
11  */
12 // Spring Boot 应用的标识
13 @SpringBootApplication
14 @ServletComponentScan
15 @MapperScan(basePackages="com.goku.demo.mapper")
16 public class DemoApplication {
17 
18     public static void main(String[] args) {
19         // 程序启动入口
20         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
21         SpringApplication.run(DemoApplication.class,args);
22     }
23 }
View Code

8.在页面上运行

http://localhost:8080/User/test

spring-boot整合Mybatis案例

9.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-mybaits-xml

上一篇:Knative 初体验:Serving Hello World


下一篇:CNCF中国大使张磊:什么是云原生?