使用 mybatis 框架进行增删改查大致有两种基础方式, 一种扩展方式. 两种基础方式分别是使用 xml 映射文件和使用方法注解. 扩展方式是使用 mybatis-plus 的方式, 其用法类似于 spring-data-jpa.
1. 引入依赖
- <!--springboot 的 web 起步依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <!-- Mybatis 起步依赖 -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.0</version>
- </dependency>
- <!-- MySql 连接驱动 -->
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
2.entity 实体类
- @TableName(value = "tb_user")// 使用 mybatis-plus 时需要添加, 与数据库中的表建立关系
- public class SysUser implements Serializable {
- @TableId(type = IdType.AUTO)// 使用 mybatis-plus 时需要添加, 主键生成策略
- private Long id;
- private String username;
- private String password;
- private String salt;
- private String avatar;
- private String introduce;
- private String remark;
- // 省略 get,set 方法
- }
3. 创建 application.properties 配置文件
- #DB Configuration 数据库信息:
- spring.datasource.driverClassName=com.MySQL.jdbc.Driver
- spring.datasource.url=jdbc:MySQL:///test
- spring.datasource.username=root
- spring.datasource.password=root
- spring.datasource.type=com.zaxxer.hikari.HikariDataSource
- #spring 集成 Mybatis 环境
- #pojo 别名扫描包
- mybatis.type-aliases-package=cn.zhq.system.entity
- # 加载 Mybatis 映射文件
- mybatis.mapper-locations=classpath:mapper/*Mapper.xml
- # 打印 sql
- # 打印 sql 到控制台
- logging.level.cn.zhq.system.mapper=debug
mybatis.type-aliases-package 是实体所在包的位置, mybatis.mapper-locations 是配置文件所在的位置. 初次使用的时候最好加上打印 sql 的配置, 如果出现错误的话, 这样容易找出错误原因.
4. 创建 UserMapper 类
- @Mapper
- public interface UserMapper {
- }
5. 在 spring boot 的 application 加上注解
@EntityScan("cn.zhq.system.entity")// 填写 entity 类所在包的位置.
加上这个注解后, springboot 就可以扫描能实体类.
6. 进行增删改查
6.1 使用 mapper.xml 进行增删改查
6.1.1 编写 UserMapper 类中的方法
- /**
- * 根据 Name 查询用户数据
- *
- * @param username
- * @return
- */
- SysUser findByName(String username);
6.1.2 在 resources 的 mapper 目录下创建 UserMapper.xml 配置文件
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="cn.zhq.system.mapper.UserMapper">
- <select id="findByName" resultType="sysuser" parameterType="String">
- select * from tb_user where username = #{username}
- </select>
- </mapper>
使用 select 标签执行查询语句, 使用 update 标签进行 增, 删, 改操作. 其中 id 是 UserMapper 类中方法的名称, resultType 是返回值的类型, parameterType 是接收的参数类型.
6.2 使用注解进行增删改查
- /**
- * 更新用户名
- * @param username
- */
- @Update("update tb_user set username= #{username} where id = #{id}")
- void updateByAvatar(String username,Long id);
使用 @Update 注解后, 就无需再编写 UserMapper 配置文件了, 同理 @Updae 可以进行增删改操作. 此外使用 @Select 进行查询. 当进行一对多, 多对多关系的查询时, 还可以使用 @Results 注解指定返回结果的数据类型, 这里就不细说了.
以上两种方式各有利弊, 使用配置文件编写 sql 语句好处是耦合度低但过于麻烦, 使用注解的方式相对于配置文件来说比较简单, 但是耦合度高. 如果是简单语句, 建议是使用注解, 如果是复杂语句, 使用配置文件的方式. 具体使用哪种方式, 可以视实际情况而定.
6.3 使用 mybatis-plus 进行增删改查
mybatis-plus 类似于 spring data jpa , 内置查询, 更新, 删除, 插入语句, 使我们无需再写那些简单的 sql 语句.
6.3.1 引入依赖
- <!-- Mybatis Plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.2.0</version>
- </dependency>
6.3.2 application.YAML 配置文件
- # mybatis-plus
- mybatis-plus:
- type-aliases-package: cn.zhq.system.entity
- mapper-locations: classpath:mapper/*.xml
- configuration:
- jdbc-type-for-null: null
- global-config:
- banner: false
type-aliases-package 填写 Entity 实体类所在包的位置. mapper-locations: 填写映射文件所在的位置, 如果不写的话, 在你的 resources 下的 mapper 配置文件会报错.
6.3.3 相关 service,serviceImpl,mapper 类
- service:
- public interface UserService extends IService<SysUser> {
- /**
- * 更新
- *
- * @param sysUser
- */
- void update(SysUser sysUser);
- }
- serviceImpl:
- @Service
- public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {
- @Autowired
- private UserMapper userMapper;
- @Override
- @Transactional
- public void update(SysUser sysUser) {
- userMapper.updateById(sysUser);
- }
- }
- mapper:
- @Mapper
- public interface UserMapper extends BaseMapper<SysUser> {
- }
虽然我们在 UserMapper 中没有定义任何方法, 但是 mybatisPlus 集成了很多方法可供我们使用.
执行后, 打印的 sql 语句结果如下
来源: https://www.cnblogs.com/Code-Handling/p/12047735.html