spring-boot整合Mybatis案例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 1.运行环境开发工具:intellij ideaJDK版本:1.8项目管理工具:Maven 3.2.52.Maven Plugin管理 1 2 5 4.0.0 6 7 spring-boot-mybaits-xml 8 spring-boot-mybaits-xml 9 1.

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 3.2.5

2.Maven Plugin管理

 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编写

 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修改具体表内容

1         <!-- !!!! Table Configurations !!!! -->
2         <table tableName="user_" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
3                enableUpdateByExample="false"/>
View Code

执行代码生成

 

 查看生成代码 model,mapper,mapping

 

5.service层

UserService接口类编写

 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接口实现类编写

 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接口类编写

 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接口实现类编写

 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启动类编写

 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

9.GITHUB地址

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
27天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
40 0
|
30天前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
20 1
|
30天前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
30天前
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
15 1
|
30天前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
21 1
|
30天前
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
22 2
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
25 0
|
30天前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
15 1
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
8天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系