1 Star 0 Fork 0

呐喊 / hlinfo-mybatis-dao

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

hlinfo-MyBatis-Dao是什么?

hlinfo-MyBatis-Dao是能够在Spring Boot中快速集成使用mybatis的工具,是mybatis的SqlSessionTemplate深度集成整合的操作实现,无需写Mapper类,只需要写xml文件即可,不对MyBatis做任何改变。

特点

引入maven依赖即可使用Mybatis,无需配置@Bean,内部已经自动注入了相关的@Bean

快速使用

1.引入pom:

请前往Maven中央库查找最新版本

非SpringBoot工程直接引入:

<dependency>
    <groupId>net.hlinfo</groupId>
    <artifactId>hlinfo-mybatis-dao</artifactId>
    <version>1.1.0</version>
</dependency>

Spring Boot 工程则引入hlinfo-utils-spring-boot-starter即可(自动注入相关Bean):

<dependency>
  <groupId>net.hlinfo</groupId>
  <artifactId>hlinfo-utils-spring-boot-starter</artifactId>
  <version>1.0.1</version>
</dependency>

注意:此maven依赖已经包含了MyBatis(mybatis-spring-boot-starter最新版)的内容,请勿再单独引入Mybatis的依赖

2.在application.yml或application.properties配置MyBatis的mapper的相关路径

application.yml配置示例:

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath*:mybatis/mapper/*.xml
  type-aliases-package: net.hlinfo.swagger.entity

注:

config-location: MyBatis xml配置文件的位置

mapper-locations: MyBatis mapper文件的位置(注意:在classpath后面的 * 必不可少,本程序路径为mybatis/mapper[也就是resources下的mybatis目录下的mapper],如果您程序需要改为其他路径,如mappers[也就是resources下的mappers目录],需要保留本程序的mapper文件路径[classpath*:mybatis/mapper/ *.xml],则mybatis.mapper-locations=classpath:mappers/*.xml,classpath*:mybatis/mapper/*.xml)

type-aliases-package: 要搜索类型别名的包(也就是实体类的包名)

3.在Controller使用


package net.hlinfo.example.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;

import net.hlinfo.mybatis.opt.QueryResult;
import net.hlinfo.mybatis.service.MybatisService;
import net.hlinfo.swagger.entity.UserInfo;
import net.hlinfo.swagger.opt.Funs;
import net.hlinfo.swagger.opt.Resp;

//@Api(tags="首页")
@RequestMapping("/")
@RestController
public class IndexController {

	private MybatisService mybatisService;
	
	public IndexController(MybatisService mybatisService) {
		super();
		this.mybatisService = mybatisService;
	}

	/**
	*管理员列表
	*/
	@GetMapping("/list")
	public Resp<QueryResult<UserInfo>> list(){
		 List<Map> list = mybatisService.queryList("select_userinfo_list", Map.class);
		return new Resp<>().ok("ok", new QueryResult<>(list));
	}
	
}

Mapper文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="net.hlinfo.example.entity.UserInfo">
  <select id="select_userinfo_list" resultType="Map">
  select * from user_info 
  </select>
</mapper>

4.备注

参考资料

许可证

MIT License

MIT License Copyright (c) 2021 呐喊 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

mybatis SqlSessionTemplate 集成深度整合,在Spring Boot中快速使用,无需写Mapper类,只需要写xml文件即可 展开 收起
Java
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/hlinfo/hlinfo-mybatis-dao.git
git@gitee.com:hlinfo/hlinfo-mybatis-dao.git
hlinfo
hlinfo-mybatis-dao
hlinfo-mybatis-dao
master

搜索帮助