spring-boot整合Mybatis案例
1.运行环境
开发工具:intellij idea
JDK版本:1.8
项目管理工具:Maven 3.2.5
2.Maven Plugin管理
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion> <groupId>spring-boot-mybaits-xml</groupId>
<artifactId>spring-boot-mybaits-xml</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent> <dependencies>
<!-- spring-boot的web启动的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- Spring Boot 集成MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.27</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build> </project>
3.application.properties编写
# 驱动配置信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springbootdb
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver # mybatis
mybatis.type-aliases-package=com.goku.demo.model
mybatis.mapper-locations=classpath:mapping/**/*.xml
4.mybatis.generator代码生成器
generatorConfig.xml修改具体表内容
<!-- !!!! Table Configurations !!!! -->
<table tableName="user_" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
执行代码生成

查看生成代码 model,mapper,mapping

5.service层
UserService接口类编写
package com.goku.demo.service; import com.goku.demo.model.UserWithBLOBs; /**
* Created by nbfujx on 2017-12-01.
*/
public interface UserService {
UserWithBLOBs selectByPrimaryKey(String id);
}
UserServiceImpl接口实现类编写
package com.goku.demo.service.impl; import com.goku.demo.mapper.UserMapper;
import com.goku.demo.model.UserWithBLOBs;
import com.goku.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* Created by nbfujx on 2017-12-01.
*/
@Service("UserService_one")
public class UserServiceImpl implements UserService { @Autowired
UserMapper userMapper; @Override
public UserWithBLOBs selectByPrimaryKey(String id) {
return userMapper.selectByPrimaryKey(id);
}
}
6.controller层
UserController接口类编写
package com.goku.demo.controller; import com.goku.demo.model.UserWithBLOBs; /**
* Created by nbfujx on 2017-12-01.
*/
public interface UserController {
UserWithBLOBs selectByPrimaryKey(String id);
}
UserControllerImpl接口实现类编写
package com.goku.demo.controller.impl; import com.goku.demo.controller.UserController;
import com.goku.demo.model.UserWithBLOBs;
import com.goku.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* Created by nbfujx on 2017-12-01.
*/
@RestController
@RequestMapping("/User")
public class UserControllerImpl implements UserController { @Autowired
@Qualifier("UserService_one")
UserService userservice; @Override
@RequestMapping("/{id}")
public UserWithBLOBs selectByPrimaryKey(@PathVariable String id) {
return userservice.selectByPrimaryKey(id);
}
}
7.Application启动类编写
package com.goku.demo; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* Created by nbfujx on 2017/11/20.
*/
// Spring Boot 应用的标识
@SpringBootApplication
@ServletComponentScan
@MapperScan(basePackages="com.goku.demo.mapper")
public class DemoApplication { public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(DemoApplication.class,args);
}
}
8.在页面上运行
http://localhost:8080/User/test

9.GITHUB地址
https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-mybaits-xml
spring-boot整合Mybatis案例的更多相关文章
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- spring boot 整合 mybatis 以及原理
同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...
- Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题
现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...
- Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider
Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包
- Spring Boot整合MyBatis(非注解版)
Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring Boot系列(三):Spring Boot整合Mybatis源码解析
一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...
- 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?
Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
- Spring boot整合Mybatis
时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了.闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboot已经成为整个业界开发主 ...
随机推荐
- threading.get_ident()
https://docs.python.org/3/library/threading.html Return the 'thread identifier' of the current threa ...
- Android O编译前修改文件和目录权限
当需要修改某文件或路径权限时,我们可以在init.rc开机启动某节点添加chmod命令进行修改.但是对于system分区,由于是ro权限,在init.rc使用chmod修改权限无效.需要在文件编译时, ...
- Grafana+Prometheus系统监控之Redis
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...
- springMvc中自定义bean转换接收前台传的参数
转载:https://blog.csdn.net/u013476435/article/details/81538099 因前端整体传参时,参数名都不是驼峰写法,类似 music_name,music ...
- 泛微e-cology OA Beanshell组件远程代码执行漏洞复现CNNVD-201909-1041
靶机 影响版本 泛微e-cology<=9.0 https://github.com/jas502n/e-cology 部署 复现 /weaver/bsh.servlet.BshServlet ...
- 身份证验证的js
function isIdCardNo(num) { num = num.toUpperCase(); //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能 ...
- Hibernate入门2
实体类的编写规则 要求实体类的属性是私有的 要求实体类中的私有属性有公开的get和set方法(设置器和访问器) 要求实体类有一个属性作为唯一值(一般使用id值) 实体类属性建议不使用基本数据类型,使用 ...
- vue使用vue-router beforEach实现判断用户登录跳转路由筛选
vue使用vue-router beforEach实现判断用户登录跳转路由筛选 :https://www.colabug.com/3306814.html 在开发webApp的时候,考虑到用户体验,经 ...
- [NOIP2016]借教室
NOIP2012提高组D2T2. 这道题目非常基础,正解貌似是二分+差分数组,在这里提供一种线段树的思路. 很容易发现题目让我们每次修改一段区间,然后我们只需要看每一个区间内有没有负数就可以了.可以用 ...
- luoguP1965 转圈游戏(NOIP2013)(快速幂)
luogu P1965 转圈游戏 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include ...