Java web项目集成Mybatis

  • 配置pom文件


      mysql
      mysql-connector-java
      8.0.11



      com.alibaba
      druid
      1.1.3



      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.3.1





  org.mybatis.generator
  mybatis-generator-maven-plugin
  1.3.5
  
    
      org.mybatis.generator
      mybatis-generator-core
      1.3.5
    
    
      mysql
      mysql-connector-java
      8.0.11
    
  
  
    
      mybatis generator
      package
      
        generate
      
    
  
  
    true
    
    true
    
      src/main/resources/mybatis-generator.xml
    
  

  • resources文件夹下面新建mapping文件夹
  • 在application.properties中增加配置
mybatis.mapper-locations=classpath:mapping/*.xml
  • 编写mybatis-generator.xml,注意生成dataobject、dao、mapper路径需要根据自己的项目路径进行配置,在项目的com.***project下新建dao和dataobject文件夹
  • xml文件中table表示要生成的表的信息,按照需求自己进行编写



<generatorConfiguration>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/***?useSSL=false"
                        userId="***"
                        password="***">
        jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.***project.dataobject" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mapping"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.***project.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>

        <table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
        <table tableName="user_password" domainObjectName="UserPsswordDO" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>

    context>
generatorConfiguration>
  • 点解IDEA的run功能,配置mybatis生成命令,然后运行,生成项目所需代码
    Java web项目集成Mybatis_第1张图片
  • 编写application.properties文件
#数据库名称
spring.datasource.name=***
spring.datasource.url=jdbc:mysql://localhost:3306/***?useSSL=false
spring.datasource.username=root
spring.datasource.password=***

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
  • 最后通过注解实现mybatis相关配置扫描
@SpringBootApplication(scanBasePackages = {"com.***project"})
//构建controller
@RestController
@MapperScan("com.***project.dao")
  • 最后新建Maven指令,选择执行的指令为pom文件中设置的指令,这里为mybatis-generator:generate,执行Maven指令,自动生成DoMapper和DataObject

你可能感兴趣的:(java)