我是靠谱客的博主 快乐羊,这篇文章主要介绍MyBatis框架学习(二)——查询、封装、事务管理以及分页查询1. 查询操作3. 封装3. 事务提交4. 分页查询,现在分享给大家,希望可以做个参考。

文章目录

  • 1. 查询操作
    • 1.1 查询全部
    • 1.2 查询单个
  • 3. 封装
  • 3. 事务提交
    • 3.1 手动提交事务
    • 3.2 自动提交
  • 4. 分页查询
    • 4.1 添加依赖
    • 4.2 配置插件
    • 4.3 分页示例

1. 查询操作

1.1 查询全部

  • DAO中定义查询全部的接口。
复制代码
1
2
3
4
5
6
public interface BookDAO { public int insertBook(Book book); public int deleteBook(int book_num); public List<Book> selectAll(); }
  • BookMapper.xml 文件中定义实现。
复制代码
1
2
3
4
<select id="selectAll"> select * from novels.books </select>
  • 如果不知道类中每个变量与数据库中每一列的对应关系,可以使用 resultMap 标签,写了resultMap 标签后,可以在查询中指定相关的 resultMap ,不写 resultMap 的话,就必须在 select 标签中写 resultType
复制代码
1
2
3
4
5
6
7
8
9
10
<resultMap id="BookMap" type="com.mr.things.Book"> <id column="num" property="num"/> <result column="name" property="book_name"/> <result column="author" property="author"/> </resultMap> <select id="selectAll" resultMap="BookMap"> select * from novels.books </select>

resultMap标签前面是数据库中的列名,后面是数据库中列名对应的类中的属性名。

  • 单元测试
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test public void selectAll() { try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(is); SqlSession sqlSession = factory.openSession(); BookDAO bookDAO = sqlSession.getMapper(BookDAO.class); List<Book> books = bookDAO.selectAll(); for(Book book : books){ System.out.println(book); } } catch (IOException e) { e.printStackTrace(); } }

1.2 查询单个

  • DAO中定义查询单个的接口。
复制代码
1
2
3
4
5
6
7
public interface BookDAO { public int insertBook(Book book); public int deleteBook(int book_num); public List<Book> selectAll(); public Book selectOne(int book_num); }
  • BookMapper.xml 文件中定义接口实现。
复制代码
1
2
3
4
<select id="selectOne" resultMap="BookMap"> select * from novels.books where num=#{num} </select>

resultMap标签前面是数据库中的列名,后面是数据库中列名对应的类中的属性名。

  • 单元测试
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Test @Test public void selectOne() { try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(is); SqlSession sqlSession = factory.openSession(); BookDAO bookDAO = sqlSession.getMapper(BookDAO.class); Book book = bookDAO.selectOne(3); System.out.println(book); } catch (IOException e) { e.printStackTrace(); } }

3. 封装

在包下建立一个 Utils 的包,放入我们封装的类,类中代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MyBatisUtil { private static SqlSessionFactory factory; private static final ThreadLocal<SqlSession> local = new ThreadLocal<SqlSession>(); static{ try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); factory = builder.build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getFactory(){ return factory; } private static SqlSession getSqlSession(boolean isAutoCommit){ SqlSession sqlSession = local.get(); if(sqlSession == null ){ sqlSession = factory.openSession(isAutoCommit); local.set(sqlSession); } return sqlSession; } //手动事务管理 public static SqlSession getSqlSession(){ return getSqlSession(false); } //自动事务提交 public static <T extends Object>T getMapper(Class<T> c){ SqlSession sqlSession = getSqlSession(true); return sqlSession.getMapper(c); } }

3. 事务提交

3.1 手动提交事务

  • sqlSession.commit();提交事务
  • sqlSession.rollback();事务回滚
    测试类中进行事务管理如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test public void insertStudent() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); //1.当我们获取sqlSession对象时,就默认开启了事务 try{ //通过会话获取DAO对象 StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class); //测试StudentDAO中的方法 Student student = new Student(0, "10005", "Lily", "女", 21); int i = studentDAO.insertStudent(student); //2.操作完成并成功之后,需要手动提交 sqlSession.commit(); }catch (Exception e){ //3.当操作出现异常,调用rollback进行回滚 sqlSession.rollback(); } }

3.2 自动提交

通过 SqlSessionFactory 调用 openSession 方法获取 SqlSession 对象时,可以通过参数设置事务是否自动提交:

  • 如果参数设置为 true ,表示自定提交事务: factory.openSession(true);
  • 如果参数设置为 false ,或者不设置参数,表示手动提交:factory.openSession();/factory.openSession(false);
    测试代码
复制代码
1
2
3
4
5
6
@Test public void deleteBook() { BookDAO studentDAO = MyBatisUtil.getMapper(BookDAO.class); int i = studentDAO.deleteBook(11); }

4. 分页查询

4.1 添加依赖

添加 pagehelper 依赖,添加示例:

复制代码
1
2
3
4
5
6
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency>

4.2 配置插件

在主配置文件 mybatis-config.xml 中对插件进行配置。

复制代码
1
2
3
4
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>

<plugins> 标签的位置在 <properties> 标签和 <environments> 标签之间。

4.3 分页示例

复制代码
1
2
3
4
5
6
7
8
9
@Test public void testListStudentsByPage() { BookDAO bookDAO = MyBatisUtil.getMapper(BookDAO.class); //sqlSession PageHelper.startPage(2,4); List<Book> books = bookDAO.selectAll(); PageInfo<Book> pageInfo = new PageInfo<Book>(books); //pageInfo中就包含了数据及分页信息 }

最后

以上就是快乐羊最近收集整理的关于MyBatis框架学习(二)——查询、封装、事务管理以及分页查询1. 查询操作3. 封装3. 事务提交4. 分页查询的全部内容,更多相关MyBatis框架学习(二)——查询、封装、事务管理以及分页查询1.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部