Mybatis(十)之逆向工程

mac2025-10-20  5

逆向工程:使用官方网站的Mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和Mapper映射文件

导入逆向工程 在generatorConfig.xml中配置以下几点: 数据库连接的信息:驱动类、连接地址、用户名、密码pojo文件所在包路径Mapper所在的包路径修改要生成的数据库表 <?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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.lin.nixiang.pojo" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.lin.nixiang.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lin.nixiang.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table schema="" tableName="user"></table> <table schema="" tableName="orders"></table> <!-- 有些表的字段需要指定java类型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration> 执行main方法 得到相应的接口、映射文件和pojo

测试逆向工程所得文件

把生成的文件放入spring-maybatis集成环境修改applicationContext.xml <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage:配置映射包装扫描,多个包时用","或";"分隔 --> <!-- <property name="basePackage" value="com.lin.mapper" /> --> <property name="basePackage" value="com.lin.nixiang.mapper"></property> </bean> 测试类 package com.lin.test; import static org.junit.Assert.fail; import java.util.Date; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lin.nixiang.mapper.UserMapper; import com.lin.nixiang.pojo.User; import com.lin.nixiang.pojo.UserExample; import com.lin.nixiang.pojo.UserExample.Criteria; /* *@author linone */ public class UserMappernixiangTest { private ApplicationContext applicationContext; @Before public void init() { applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testInsertSelective() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); User record =new User(); record.setUsername("linone"); record.setBirthday(new Date()); userMapper.insertSelective(record); } @Test //测试模糊查询 public void testSelectByExample() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); UserExample example = new UserExample(); Criteria createCriteria = example.createCriteria(); createCriteria.andUsernameLike("%lin%"); createCriteria.andSexEqualTo("1"); //创建User对象扩展类,再创建Criteria 用来设置用户查询条件 List<User> list = userMapper.selectByExample(example); for (User user : list) { System.out.println(user); } } @Test //通过主键查询 public void testSelectByPrimaryKey() { UserMapper mapper = applicationContext.getBean(UserMapper.class); User user = mapper.selectByPrimaryKey(20); System.out.println(user); } }
最新回复(0)