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 折 ...
随机推荐
- Python+selenium打开或关闭浏览器
Python+selenium打开或关闭浏览器 一.打开或关闭火狐浏览器 1. 初始化一个webdriver实例对象driver,然后打开和关闭firefox浏览器.要用selenium打 ...
- 文件存储 FileUtil FileBaseDto
package com.guige.base.fileutils; import com.alibaba.fastjson.JSONArray; import com.aliyun.oss.Servi ...
- phantom的使用
phantom页面加载 通过Phantomjs,一个网页可以被加载.分析和通过创建网页对象呈现,访问我的博客园地址:http://www.cnblogs.com/paulversion/p/83938 ...
- C#安装,启动,停止,卸载Windows服务
public class OptionServices { //安装服务 public static void InstallService(string filep ...
- iptables 概念 1
[ 实战笔记 -- iptables 概念 1 ] 一. 防火墙相关概念 # 从逻辑上讲,防火墙可以分为主机防火墙和网络防火墙. 1> 主机防火墙: 针对于单个主机进行防护 2> 网络防火 ...
- iOS学习笔记(十二)——iOS国际化
开发的移动应用更希望获取更多用户,走向世界,这就需要应用国际化,国际化其实就是多语言.这篇文章介绍Xcode4.5以后的国际化,包括应用名国际化和应用内容国际化.如果是Xcode4.5之前版本请参考. ...
- Redis 单机版本安装
亲装! 1.linux 系统镜像 redis 版本 使用redis-3.2.8.tar.gz(截止2017年4月的最新稳定版) 在安装之前先安装下redis 需要的环境 wget http://do ...
- 在执行save操作时候出现的诡异!
情景:有一份excel数据需要导入到数据库中,想了想就写了一个导入excel的小功能. .............................准备使用时候,就开搞了,擦! 什么鬼??? 全程无报错 ...
- dbUtils 工具类介绍
导包: commons-dbutils.jar 核心类: QueryRunner 常用方法: // 执行增,删,改语句, 返回影响的行数 int update(String sql,Object... ...
- 使用QFile进行文件操作(QFile可以使用FILE *指针,还必须指定AutoCloseHandle)
QFile类我我们提供了操作文件的常用功能.它是一种io设备,可以用来读写文本文件和二进制文件,也可以用来读写Qt的资源文件.QFile类可以单独使用,该类本身提供了read/write函数,但更方便 ...