MyBatis从前世今生一网打尽(全网最全,建议收藏)5️⃣

网友投稿 669 2022-05-30

八、Mybatis 逆向工程

8.1、逆向工程简介

MyBatis Generator: 简称 MBG,是一个专门为 MyBatis 框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及 bean 类。支持基本的增删改查,以及 QBC 风格的条件查询。但是表连接、存储过程等这些复杂 sql 的定义需要我们手工编写

官方文档地址

http://www.mybatis.org/generator/

官方工程地址

https://github.com/mybatis/generator/releases

8.2、逆向工程的配置

导入逆向工程的 jar 包:mybatis-generator-core-1.3.2.jar

编写 MBG 的配置文件(重要几处配置),可参考官方手册

编写java代码运行

@Test public void testMBG() throws Exception { List warnings = new ArrayList(); boolean overwrite = true; File configFile = new File("mbg.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }

8.3、逆向工程的使用

基本查询的测试

@Test public void testSelect() throws Exception { SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession(); try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); List emps = mapper.selectAll(); for (Employee employee : emps) { System.out.println(employee); } } finally { session.close(); } }

带条件查询的测试

@Test public void testSelect() throws Exception { SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession(); try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); //条件查询: 名字中带有'张' 并且 email中'j' 或者 did = 2 EmployeeExample example = new EmployeeExample(); Criteria criteria = example.createCriteria(); criteria.andLastNameLike("%张%"); criteria.andEmailLike("%j%"); //or Criteria criteriaOr = example.createCriteria(); criteriaOr.andDIdEqualTo(2); example.or(criteriaOr); List emps = mapper.selectByExample(example); for (Employee employee : emps) { System.out.println(employee); } } finally { session.close(); } }

#九、扩展-PageHelper 分页插件

9.1、PageHelper 分页插件简介

PageHelper 是 MyBatis 中非常方便的第三方分页插件,官方文档:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md

9.2、PageHelper 的使用步骤

导入相关包 pagehelper-x.x.x.jar 和 jsqlparser-0.9.5.jar

在 MyBatis 全局配置文件中配置分页插件

使用 PageHelper 提供的方法进行分页

可以使用更强大的 PageInfo 封装返回结果

9.3、Page 对象的使用

在查询之前通过 PageHelper.startPage(页码,条数)设置分页信息,该方法返回 Page 对象

@Test public void testPageHelper() throws Exception{ SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession(); try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); //设置分页信息 Page page = PageHelper.startPage(9, 1); List emps = mapper.getAllEmps(); for (Employee employee : emps) { System.out.println(employee); } System.out.println("=============获取分页相关的信息 ================="); System.out.println("当前页: " + page.getPageNum()); System.out.println("总页码: " + page.getPages()); System.out.println("总条数: " + page.getTotal()); System.out.println("每页显示的条数: " + page.getPageSize()); } finally { session.close(); } }

9.4、PageInfo 对象的使用

在查询完数据后,使用 PageInfo 对象封装查询结果,可以获取更详细的分页信息以及可以完成分页逻辑

@Test public void testPageHelper1() throws Exception{ SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession(); try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); //设置分页信息 Page page = PageHelper.startPage(9, 1); List emps = mapper.getAllEmps(); // PageInfo info = new PageInfo<>(emps,5); for (Employee employee : emps) { System.out.println(employee); } System.out.println("=============获取详细分页相关的信息 ================="); System.out.println("当前页: " + info.getPageNum()); System.out.println("总页码: " + info.getPages()); System.out.println("总条数: " + info.getTotal()); System.out.println("每页显示的条数: " + info.getPageSize()); System.out.println("是否是第一页: " + info.isIsFirstPage()); System.out.println("是否是最后一页: " + info.isIsLastPage()); System.out.println("是否有上一页: " + info.isHasPreviousPage()); System.out.println("是否有下一页: " + info.isHasNextPage()); System.out.println("============分页逻辑==============="); int [] nums = info.getNavigatepageNums(); for (int i : nums) { System.out.print(i +" " ); } } finally { session.close(); } }

十、SSM 框架整合

10.1、整合注意事项

查看不同 MyBatis 版本整合 Spring 时使用的适配包;

下载整合适配包:https://github.com/mybatis/spring/releases

官方整合示例,jpetstore:https://github.com/mybatis/jpetstore-6

10.2、整合思路、步骤

搭建环境

创建一个动态的 WEB 工程,导入 SSM 需要使用的 jar 包,导入整合适配包,导入其他技术的一些支持包 连接池 数据库驱动 日志…

Spring + Springmvc

在web.xml 中配置: Springmvc 的前端控制器 实例化Spring 容器的监听器 ,字符编码过滤器 REST 过滤器

创建 Spring 的配置文件: applicationContext.xml:组件扫描、 连接池、 事务…

创建 Springmvc 的配置文件: springmvc.xml : 组件扫描、 视图解析器

MyBatis

创建 MyBatis 的全局配置文件

编写实体类 Mapper 接口 Mapper 映射文件

Spring + MyBatis

MyBatis 的 SqlSession 的创建 .

MyBatis 的 Mapper 接口的代理实现类

测试

10.3、整合的配置

10.3.1、web.xml

CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /* HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter /* contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener springDispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 springDispatcherServlet /

10.3.2、Spring 配置

10.3.3 SpringMVC 配置

10.3.4、MyBatis 配置

MyBatis从前世到今生一网打尽(全网最全,建议收藏)5️⃣

全局文件的配置

SQL 映射文件配置

Spring 整合 MyBatis 配置

测试

MyBatis Spring

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口自动化测试--requests请求+unittest框架封装
下一篇:【软考】2020年全国计算机技术与软件专业技术资格考试,网络工程师(中级),考纲
相关文章