SpringBoot Mybatis 入门
Mybatis for Java API官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html
Mybatis语法介绍
@Select 查询,所有的查询均使用这个
@Insert 插入,直接传入实体类会自动解析属性到对应的值
@Update 修改,也可以直接传入对象
@Delete 删除 @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
这里需要注意,使用#符号和$符号的不同:
// This example creates a prepared statement, something like select * from teacher where name = ?;
@Select("Select * from teacher where name = #{name}")
Teacher selectTeachForGivenName(@Param("name") String name); // This example creates n inlined statement, something like select * from teacher where name = 'someName';
@Select("Select * from teacher where name = '${name}'")
Teacher selectTeachForGivenName(@Param("name") String name);
使用步骤
1、在Maven配置文件中,添加mybatis和mysql依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、application.properties添加数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.16.65.200/hibernate?characterEncoding=UTF8
spring.datasource.username=root
spring.datasource.password=123456
mybatis.type-aliases-package=com.vmware.SpringMVC.domain // Mybatis Mapper接口类的路径
Springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了。
3、编写Mapper接口类
public interface UserMapper {
@Select("SELECT * FROM users")
@Results({
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
List<UserEntity> getAll();
@Select("SELECT * FROM users WHERE id = #{id}")
@Results({
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
UserEntity getOne(Long id);
@Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})")
void insert(UserEntity user);
@Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}")
void update(UserEntity user);
@Delete("DELETE FROM users WHERE id =#{id}")
void delete(Long id);
4、在Service业务逻辑层去调用Mapper接口
// 业务逻辑层调用Mapper
@Service
public class UserService { @Autowired
private UserMapper userMapper; public List<User> getAll(){
return userMapper.getAll();
}
}
// Controller调用Service
@RestController
@RequestMapping("user")
public class UserController { @Autowired
private UserService userService; @GetMapping("/all")
public List<User> getAll(){
return userService.getAll();
}
}
5、在启动类中添加对Mapper包扫描@MapperScan()
/**
* @MapperScan() 表示扫描MyBatis的Mapper接口所在的包,找出所有加了@Mapper或@Repository注解的接口。
*/
@SpringBootApplication
@MapperScan("com.vmware.SpringMVC.domain")
public class SpringMvcApplication { public static void main(String[] args) {
SpringApplication.run(SpringMvcApplication.class, args);
}
}
参考资料:
http://412887952-qq-com.iteye.com/blog/2391924
https://blog.csdn.net/didi7696/article/details/80117238
https://cloud.tencent.com/developer/article/1347912
SpringBoot Mybatis 入门的更多相关文章
- SpringBoot+Mybatis整合入门(一)
SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...
- SpringBoot开发四-MyBatis入门
需求介绍-MyBatis入门 首先就是安装Mysql Server 和Mysql Workbench. SqlSessionFactory:用于创建SqlSession的工厂类 SqlSession: ...
- SpringData 基于SpringBoot快速入门
SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战 ...
- DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)
DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...
- 01-项目简介Springboot简介入门配置项目准备
总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...
- MyBatis从入门到精通(1):MyBatis入门
作为一个自学Java的自动化专业211大学本科生,在学习和实践过程中"趟了不少雷",所以有志于建立一个适合同样有热情学习Java技术的参考"排雷手册". 最近在 ...
- springBoot从入门到源码分析
先分享一个springBoot搭建学习项目,和springboot多数据源项目的传送门:https://github.com/1057234721/springBoot 1. SpringBoot快速 ...
- SpringBoot基础篇-SpringBoot快速入门
SpringBoot基础 学习目标: 能够理解Spring的优缺点 能够理解SpringBoot的特点 能够理解SpringBoot的核心功能 能够搭建SpringBoot的环境 能够完成applic ...
- 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战
大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...
随机推荐
- ORACL EXP导出数据说明
转载自:http://www.jb51.net/article/17358.htm Oracle 数据库导出(exp)导入(imp)说明 exp 将数据库内的各对象以二进制方式下载成dmp文件,方 ...
- EntityFramework(EF) 单表与主从表的使用
一.单表Reader 1 构建Reader类 public class Reader { public int ReaderID { get; set; } publ ...
- python greenlet背景介绍与实现机制
并发处理的技术背景 并行化处理目前很受重视, 因为在很多时候,并行计算能大大的提高系统吞吐量,尤其在现在多核多处理器的时代, 所以像lisp这种古老的语言又被人们重新拿了起来, 函数式编程也越来越流行 ...
- 第11章 Docker Registry 相关问题
11.1 我 docker push 的时候怎么报 authentication required 错误? 因为你没有登录.如果是向 Docker Hub 推送镜像,需要在注册一个用户: https: ...
- Python中的图像处理
第 1 章 基本的图像操作和处理 本章讲解操作和处理图像的基础知识,将通过大量示例介绍处理图像所需的 Python 工具包,并介绍用于读取图像.图像转换和缩放.计算导数.画图和保存结果等的基本工具.这 ...
- 网易2016研发project师编程题
http://www.nowcoder.com/test/970447/summary [编程题] 小易的升级之路 小易常常沉迷于网络游戏.有一次,他在玩一个打怪升级的游戏,他的角色的初始能力值为 a ...
- eclipse中如何查看一个android模拟器的内部文件
eclipse中如何查看一个android模拟器的内部文件,有时要在其中添加一个文件夹或是什么的,要手动的做这件事,而不能够用代码去完成时,就要用这个方法了. 1.首先,打开一个安卓模拟器. 2.这个 ...
- CodeIgniter框架——访问方式 URI 分配变量 数据库操作
1.访问方式: CodeIgniter 的访问URL使用的是pathinfo,入口文件/控制器/方法(/参数列表) eg:localhost/index.php/welcome/index/id 第一 ...
- idea 右下角不显示git分支
开发工程中遇到idea右下角不显示git分支问题: 解决方案:查找资料说是需要打开VCS->Enable version control. 但是Enable version control 已经 ...
- 使用GUID作为数据表主键的好处(转)
http://blog.itpub.net/3875/viewspace-789520/ 分类: 数据库开发技术 使用GUID作为数据表主键的好处 [@more@] 使用GUID作为数据表主键的好处 ...