栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis的逆向工程的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis的逆向工程的使用

MyBatis的逆向工程
  • 简洁版 只有基本的crud
    • 导入pom依赖
    • 创建逆向工程的配置文件
    • 文件生成
  • 奢华版(带条件的crud)
    • 文件生成
    • 使用
    • 使用条件查询内容
    • 修改
    • 数据连接失败

简洁版 只有基本的crud

通过解析数据的表反向生成逆向文件

导入pom依赖


    4.0.0

    org.example
    MyBatis_MBG
    1.0-SNAPSHOT

    
        8
        8
    
    
    
        
            org.mybatis
            mybatis
            3.5.7
        
        
            log4j
            log4j
            1.2.17
        
        
            mysql
            mysql-connector-java
            5.1.38
        
    

    
     
         
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.0 
                 
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.2
                     
                    
                        com.mchange
                        c3p0
                        0.9.2
                     
                    
                        mysql
                        mysql-connector-java
                        5.1.8
                    
                
            
        
    


创建逆向工程的配置文件

文件名必须是:generatorConfig.xml
报红不需要管







    

        
        
        

        
        
            
        

        
        

            

            
        

        
        
            
            
        

        

        
            
            
        

        

        
文件生成

双击运行这个插件

会帮你生成好实体类和接口

奢华版(带条件的crud)






    

        
        
        

        
        
            
        

        
        

            

            
        

        
        
            
            
        

        

        
            
            
        

        

        
文件生成

双击运行这个插件

输出 success就是生成成功

使用

selectByExample 用条件来进行查询
selectByPrimaryKey 说明根据主键来查询
updateByExampleSelective 根据条件选择性修改
updateByExample 根据条件修改
updateByPrimaryKeySelective 根据主键选择性修改
updateByPrimaryKey 根据主键修改

package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.TeachMapper;
import com.atguigu.mybatis.pojo.Teach;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MBGtest {
    @Test
    public void testMBG(){
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession(true);
            TeachMapper mapper = sqlSession.getMapper(TeachMapper.class);
            //查询所有数据
            List list = mapper.selectByExample(null);//没有条件就是查询所有
            list.forEach(emp -> System.out.println(list));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

使用条件查询内容
package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.TeachMapper;
import com.atguigu.mybatis.pojo.Teach;
import com.atguigu.mybatis.pojo.TeachExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MBGtest {
    @Test
    public void testMBG(){
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession(true);
            TeachMapper mapper = sqlSession.getMapper(TeachMapper.class);
            //获取条件查询
            TeachExample teachExample = new TeachExample();
            //让你的某个字段等于 3
            //可以用链式结构一直往后and. and.and
            //如果有其他条件 如 : or方法来连接
            //teachExample.or().andDidIsNoNull 并且不为空
            teachExample.createCriteria().andTidEqualTo(3);
            //把条件放进去就会根据你的条件来获取返回内容
            List list = mapper.selectByExample(teachExample);//没有条件就是查询所有
            list.forEach(emp -> System.out.println(list));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

修改
           //根据主键修改 会匹配到相同的id然后修改
                mapper.updateByPrimaryKey(new Teach(3,"admin"));
                //选择性修改,如果属性值为null的内容不会变
                mapper.updateByPrimaryKeySelective(new Teach(3,null));
数据连接失败
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
无法创建到数据库服务器的连接。

1.检查数据库和pow里的jar版本是否一致
2.检查有没有在逆向工程插件中添加SQL依赖

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/895011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号