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 折 ...
随机推荐
- php文件加密
1.在线加密 网址:http://www.phpjm.net/encode.html 本人测试过还可以,就是纯加密,没有解密.
- android自定义View_2——Making the View Interactive
前言:绘制出一个view只是自定义view的一个部分,还需要自定义一些view的行为,来反馈用户的使用操作,反馈的 行为要合理性,就像真是的物理世界一样,不要太玄幻哦. 可以将view的行为封装到in ...
- go语言发送邮件
package main import ( "fmt" "net/smtp" "strings" ) //发送邮件的逻辑函数 func Se ...
- Swift学习笔记(一):No such module 'Cocoa'
在xcode中创建一个Playground文件, 进行导包操作 ,import Cocoa 却提示No such module 'Cocoa' 原因是该Playground文件的platform 设置 ...
- 如何自己实现一套EasyNVR这样的无插件流媒体服务器
EasyNVR流媒体解决方案 EasyNVR能够通过简单的网络摄像机通道配置,将传统监控行业里面的高清网络摄像机IP Camera.NVR等具有RTSP协议输出的设备接入到EasyNVR,EasyNV ...
- Using InfluxDB in Grafana,influxDB在grafana中使用
grafana带有功能丰富的数据源插件influxDB.支持丰富的查询编辑器.注释和templating(模版)查询. 增加数据源(Adding the data source) 点击顶部Grafan ...
- coursera 《现代操作系统》
什么是独占设备技术?为什么说 “SPOOLing不是独占设备的”? 百度百科没有解释,从教材中找到了: 第二章 取数指令 load To load a value from memory, you ...
- A Universally Unique IDentifier (UUID) URN Namespace
w Network Working Group P. Leach Request for Comments: 4122 Microsoft Category: Standards Track M. M ...
- You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or MEMORY storage engine.
w https://dev.mysql.com/doc/refman/5.7/en/create-index.html MySQL :: MySQL 5.7 Reference Manual :: B ...
- JDBC 入门
1. JDBC 简介 JDBC (Java DataBase Connectivity) 就是 Java 数据库连接, 说白了就是用 Java 语言向 数据库发送 SQL 语句. JDBC 其实是访问 ...