引言
MyBatis 是一款优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。在Java开发中,MyBatis 的应用越来越广泛。本文将带你从入门到进阶,通过实战案例,让你轻松掌握MyBatis。
一、MyBatis入门
1.1 MyBatis简介
MyBatis 是一个半自动化的持久层框架,它使用 XML 或注解来配置和映射原生 SQL 到参数以及到对象的映射关系。MyBatis 可以将数据持久层的操作从业务逻辑中分离出来,使得代码更加简洁易读。
1.2 MyBatis环境搭建
- 添加依赖:在项目的
pom.xml文件中添加 MyBatis 的依赖。<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> - 配置数据库连接:在
application.properties或application.yml文件中配置数据库连接信息。db.url=jdbc:mysql://localhost:3306/mybatis_db db.user=root db.password=root db.driver=com.mysql.jdbc.Driver - 编写实体类:定义与数据库表对应的实体类。
public class User { private Integer id; private String name; private Integer age; // 省略getter和setter方法 } - 编写Mapper接口:定义 MyBatis 的 Mapper 接口,用于编写 SQL 语句。
public interface UserMapper { User getUserById(Integer id); } - 编写MyBatis配置文件:创建
mybatis-config.xml文件,配置数据源、事务管理器、Mapper 映射等。<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_db"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/UserMapper.xml"/> </mappers> </configuration> - 编写Mapper XML:创建
UserMapper.xml文件,配置 SQL 语句和结果映射。<mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper>
二、MyBatis进阶技巧
2.1 动态SQL
MyBatis 提供了动态 SQL 的功能,可以方便地实现复杂的 SQL 语句。
- 条件判断:使用
<if>标签实现条件判断。<select id="selectUsers" resultType="User"> SELECT * FROM users <where> <if test="name != null"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select> - 选择:使用
<choose>标签实现类似 switch-case 的功能。<select id="selectUsers" resultType="User"> SELECT * FROM users <where> <choose> <when test="name != null"> name = #{name} </when> <when test="age != null"> age = #{age} </when> <otherwise> name = 'unknown' </otherwise> </choose> </where> </select> - 循环:使用
<foreach>标签实现集合遍历。<update id="updateUserRoles" parameterType="map"> UPDATE users SET roles = #{roles} WHERE id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </update>
2.2 一对一、一对多、多对多关联
- 一对一关联:使用
<resultMap>标签实现一对一关联。<resultMap id="userRoleMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <association property="role" javaType="Role"> <id property="id" column="role_id"/> <result property="name" column="role_name"/> </association> </resultMap> - 一对多关联:使用
<collection>标签实现一对多关联。<resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <collection property="roles" ofType="Role"> <id property="id" column="role_id"/> <result property="name" column="role_name"/> </collection> </resultMap> - 多对多关联:使用
<collection>标签实现多对多关联。<resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> <result property="price" column="price"/> <collection property="items" ofType="Item"> <id property="id" column="item_id"/> <result property="name" column="item_name"/> <result property="price" column="item_price"/> </collection> </collection> </resultMap>
三、项目实战案例分享
3.1 用户管理系统
- 功能模块:用户管理、角色管理、权限管理、部门管理、日志管理。
- 技术栈:Spring Boot、MyBatis、MySQL、Thymeleaf。
- 实现步骤:
- 创建 Spring Boot 项目,添加 MyBatis 依赖。
- 配置数据库连接信息。
- 创建实体类、Mapper 接口和 Mapper XML。
- 编写控制器和视图层。
- 实现业务逻辑。
3.2 在线商城
- 功能模块:商品管理、订单管理、购物车、用户管理、评论管理。
- 技术栈:Spring Boot、MyBatis、MySQL、Redis、Elasticsearch。
- 实现步骤:
- 创建 Spring Boot 项目,添加 MyBatis 依赖。
- 配置数据库连接信息。
- 创建实体类、Mapper 接口和 Mapper XML。
- 编写控制器和视图层。
- 实现业务逻辑。
- 集成 Redis 缓存和 Elasticsearch 搜索功能。
结语
通过本文的学习,相信你已经对 MyBatis 有了一定的了解。在实际项目中,MyBatis 可以帮助你快速开发出高性能、可扩展的数据持久层。希望本文能对你有所帮助,祝你学习愉快!
