springboot+mybatis配置及使用

配置:

1.首先在项目resources目录下创建mybatis-config.xml配置文件,并写入如下配置项.(是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射)

2.在配置中心或者模块application.properties中配置spring.datasource.xx(数据库连接配置drivername/url/username/password);并增加mybatis.mapper-locations=classpath:mapper/*.xml(指定SQL xml映射文件放在resources/mapper文件夹下),mybatis.type-aliases-package=com.myself.entity(指定实体类所在的包地址)

使用:

1.使用mybatisGenerator反向生成实体类/接口/及映射文件

这里使用maven的插件进行生成,步骤如下

a.在pom.xml中添加如下配置

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/GeneratorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>

              </plugin>

b.写GeneratorConfig.xml文件    <

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

<!--defaultModelType指定一个表只生成一个实体类-->

    <context id="test" targetRuntime="MyBatis3" defaultModelType="flat">

        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>  
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> 
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost/mytest" userId="root" password="123456">
            </jdbcConnection>
        <!-- 生成实体类的包名和位置 -->
        <javaModelGenerator targetPackage="com.myself.po"
            targetProject="src/main/java"/>    
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="mapper"
            targetProject="src/main/resources"/>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.myself.dao"  targetProject="src/main/java"/>
        
        <!-- 要生成哪些表 -->
        <table tableName="user" domainObjectName="user"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"

            selectByExampleQueryId="false"></table>

<!--为insert语句自动生成返回主键的语句-->

<generatedKey column="主键列名" sqlStatement="MySql"

  identity="true" />
    </context>

</generatorConfiguration>

c.双击maven的mybatis generator插件

猜你喜欢

转载自blog.csdn.net/qq_33315102/article/details/80030410