Mybatis --- 逆向工程
2018-02-27 21:55
274 查看
一、pom.xml
二、generatorConfig.xml逆向工程配置文件
三、逆向工程启动类
三、逆向工程测试类
<dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies>
二、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> <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> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="cn.mybatis.po" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="cn.mybatis.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.mybatis.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table schema="" tableName="user"></table> <table schema="" tableName="orders"></table> <table schema="" tableName="items"></table> <table schema="" tableName="orderdetail"></table> <!-- 有些表的字段需要指定java类型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration>
三、逆向工程启动类
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("generatorConfig.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); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
三、逆向工程测试类
package com.mybatis.mapper; import static org.junit.Assert.*; 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.mybatis.po.User; import com.mybatis.po.UserExample; import com.mybatis.po.UserExample.Criteria; public class UserMapperTest { private ApplicationContext applicationContext; @Before public void init() throws Exception { //初始化spring容器 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void testInsert() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); User user = new User(); user.setUsername("test"); user.setSex("1"); user.setBirthday(new Date()); user.setAddress("test"); userMapper.insert(user); } @Test public void testSelectByExample() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); UserExample example = new UserExample(); //添加查询条件 //Criteria criteria = example.createCriteria(); //criteria.andUsernameLike("%张%"); //执行查询 List<User> list = userMapper.selectByExample(example); for (User user : list) { System.out.println(user); } } @Test public void testSelectByPrimaryKey() { UserMapper userMapper = applicationContext.getBean(UserMapper.class); User user = userMapper.selectByPrimaryKey(10); System.out.println(user); } }
相关文章推荐
- Mybatis学习(14)-MyBatis的逆向工程生成代码
- MyBatis逆向工程
- mybatis的逆向工程使用实现
- 【MyBatis学习15】MyBatis的逆向工程生成代码
- mybatis逆向工程文件排序
- mybatis逆向工程
- Mybatis逆向工程的使用方法
- mybatis的逆向工程中创建的以Example类的作用
- oracle mysql 的mybatis 逆向工程中的分页查询的写法
- Mybatis 逆向工程 数据库生成mapper
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解
- 在IDEA中使用MyBatis Generator逆向工程生成代码
- mybatis 的逆向工程
- mybatis逆向工程生成的mapper.xml文件部分修改
- mybatis逆向工程生成xml映射文件
- Mybatis逆向工程构建项目实例.
- 【MyBatis框架】MyBatis的逆向工程生成代码,如何生成逆向工程
- Mybatis逆向工程
- Mybatis-逆向工程
- Mybatis逆向工程