SpringBoot(九)_springboot集成 MyBatis
MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中。具体细节这里就不在叙述,大家自行查找资料进行学习下。
加载依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
application 配置(application.yml)
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.zhb.entity
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
password: root
username: root
启动类
在启动类中添加对 Mapper 包扫描@MapperScan,Spring Boot 启动的时候会自动加载包路径下的 Mapper。
@Spring BootApplication
@MapperScan("com.zhb.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 Mapper 加个注解会很麻烦。
代码展示
这里只说下,我们在controller 中 尽量使用 Restful 风格
@RestController
public class UserController {
@Resource
private UserMapper userMapper;
@GetMapping(value = "/users")
public List<UserEntity> getUsers() {
List<UserEntity> users=userMapper.getAll();
return users;
}
@GetMapping(value = "/users/{id}")
public UserEntity getUser(@PathVariable(value = "id") Long id) {
UserEntity user=userMapper.getOne(id);
return user;
}
@PostMapping("/users")
public void save(UserEntity user) {
userMapper.insert(user);
}
@PutMapping("/users")
public void update(UserEntity user) {
userMapper.update(user);
}
@DeleteMapping(value="/users/{id}")
public void delete(@PathVariable("id") Long id) {
userMapper.delete(id);
}
}
实际开发常遇问题
在平常开发中,我们经常会遇到返回树结构,如下展示
[
{
"id": "1",
"name": "山东",
"pid": "0",
"children": [
{
"id": "2",
"name": "济南",
"pid": "1",
"children": [
{
"id": "3",
"name": "高新区",
"pid": "2",
"children": null
}
]
}
]
}
]
(1) java中 通过对list数据进行拼接成树
如下面的方法(这里直接声明了list,往里面添加数据,来演示查询出的list集合)
@GetMapping(value = "/area")
public List<AreaTreeEntity> get(){
List<AreaTreeEntity> res= new ArrayList<>();
Map<String,List<AreaTreeEntity>> childMap = new HashMap<>(16);
//为了方便测试,构建list数据
List<AreaTreeEntity> s = new ArrayList<>();
AreaTreeEntity a = new AreaTreeEntity();
a.setId("1");
a.setName("山东");
a.setPid("0");
s.add(a);
AreaTreeEntity a1 = new AreaTreeEntity();
a1.setId("2");
a1.setName("济南");
a1.setPid("1");
s.add(a1);
AreaTreeEntity a2 = new AreaTreeEntity();
a2.setId("3");
a2.setName("高新区");
a2.setPid("2");
s.add(a2);
for(AreaTreeEntity entity : s){
if ("0".equals(entity.getPid())) {
res.add(entity);
}
else {
List<AreaTreeEntity> childList = (childMap.containsKey(entity.getPid())) ? childMap.get(entity.getPid()) : new ArrayList<>();
childList.add(entity);
if (!childMap.containsKey(entity.getPid())){
childMap.put(entity.getPid(),childList);
}
}
}
for(AreaTreeEntity entity : res){
findChild(entity,childMap);
}
return res;
}
public void findChild(AreaTreeEntity entity,Map<String,List<AreaTreeEntity>> childMap){
if (childMap.containsKey(entity.getId())){
List<AreaTreeEntity> chidList = childMap.get(entity.getId());
for (AreaTreeEntity childEntity : chidList){
findChild(childEntity,childMap);
}
entity.setChildren(chidList);
}
}
经过验证,返回数据如下
[
{
"id": "1",
"name": "山东",
"pid": "0",
"children": [
{
"id": "2",
"name": "济南",
"pid": "1",
"children": [
{
"id": "3",
"name": "高新区",
"pid": "2",
"children": null
}
]
}
]
}
]
(2) 使用mabatis的collection 直接查询出树结构
<mapper namespace="com.zhb.mapper.AreaTreeMapper" >
<resultMap id="TreeResultMap" type="com.zhb.entity.AreaTreeEntity" >
<result column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<collection property="children" column="id" ofType="com.zhb.entity.AreaTreeEntity" javaType="ArrayList" select="selectChildrenById"/>
</resultMap>
<select id="getAreaTree" resultMap="TreeResultMap">
select id,name from area where parent_id = '0'
</select>
<select id="selectChildrenById" parameterType="java.lang.String" resultMap="TreeResultMap">
select id,name from area where parent_id = #{id}
</select>
</mapper>
完整代码下载:github
注:项目中加入swagger ,方便大家测试,直接访问 http://localhost:8080/swagger-ui.html
SpringBoot(九)_springboot集成 MyBatis的更多相关文章
- SpringBoot系列之集成Mybatis教程
SpringBoot系列之集成Mybatis教程 环境准备:IDEA + maven 本博客通过例子的方式,介绍Springboot集成Mybatis的两种方法,一种是通过注解实现,一种是通过xml的 ...
- SpringBoot学习之集成mybatis
一.spring boot集成Mybatis gradle配置: //gradle配置: compile("org.springframework.boot:spring-boot-star ...
- SpringBoot(十)_springboot集成Redis
Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...
- SpringBoot(八)_springboot集成swagger2
swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试. (1) 引入依赖,我们选择现在最新的版本 <dependency> &l ...
- springboot 零xml集成mybatis
maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- springboot之集成mybatis mongo shiro druid redis jsp
闲来无事,研究一下spingboot 发现好多地方都不一样了,第一个就是官方默认不支持jsp 于是开始狂找资料 终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
随机推荐
- 【搭建RAC报错】搭建RAC,第二个节点执行root.sh报错:CRS-2800、CRS-4000
Creating /etc/oratab file...Entries will be added to the /etc/oratab file as needed byDatabase Confi ...
- 【linux】linux常用命令汇总
linux主要的目录树的有/. /root. /home. /usr. /bin 等目录 / 根目录 /bin 存放必要的命令 /boot 存放内核以及启动所需的文件 /dev 存放设备文件 /etc ...
- Flutter - 创建横跨所有页面的侧滑菜单
前一篇博客讲到了如何创建侧滑菜单,但是再实际使用过程中,会发现,这个策划菜单只能在首页侧滑出来. 当导航到其他页面后,侧滑就不管用了.这也有点不符合良好的用户体验设计.Google Play就是很好的 ...
- 关于判断用户输入的是不是int类型,这次没有正则表达式
末尾没有目的地的出租车,污点证人禁止入内!!! 不同的尝试有不同的方法 关于int类型的判断,我尝试了这么一个方法,可行,只是笨 正则表达式我没有搞清楚,没办法给大家讲解,欢迎各位明白人讲解,或者是我 ...
- 【前端模板之路】一、重构的兄弟说:我才不想看你的代码!把HTML给我交出来!
写在前面 随着前端领域的发展和社会化分工的需要,继前端攻城湿之后,又一重要岗位横空出世——重构攻城湿!所谓的重构攻城湿,他们的一大特点之一,就是精通CSS配置文件的编写...前端攻城湿跟重构攻城湿是一 ...
- 2018年美国大学生数学建模竞赛(MCM/ICM) 比赛心得
话不多说,题目先上: 这是我们这次选择的题目,说说建模的那些事! 美赛的时间和国赛挑战杯时间略有不同,貌似多的一天是为了让我们对文章进行一个翻译吧QAQ 建议参加美赛的同学可以参照此计划进行 Day0 ...
- 大神教你零基础学PS,30堂课从入门到精通
ps视频教程,ps自学视频教程.ps免费视频教程下载,大神教你零基础学PS教程视频内容较大,分为俩部分: 大神教你零基础学PS--30堂课从入门到精通第一部分:百度网盘,https://pan.bai ...
- vim使用技巧(插入,删除,查找,复制,粘贴,剪切)
原文链接:http://blog.csdn.net/qq_38646470/article/details/79643000 编程人员很喜欢的编辑器:vim 先搞清楚vim的三种模式: 1.命令模式: ...
- FM在特征组合中的应用
原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun 特征组合 x1年龄 x2北京 x3上海 x4深圳 x5男 x6女 用户1 ...
- 基本数据结构 -- 栈简介(C语言实现)
栈是一种后进先出的线性表,是最基本的一种数据结构,在许多地方都有应用. 一.什么是栈 栈是限制插入和删除只能在一个位置上进行的线性表.其中,允许插入和删除的一端位于表的末端,叫做栈顶(top),不允许 ...