首页 > 其他 > 详细

13-MyBatis03(逆向工程)

时间:2019-08-06 01:08:30      阅读:143      评论:0      收藏:0      [点我收藏+]

MyBatis逆向工程

1.导入jar包

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.6</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!--逆向工程的包-->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>
<!--junit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

2. 创建generator.xml配置文件

1. 在classpath下,创建generator.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="mysqlTable" targetRuntime="MyBatis3">
    <commentGenerator>
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <property name="suppressAllComments" value="true" />
    </commentGenerator>
    <!-- 1.数据连接参数 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3306/mybatisday01"
                    userId="root"
                    password="123456">
        <!--是否去除同名表-->
        <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>

    <!-- 2.默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
            和 NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver >
        <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- 3.生成模型的位置 -->
    <!-- javaModelGenerator javaBean生成的配置信息
        targetProject:生成PO类的位置
        targetPackage:生成PO类的类名-->
    <javaModelGenerator targetPackage="com.rqy.domain" targetProject=".\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="true" />
        <!-- 从数据库返回的值是否清理前后的空格 -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!-- 4.targetProject:mapper映射文件生成的位置 -->
    <!-- sqlMapGenerator Mapper映射文件的配置信息
      targetProject:mapper映射文件生成的位置
      targetPackage:生成mapper映射文件放在哪个包下-->
    <sqlMapGenerator targetPackage="com.rqy.mapper"  targetProject=".\src\main\resources">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 5. targetPackage:mapper接口生成的位置 -->
    <!--
      javaClientGenerator 生成 Model对象(JavaBean)和 mapper XML配置文件 对应的Dao代码
      targetProject:mapper接口生成的位置
      targetPackage:生成mapper接口放在哪个包下

      ANNOTATEDMAPPER
      XMLMAPPER
      MIXEDMAPPER
   -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.rqy.mapper"
                         targetProject=".\src\main\java">
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 指定所有数据库表 -->

    <!--<table tableName="%"
           enableCountByExample="false"
           enableUpdateByExample="false"
           enableDeleteByExample="false"
           enableSelectByExample="false"
           enableInsert="false"
           enableDeleteByPrimaryKey="true"
           enableSelectByPrimaryKey="true"
           selectByExampleQueryId="false" ></table>-->

    <!-- 指定数据库表,要生成哪些表,就写哪些表,要和数据库中对应,不能写错! -->

    <!-- 6.要生成的表 -->
    <table tableName="items"
           enableCountByExample="false"
           enableUpdateByExample="false"
           enableDeleteByExample="false"
           enableSelectByExample="false"
           enableInsert="true"
           enableDeleteByPrimaryKey="true"
           enableSelectByPrimaryKey="true"
           selectByExampleQueryId="false"
           domainObjectName="Items"
    />
    <table tableName="orderdetail" domainObjectName="Orderdetail"/>
    <table tableName="orders" domainObjectName="Order"/>
    <table tableName="user" domainObjectName="User"/>
</context>
</generatorConfiguration>

3.使用java类来执行逆向工程

1. 需要导入mysql的驱动包和mybatis的逆向工程
* 该类放在java资源中就可以
public class Generator {
public static void main(String[] args)  throws Exception{
	List<String> warnings = new ArrayList<String>();
	boolean overwrite = true;
	File configFile = new File("config/generator.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);
}
}

4. 执行上面的java类

5. 测试CRUD

1. 测试案例:逆向工程提供了很多查询方法,可以不用写sql,这个根hibernate有点类似
public class MyTest {
SqlSession sqlSession=null;
SqlSessionFactory ssf=null;
@Before
public void before() throws IOException {
     ssf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
    sqlSession=ssf.openSession();
}
@After
public  void after(){
    sqlSession.commit();
    sqlSession.close();
}
//查询
@Test
public void test(){
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    UserExample userExample = new UserExample();

    userExample.createCriteria().andIdBetween(32,35);

    List<User> users = mapper.selectByExample(userExample);
    for (User user : users) {
        System.out.println(user);
    }
}
//删除
@Test
public void test2(){
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    UserExample userExample = new UserExample();

    userExample.createCriteria().andIdEqualTo(41);

    int i = mapper.deleteByExample(userExample);

    System.out.println(i);
}
//修改
@Test
public void test3(){
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    UserExample userExample = new UserExample();

    userExample.createCriteria().andIdEqualTo(33);//条件
    User user=new User();
    user.setId(33);
    user.setUsername("你好");
    user.setSex("2");
    user.setAddress("123");
    user.setBirthday(new Date());
    int i = mapper.updateByExample(user,userExample);

    System.out.println(i);
}
//插入
@Test
public void test4(){
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setId(41);
    user.setUsername("你好");
    user.setSex("2");
    user.setAddress("123");
    user.setBirthday(new Date());
    int i = mapper.insert(user);

    System.out.println(i);
}

}

13-MyBatis03(逆向工程)

原文:https://www.cnblogs.com/rqy0526/p/11306428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!