正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的。
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
<!-- 依赖MyBatis核心包 --> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> </dependencies> <!-- 控制Maven在构建过程中相关配置 --> <build> <!-- 构建过程中用到的插件 --> <plugins> <!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <!-- 插件的依赖 --> <dependencies> <!-- 逆向工程的核心依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency> </dependencies> </plugin> </plugins> </build>
文件名必须是:generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- targetRuntime: 执行生成的逆向工程的版本 MyBatis3Simple: 生成基本的CRUD(清新简洁版) MyBatis3: 生成带条件的CRUD(奢华尊享版) --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 数据库的连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="1234"> </jdbcConnection> <!-- javaBean的生成策略--> <javaModelGenerator targetPackage="bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- SQL映射文件的生成策略 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- Mapper接口的生成策略 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cmapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 逆向分析的表 --> <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName --> <!-- domainObjectName属性指定生成出来的实体类的类名 --> <table tableName="t_emp" domainObjectName="Emp"/> <table tableName="t_dept" domainObjectName="Dept"/> </context> </generatorConfiguration>
选择性修改添加,如果修改为null,则对应的值为=不做修改
@Test public void testMBG() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); EmpExample empExample = new EmpExample(); //创建条件对象,通过andXXX方法为SQL添加查询添加,每个条件之间是and关系 empExample.createCriteria().andEnameLike("a").andAgeGreaterThan(20).andDidIsNot Null(); //将之前添加的条件通过or拼接其他条件 empExample.or().andSexEqualTo("男"); List<Emp> list = mapper.selectByExample(empExample); for (Emp emp : list) { System.out.println(emp); } }
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency>
在MyBatis的核心配置文件中配置插件
<plugins> <!--设置分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
@Test public void test() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("myBatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = factory.openSession(true); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); //在查询功能之前使用PageHelper.startPage(int pageNum, int pageSize)开启分页功能 //pageNum:当前页的页码 pageSize:每页显示的条数 Page<Object> objects = PageHelper.startPage(1, 3); //输出一: System.out.println(objects); List<Emp> emps = mapper.selectByExample(null); PageInfo<Emp> pageInfo = new PageInfo<>(emps,3); //输出二: emps.forEach(System.out::println); //输出三: System.out.println(pageInfo); }
//输出一 Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=0, pages=0, reasonable=null, pageSizeZero=null}[] //输出二 Emp{eid=1, empName='2', age=23, sex='null', email='null', did=2} Emp{eid=2, empName='李四', age=32, sex='女', email='123@qq.com', did=3} Emp{eid=3, empName='王五', age=43, sex='女', email='123@qq.com', did=2} //输出三 PageInfo{ pageNum=1, pageSize=3, size=3, startRow=1, endRow=3, total=11, pages=4, list=Page{ count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=11, pages=4, reasonable=false, pageSizeZero=false} [Emp{eid=1, empName='2', age=23, sex='null', email='null', did=2}, Emp{eid=2, empName='李四', age=32, sex='女', email='123@qq.com', did=3}, Emp{eid=3, empName='王五', age=43, sex='女', email='123@qq.com', did=2} ], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=3, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3] }
pageNum:当前页的页码 pageSize:每页显示的条数 size:当前页显示的真实条数 total:总记录数 pages:总页数 prePage:上一页的页码 nextPage:下一页的页码 isFirstPage/isLastPage:是否为第一页/最后一页 hasPreviousPage/hasNextPage:是否存在上一页/下一页 navigatePages:导航分页的页码数 navigatepageNums:导航分页的页码,[1,2,3,4,5]