mybatis 高级映射和spring整合之逆向工程(7)

mybatis 高级映射和spring整合之逆向工程(7)

4.0 逆向工程
    4.1 mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需的代码(mapper.java,mapper.xml,po..)

    企业实际开发中,常用的逆向工程方式:
    由数据库的表生成java代码。

    4.2 下载逆向工程
            mybatis-generator-core-1.3.2-bundle
    4.3 使用方法(会用)
        4.3.1 运行逆向工程(建议使用java程序方式,不依赖开发工具)
        4.3.2 生成代码配置文件

        <!-- targetProject:生成po类的位置 -->
        <javaModelGenerator targetPackage="com.demo.ssm.pc">
        ....
        <!-- targetProject:mapper映射文件生成的位置 -->
        <javaModelGenerator targetPackage="com.demo.ssm.mapper">
        ....
        <!-- targetProject:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER">
            targetPackage="com.demo.ssm.mapper"
            targetPackage=".\src">
        ....
        <!-- 指定数据库表 -->
        <table tableName="items"></table>
        <table tableName="orders"></table>
        <table tableName="orderdetail"></table>
        <table tableName="user"></table>
        4.3.3 执行生成程序
        List<string> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定逆向工程配置文件
        File configFile = new File("generatorConfig.xml");

        4.3.4 使用生成的文件
        需要将生成工程中所生成的代码拷贝到自己的工程中。

        测试itemsMapper中的方法

        //自定义条件查询
        @Test
        public void testSelectByExample(){
            ItemsExample itemsExample = new itemsExample.createCriteria();
            Criteria.andNameEqualTo("笔记本3");
            //可能返回多条记录
            List<items>list = itemsMapper.selectByExample(itemsExample);
            System.out.println(list);
        }
        //根据主键查询
        @Test
        public void testSelectByPrimaryKey(){
            Items items = itemsMapper.selectByPrimaryKey(1);
            System.out.println(items);
        }
        //更新数据
        @Test
        public void testUpdateByPrimaryKey(){
            //对所有字段进行更新,需要先查询出来再更新
            Items items = itemsMapper.selectByPrimaryKey(1);
            items.setName("水杯");
            itemsMapper.updateByPrimaryKey(items);
                //如果出入字段不为空才更新,在批量更新中使用此方法,不需要先查询再更新
                //itemsMapper.updateByPrimaryKeySelective(record);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值