码迷,mamicode.com
首页 > 其他好文 > 详细

总结:mybatis自动映射生成代码

时间:2020-12-21 11:44:40      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:art   value   apu   uil   文件   directory   main   set   hsql   

一、mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
    <setting name="logImpl" value="LOG4J"/>
    <!--通过此设置可以免去UserMapper.xml中
    selectAll中列名取别名这一步骤
    select id,
            user_name,
            user_password,
            user_email,
            user_info,
            head_img ,
            create_time
        from sys_user
    -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
    <typeAliases>
        <package name="com.example.simple.model"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value=""/>
            </transactionManager>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://47.100.59.91:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="Aa12345&amp;_"/>
            </dataSource>

        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/CountryMapper.xml"></mapper>
        <mapper resource="mapper/UserMapper.xml"></mapper>
        <mapper resource="mapper/RoleMapper.xml"></mapper>
        <mapper resource="mapper/UserRoleMapper.xml"></mapper>
        <mapper resource="mapper/PrivilegeMapper.xml"></mapper>
        <mapper resource="mapper/RolePrivilegeMapper.xml"></mapper>

    </mappers>
    <!--mybatis多数据库支持的形式-->
    <!--<databaseIdProvider type="DB_VENDOR">
        <property name="SQL Server" value="sqlserver"/>
        <property name="DB2" value= "db2"/>
        <property name="Oracle Server" value="oracle"/>
        <property name="MySQL" value= "rnysql"/>
        <property name="PostgreSQL" value="postgresql"/>
        <property name="Derby" value= "derby"/>
        <property name="H2" value= "h2"/>
        <property name="HSQL" value= "hsqldb"/>

    </databaseIdProvider>-->
    
</configuration>

2. 导入依赖,pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>simple</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.soureEncoding>UTF-8</project.build.soureEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>

        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId> org.apache.cassandra</groupId>
            <artifactId>cassandra-all</artifactId>
            <version>0.8.1</version>

            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
<!--                <exclusion>-->
<!--                    <groupId>log4j</groupId>-->
<!--                    <artifactId>log4j</artifactId>-->
<!--                </exclusion>-->
            </exclusions>

        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
            <scope>test</scope>
        </dependency>
        <!--代码生成器-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.3</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>

        <!--<resources>
            <resource>
                <directory>src/main/resources/com/example/simple</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>

            </resource>
        </resources>-->

    </build>

</project>

3. log4j.properties配置

#全局配置
log4j.rootLogger = ERROR,stdout
#MyBatis日志配置
log4j.logger.com.example.simple.mapper = TRACE
#控制台输出配置、
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %5p [%t] - %m%n

4. generatorConfig.xml配置

详细代码见:https://i.cnblogs.com/posts/edit;postId=14139081

5. 在mysql中新建好对应数据库和表。

至此,结束。

总结:mybatis自动映射生成代码

标签:art   value   apu   uil   文件   directory   main   set   hsql   

原文地址:https://www.cnblogs.com/yeyuting/p/14142979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!