mybatis3开始支持java注解,使用java注解可以替代xml配置文件,简化代码。下面来看一下怎么在spring boot中使用mybatis注解。

1 使用mybatis注解需要的配置。如下面的代码所示,使用@MapperScan来扫描注册mybatis数据库接口类,其中basePackages属性表明接口类所在的包,sqlSessionTemplateRef表明接口类使用的SqlSessionTemplate。如果项目需要配置两个数据库,@MapperScan也需要分别配置。

@Configuration
@MapperScan(basePackages="com.boot.test.mapper",sqlSessionTemplateRef="sqlSessionTemplate")
public class DatabaseConfig { @Bean
@ConfigurationProperties(prefix = "test.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
} @Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
} @Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

2 mybatis注解介绍
mybatis3支持多个java注解,下面介绍几个基本的注解。
@Insert

 @Insert("insert into t_user(name,age,gender) values (#{name},#{age},#{gender})")
public int addUser(User user) throws DataAccessException;

如上面的代码所示,使用@Insert注解可以在()中直接写sql语句,传参可以用#{}实现,其中name字段名直接对应User类中的name字段,肿么样,有没有很直观方便:)

@Select

@Select("select age,gender from t_user where name={name}")
public User queryUser(String name) throws DataAccessException;

@Select 注解所查询出的字段可以直接绑定到User类中对应的字段中,由于在类中可以import对应的类,免去了在xml中配置结果集的麻烦。

@SelectKey

 @SelectKey(statement = "select max(id) from t_id", before = true, resultType = long.class, keyProperty = "id")
@Insert("insert into t_user(id,name,age,gender) values (#{id},#{name},#{age},#{gender})")
public int addUser(User user) throws DataAccessException;

@SelectKey注解用于查询一下需要的前置字段,如上面的代码所示先查出id字段的值,然后插入到t_user表中,一般配合@Insert和@Update
注解使用。

参考链接:https://www.jianshu.com/p/b1cad28c34eb

Spring Boot集成Mybatis注解相关的更多相关文章

  1. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  2. spring boot集成mybatis(2) - 使用pagehelper实现分页

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  4. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  6. 详解Spring Boot集成MyBatis的开发流程

    MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...

  7. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  8. spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete

    前言 spring boot集成mybatis时只生成两个sql, 搞了一个早上,终于找到原因了 找了很多办法都没有解决, 最后注意到生成sql的时候打印了一句话: Cannot obtain pri ...

  9. spring boot 集成 Mybatis,JPA

    相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...

随机推荐

  1. Java 字符串(二)字符串常用操作

    一.连接字符串 1.连接多个字符串 使用“+”运算符可以实现连接多个字符串的功能.“+” 运算符可以连接多个运算符并产生一个 String 对象. 2.连接其他数据类型 字符串与其他基本数据类型进行连 ...

  2. consul:架构

    官方文档:https://www.consul.io/docs/internals/architecture.html

  3. Pandas 之 DataFrame 常用操作

    import numpy as np import pandas as pd This section will walk you(引导你) through the fundamental(基本的) ...

  4. 不知道密码情况下 进行docker数据库可视化连接

    1. 通过命令 docker inpect mysql容器id ,查询mysql容器的密码和绑定的端口号 2.通过vscode插件或者navicat等可视化工具,进行连接即可. 有问题请进群联系我,或 ...

  5. vue2.0 在页面中使用process获取全局路径的时候 报错 process is not defined

    如果是刚配置好的全局变量需要 重新启动一下vue才能通过proccess.env.xxx 获取到 如果想在html中使用 需要在data中声明一个变量 然后在vue生命周期中 将process.env ...

  6. Letsencrypt.org CA免费证书生成

    Letsencrypt.org CA免费证书使用 Let's 支持多中客户端,这里使用acme.sh客户端配置免费证书. acme.sh优点: github官方地址 纯用Shell(Unix外壳)语言 ...

  7. 【转】如何在 Linux 中查看可用的网络接口

    原文:https://www.cnblogs.com/qianpangzi/p/10563979.html 查看ubuntu系统当前的可用的网络接口.方法如下 -------------------- ...

  8. P1092 虫食算[搜索]

    这个式子是是由\(A\sim A+N\)组成的,那么\(A\sim A+N\)就只能等于\(0\sim N-1\),因此我们每次对\(A\sim A+N\)的取值做一个新的排列,然后judge一下当前 ...

  9. objc_object 与 NSObject

    objc_object 与 NSObject:同一个事物的不同表现形式.

  10. centos 7 开启外网方法端口方法

    firewall-cmd --zone=public --add-port=80/tcp --permanent  开启80端口 systemctl stop firewalld.service sy ...