Java的MyBatis快速入门和实战详解
什么是MyBatis
MyBatis 是一款轻量级的 Java 持久层框架。 它支持自定义 SQL、存储过程以及高级映射。MyBatis 通过简化 JDBC 编程来实现对数据库的操作,并将 SQL 语句与程序代码分离,使应用程序的开发和维护更加简单。
MyBatis快速入门
环境搭建
- 安装 JDK
- 安装 Maven
- 创建 Maven 项目
Maven 配置
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
数据库配置
使用 MyBatis 操作数据库需要在 mybatis-config.xml
中配置数据库连接信息和映射文件路径。配置示例如下:
<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/testdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/BookMapper.xml"/>
</mappers>
</configuration>
映射文件编写
MyBatis 通过自定义 XML 配置文件进行数据操作。映射文件中主要包含了要执行的 SQL 语句、参数映射、结果映射等内容。
以下示例演示了如何编写一个查询 SQL 的映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.BookMapper">
<select id="getBookById" resultType="com.example.model.Book">
SELECT * FROM book WHERE book_id = #{bookId}
</select>
</mapper>
Java 代码编写
在 Java 代码中创建并使用 MyBatis 的 SQL 会话工厂来执行 SQL 命令。下面的示例展示如何在 Java 代码中调用查询 SQL:
public class BookDao {
private SqlSessionFactory sqlSessionFactory;
public BookDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Book getBookById(int bookId) {
try (SqlSession session = sqlSessionFactory.openSession()) {
return session.selectOne("com.example.mapper.BookMapper.getBookById", bookId);
}
}
}
MyBatis实战详解
使用 MyBatis 进行增删改查
在 MyBatis 中,增删改查对应的 SQL 语句分别是 insert、update、delete 和 select,每个 SQL 对应一个映射文件。
以下示例演示了如何编写和调用 insert SQL:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.BookMapper">
<insert id="addBook" parameterType="com.example.model.Book">
INSERT INTO book (book_id, book_name, author, price, invent_date)
VALUES (#{bookId}, #{bookName}, #{author}, #{price}, #{inventDate})
</insert>
</mapper>
public class BookDao {
private SqlSessionFactory sqlSessionFactory;
public BookDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public void addBook(Book book) {
try (SqlSession session = sqlSessionFactory.openSession()) {
session.insert("com.example.mapper.BookMapper.addBook", book);
session.commit();
}
}
}
使用 MyBatis 进行动态 SQL
MyBatis 提供了一套强大的动态 SQL 语言来帮助我们编写灵活的 SQL 语句。使用动态 SQL 可以根据不同的条件组合生成不同的 SQL 语句。
以下示例演示了如何使用动态 SQL 编写查询 SQL:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.BookMapper">
<select id="getBookList" resultType="com.example.model.Book">
SELECT * FROM book
<where>
<if test="bookId != null">
AND book_id = #{bookId}
</if>
<if test="bookName != null">
AND book_name LIKE CONCAT('%', #{bookName}, '%')
</if>
</where>
</select>
</mapper>
public class BookDao {
private SqlSessionFactory sqlSessionFactory;
public BookDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public List<Book> getBookList(Integer bookId, String bookName) {
try (SqlSession session = sqlSessionFactory.openSession()) {
BookMapper mapper = session.getMapper(BookMapper.class);
return mapper.getBookList(bookId, bookName);
}
}
}
示例1:使用 MyBatis 进行分页查询
以下示例演示如何使用 MyBatis 进行分页查询:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.BookMapper">
<select id="getBookListByPage" resultType="com.example.model.Book">
SELECT * FROM book
<where>
<if test="bookName != null">
AND book_name LIKE CONCAT('%', #{bookName}, '%')
</if>
</where>
ORDER BY book_id
LIMIT #{start}, #{size}
</select>
</mapper>
public class BookDao {
private SqlSessionFactory sqlSessionFactory;
public BookDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public List<Book> getBookListByPage(String bookName, int start, int size) {
try (SqlSession session = sqlSessionFactory.openSession()) {
BookMapper mapper = session.getMapper(BookMapper.class);
return mapper.getBookListByPage(bookName, start, size);
}
}
}
示例2:使用 MyBatis 进行一对多关联查询
以下示例演示如何使用 MyBatis 进行一对多关联查询:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.BookMapper">
<resultMap id="bookMap" type="com.example.model.Book">
<id property="bookId" column="book_id"/>
<result property="bookName" column="book_name"/>
<result property="author" column="author"/>
<result property="price" column="price"/>
<result property="inventDate" column="invent_date"/>
<collection property="bookItems" ofType="com.example.model.BookItem">
<id property="itemId" column="item_id"/>
<result property="itemName" column="item_name"/>
<result property="count" column="count"/>
</collection>
</resultMap>
<select id="getBookWithItemsById" resultMap="bookMap">
SELECT book.book_id, book.book_name, book.author, book.price, book.invent_date,
book_item.item_id, book_item.item_name, book_item.count
FROM book
LEFT JOIN book_item ON book.book_id = book_item.book_id
WHERE book.book_id = #{bookId}
</select>
</mapper>
public class BookDao {
private SqlSessionFactory sqlSessionFactory;
public BookDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Book getBookWithItemsById(int bookId) {
try (SqlSession session = sqlSessionFactory.openSession()) {
BookMapper mapper = session.getMapper(BookMapper.class);
return mapper.getBookWithItemsById(bookId);
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java的MyBatis快速入门和实战详解 - Python技术站