MyBatis简介
MyBatis是一款优秀的Java持久层框架,它消除了几乎所有的JDBC代码和手动设置参数以及获取结果集的过程。MyBatis可以让我们用更简洁的代码完成数据库的CRUD操作,并且支持自定义SQL、存储过程以及高级映射。
MyBatis入门
1. MyBatis核心组件
MyBatis的核心组件包括:
- SqlSessionFactory:用于创建SqlSession对象,是MyBatis的入口。
- SqlSession:用于执行数据库操作,是MyBatis的工作单元。
- Executor:负责执行SQL语句。
- MappedStatement:代表一个映射文件中的一个SQL语句节点。
2. MyBatis配置
MyBatis配置主要包括:
- 配置文件:mybatis-config.xml,定义了MyBatis的全局配置。
- 映射文件:XXXMapper.xml,定义了具体的SQL语句和映射关系。
3. MyBatis基本使用
(1)创建SqlSessionFactory
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
(2)获取SqlSession
SqlSession session = sqlSessionFactory.openSession();
(3)执行SQL语句
try {
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectById(1);
System.out.println(user.getUsername());
} finally {
session.close();
}
MyBatis高级应用
1. 动态SQL
MyBatis支持动态SQL,可以根据条件动态构建SQL语句。
<select id="selectByCondition" parameterType="map" resultType="User">
SELECT * FROM user
<where>
<if test="username != null">
AND username = #{username}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
2. 实体映射
MyBatis支持实体映射,可以将数据库中的数据直接映射到Java对象中。
public interface UserMapper {
User selectById(@Param("id") Integer id);
}
3. 关联查询
MyBatis支持关联查询,可以方便地查询多表数据。
<select id="selectUserById" resultMap="userMap">
SELECT * FROM user WHERE id = #{id}
<resultMap id="userMap" type="User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="age" column="age" />
<collection property="orders" column="id" select="selectOrderById" />
</resultMap>
</select>
MyBatis实战应用
1. 实战案例:用户管理系统
在这个案例中,我们将使用MyBatis实现一个简单的用户管理系统,包括用户注册、登录、查询等功能。
(1)创建实体类
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
// ... getter和setter方法
}
(2)创建Mapper接口
public interface UserMapper {
void register(User user);
User login(User user);
User selectById(Integer id);
}
(3)创建Mapper映射文件
<mapper namespace="com.example.mapper.UserMapper">
<insert id="register" parameterType="User">
INSERT INTO user(username, password, age) VALUES(#{username}, #{password}, #{age})
</insert>
<select id="login" parameterType="User" resultType="User">
SELECT * FROM user WHERE username = #{username} AND password = #{password}
</select>
<select id="selectById" parameterType="Integer" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
2. 实战案例:商品管理系统
在这个案例中,我们将使用MyBatis实现一个商品管理系统,包括商品增删改查等功能。
(1)创建实体类
public class Product {
private Integer id;
private String name;
private Double price;
// ... getter和setter方法
}
(2)创建Mapper接口
public interface ProductMapper {
void addProduct(Product product);
void deleteProduct(Integer id);
void updateProduct(Product product);
Product selectProductById(Integer id);
}
(3)创建Mapper映射文件
<mapper namespace="com.example.mapper.ProductMapper">
<insert id="addProduct" parameterType="Product">
INSERT INTO product(name, price) VALUES(#{name}, #{price})
</insert>
<delete id="deleteProduct" parameterType="Integer">
DELETE FROM product WHERE id = #{id}
</delete>
<update id="updateProduct" parameterType="Product">
UPDATE product SET name = #{name}, price = #{price} WHERE id = #{id}
</update>
<select id="selectProductById" parameterType="Integer" resultType="Product">
SELECT * FROM product WHERE id = #{id}
</select>
</mapper>
总结
MyBatis是一款功能强大的Java持久层框架,可以帮助我们快速开发出高效的数据库应用程序。通过本文的介绍,相信你已经对MyBatis有了基本的了解。在实际项目中,你可以根据自己的需求灵活运用MyBatis的特性,提高开发效率。
