Mybatis创建逆向工程的步骤

添加依赖和插件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>mybatisartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.9version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.2version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.27version>
        dependency>
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
    dependencies>
    
    <build>
        <pluginManagement> pluginManagement>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.0version>
                
                <dependencies>
                    
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.2version>
                    dependency>
                    
                    <dependency>
                        <groupId>com.mchangegroupId>
                        <artifactId>c3p0artifactId>
                        <version>0.9.2version>
                    dependency>
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.27version>
                    dependency>
                dependencies>
            plugin>
        plugins>

    build>



project>

创建MyBatis的核心配置文件

在resources下面写配置文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <typeAliases>
        <package name=""/>
    typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>
    <mappers>
        <package name=""/>
    mappers>
configuration>

创建逆向工程的配置文件

文件名必须是:generatorConfig.xml


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="DB2Tables" targetRuntime="MyBatis3Simple">
        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/bookdb"
                        userId="root"
                        password="Thm123456!">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="com.mybatis.pojo" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.mybatis.mapper"
                         targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.mybatis.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>
        
        
        
        <table tableName="t_user" domainObjectName="User"/>
        <table tableName="t_book" domainObjectName="Book"/>
    context>
generatorConfiguration>

如果出现报错:Exception getting JDBC Driver,可能是pom.xml中,数据库驱动配置不一致。
注意

mac 、liunx 的路径和 windows 的路径不一样

找到idea 旁边的maven
Mybatis创建逆向工程的步骤_第1张图片

结果:
Mybatis创建逆向工程的步骤_第2张图片
MyBatis Generator代码生成器自动生成.java.1文件,
如果想取消
解决办法:
在工程项目pom.xml中改为允许覆盖生成文件:

<configuration>
    
    <overwrite>trueoverwrite>
configuration>

Mybatis创建逆向工程的步骤_第3张图片

QBC

查询

  • selectByExample:按条件查询,需要传入一个example对象或者null;如果传入一个null,则表示没有条件,也就是查询所有数据
  • example.createCriteria().xxx:创建条件对象,通过andXXX方法为SQL添加查询添加,每个条件之间是and关系
  • example.or().xxx:将之前添加的条件通过or拼接其他条件
    Mybatis创建逆向工程的步骤_第4张图片
@Test public void testMBG() throws IOException {
	InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
	SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
	SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
	SqlSession sqlSession = sqlSessionFactory.openSession(true);
	EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
	EmpExample example = new EmpExample();
	//名字为张三,且年龄大于等于20
	example.createCriteria().andEmpNameEqualTo("张三").andAgeGreaterThanOrEqualTo(20);
	//或者did不为空
	example.or().andDidIsNotNull();
	List<Emp> emps = mapper.selectByExample(example);
	emps.forEach(System.out::println);
}

增改

  • updateByPrimaryKey:通过主键进行数据修改,如果某一个值为null,也会将对应的字段改为null
  • mapper.updateByPrimaryKey(new Emp(1,"admin",22,null,"[email protected]",3));

就是说 为null 值也会替换它

  • updateByPrimaryKeySelective()`:通过主键进行选择性数据修改,如果某个值为null,则不修改这个字段
  • mapper.updateByPrimaryKeySelective(new Emp(2,"admin2",22,null,"[email protected]",3));

当有null 值想插入时,原理的值不是null 这不会变成null。

你可能感兴趣的:(mybatis,mybatis)