在多模块中搭建MyBatis开发环境

一、以操作Oracle数据库为例,建立五个模块,MyBatisGenerator、DAO、Service、Web、Parent、均为maven-archetype-quickstart类型。

二、Parent为父模块,

<packaging>pom</packaging>
 <parent>
    <!-- 从SpringBoot继承默认的配置 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.4</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 引入SpringBoot对web开发的依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 <!-- 解决下载JSP文件而不是打开的问题 -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.5</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.13.5</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.13.5</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.33</version>
    </dependency>

    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>4.0.3</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>3.0.2</version>
    </dependency>

    <dependency>
      <groupId>cn.easyproject</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.4</version>
    </dependency>
  </dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
          <include>**/**</include>
        </includes>
      </resource>
    </resources>
  </build>

三、MyBatisGenerator模块继承子parent父模块,关键步骤是创建generatorConfig.xml文件,对UserinfoMapper.java接口使用@Mapper注解,进行逆向操作,generatorConfig.xml文件放在resource文件夹中,代码如下:

<?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>
    <classPathEntry location="D:\app\cuihao\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar"/>
    <context id="context1" targetRuntime="MyBatis3Simple">
        <jdbcConnection
                connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
                driverClass="oracle.jdbc.OracleDriver" password="xxxxxxx" userId="xxxx"/>
        <javaModelGenerator targetPackage="com.ch.www.entity"
                            targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.ch.www.sqlmapping"
                         targetProject="src/main/java"/>
        <javaClientGenerator targetPackage="com.ch.www.sqlmapping"
                             targetProject="src/main/java" type="XMLMAPPER"/>
        <table schema="xxxxxx" tableName="userinfo">
            <generatedKey column="id"
                          sqlStatement="select idauto.nextval from dual" identity="false"/>
        </table>
    </context>
</generatorConfiguration>

四、DAO模块、Service模块、Web模块连续继承或者继承parent父模块即可,代码此处省略,Dao模块用来封装数据,Service处理业务,Web模块在MyBatis中常与DAO模块和Service模块协同工作,通过DAO模块与数据库进行数据交互,通过Service模块处理业务逻辑。Web模块负责与前端进行交互,并将数据传递给Service层或DAO层进行处理。

Web模块中需要有yml文件来定义属性并且连接数据库:

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    hikari:
      auto-commit: true
      connection-test-query: SELECT 1 from dual
      connection-timeout: 30000
      idle-timeout: 30000
      max-lifetime: 1800000
      maximum-pool-size: 15
      minimum-idle: 5
      pool-name: MyHikariCP
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:oracle:thin:@localhost:1521:orcl
    username: y2
    password: 123123

server:
  servlet:
    encoding:
      charset: utf-8
      enabled: true
      force: true

五、使用Mapper.xml文件中定义的SQL语句,需要创建一个对应的接口,并将其与Mapper XML文件进行关联,Mapper.xml文件一般是逆向操作后自动生成的,除了XML文件,MyBatis还支持使用注解来编写SQL语句。通过在Java接口的方法上添加@Select、@Insert、@Update、@Delete等注解,可以根据项目的需求和个人偏好选择使用XML文件或注解来控制SQL语句。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值