MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集。
MyBatis 原本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis ,2013年11月迁移到Github。
MyBatis 和 Hibernate 都是优秀的持久化框架,都支持JDBC(Java DataBase Connection)和JTA(Java Transaction API)事务处理。
MyBatis 优点
Hibernate 优点
Mybatis集成方式分为两种:
XML版本为老式的配置集成方式,重度集成XML文件,SQL语句也是全部写在XML中的;注解版版本,相对来说比较简约,不需要XML配置,只需要使用注解和代码来操作数据。
开发环境
MyBatis Spring Boot 是 MyBatis 官方为了集成 Spring Boot 而推出的MyBatis版本。
设置pom.xml文件,添加如下配置
体验AI代码助手
代码解读
复制代码
添加 MySQL 和 MyBatis 支持。
设置application.properties文件,添加如下配置
体验AI代码助手
代码解读
复制代码
# MyBatis 配置 spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.hello.springboot.mapper
Mapper文件说明
Mapper是MyBatis的核心,是SQL存储的地方,也是配置数据库映射的地方。
直接在启动文件SpringbootApplication.java的类上配置@MapperScan,这样就可以省去,单独给每个Mapper上标识@Mapper的麻烦。
体验AI代码助手
代码解读
复制代码
@SpringBootApplication @MapperScan("com.hello.springboot.mapper") public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
为了演示的简洁性,我们不做太多的分层处理了,我们这里就分为:实体类、Mapper接口、Controller类,使用Controller直接调用Mapper接口进行数据持久化处理。
User 类:
体验AI代码助手
代码解读
复制代码
public class User { private Long id; private String name; private int age; private String pwd; //省去set、get方法 }
UserMapper 接口:
体验AI代码助手
代码解读
复制代码
public interface UserMapper { @Select("select * from user") @Results({ @Result(property = "name", column = "name") }) List
可以看出来,所有的SQL都是写在Mapper接口里面的。
Mapper里的注解说明
Controller 控制器:
体验AI代码助手
代码解读
复制代码
@RestController @RequestMapping("/") public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/") public ModelAndView index() { User user = new User(); user.setAge(18); user.setName("Adam"); user.setPwd("123456"); userMapper.install(user); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("count", userMapper.getAll().size()); return modelAndView; } }
到此为止,已经完成了MyBatis项目的配置了,可以运行调试了。
注解版GitHub源码下载:github.com/vipstone/sp…
设置pom.xml文件,添加如下配置
体验AI代码助手
代码解读
复制代码
添加 MySQL 和 MyBatis 支持。
设置application.properties文件,添加如下配置
体验AI代码助手
代码解读
复制代码
# MyBatis 配置 spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.hello.springboot.entity mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
直接在启动文件SpringbootApplication.java的类上配置@MapperScan,这样就可以省去,单独给每个Mapper上标识@Mapper的麻烦。
体验AI代码助手
代码解读
复制代码
@SpringBootApplication @MapperScan("com.hello.springboot.mapper") public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
本示例设置两个xml文件,在resource/mybatis下的mybatis-config.xml(配置MyBatis基础属性)和在resource/mybatis/mapper下的UserMapper.xml(用户和数据交互的SQL语句)。
mybatis-config.xml
体验AI代码助手
代码解读
复制代码
UserMapper.xml
体验AI代码助手
代码解读
复制代码
SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):
cache
– 给定命名空间的缓存配置。cache-ref
– 其他命名空间缓存配置的引用。resultMap
– 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。parameterMap
– 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。sql
– 可被其他语句引用的可重用语句块。insert
– 映射插入语句update
– 映射更新语句delete
– 映射删除语句select
– 映射查询语句注意: MyBatis中 config 和 mapper 的 XML 头文件是不一样的。
config 头文件
体验AI代码助手
代码解读
复制代码
mapper 头文件
体验AI代码助手
代码解读
复制代码
Mapper XML 更多配置:www.mybatis.org/mybatis-3/z…
为了演示的便捷性,我们添加3个类用于功能的展示,分别是实体类User.java、mapper接口UserMapper.java和控制器类UserController.java,使用控制器类直接调用UserMapper的方法,进行数据存储和查询。
User.java
体验AI代码助手
代码解读
复制代码
package com.hello.springboot.entity; public class User { private Long id; private String name; private int age; private String pwd; //省略set/get方法 }
UserMapper.java
体验AI代码助手
代码解读
复制代码
package com.hello.springboot.mapper; import com.hello.springboot.entity.User; import java.util.List; public interface UserMapper { List
注意: Mapper里的方法名必须和Mapper XML里的一致,不然会找不到执行的SQL。
UserController.java
体验AI代码助手
代码解读
复制代码
@RestController @RequestMapping("/") public class UserController { @Resource private UserMapper userMapper; @RequestMapping("/") public ModelAndView index() { User user = new User(); user.setAge(18); user.setName("Adam"); user.setPwd("123456"); userMapper.insert(user); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("count", userMapper.findAll().size()); return modelAndView; } }
到此为止,已经完成了MyBatis项目的配置了,可以运行调试了。
XML版GitHub源码下载:github.com/vipstone/sp…
到目前为止我们已经掌握了MyBatis的两种集成方式,注解集成和XML集成,注解版更符合程序员的代码书写习惯,适用于简单快速查询;XML版可以灵活的动态调整SQL,更适合大型项目开发,具体的选择还要看开发场景以及个人喜好了。