前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mybatis 高级映射和spring整合之逆向工程(7)

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

作者头像
王小雷
发布2019-05-26 21:29:13
2720
发布2019-05-26 21:29:13
举报
文章被收录于专栏:王小雷王小雷

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

代码语言:javascript
复制
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);
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年10月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档