springBoot+mybatis+thymeleaf+oracle+maven搭建web项目

java 同时被 3 个专栏收录
13 篇文章 0 订阅
2 篇文章 0 订阅
1 篇文章 0 订阅

我用的开发是IDEA,所以首先建立spring-boot项目,这个在spring-boot中很简单

这是完成搭建后的目录结构,后面如果我说的文件不知道放在哪,可以返回看看

1.引入thymeleaf:在maven中pom.xml 中加入依赖

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

在applcation.yml(application.properties改变格式就行(百度))中加入:

spring:
  #thymeleaf模板配置
  #热部署文件,页面不产生缓存,及时更新
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    cache: false
  resources:
    chain:
      strategy:
        content:
          enabled: true
          paths: /**

2.引入oracle:首先因为版权等原因,Oracle是不能像mysql那样直接在maven中添加的!所以我采用自己下驱动的方法(当然也可以用自己安装的Oracle里面有驱动)首先oracle的ojbc.jar(我的jar上传了)。第一步:在命令界面添加命令:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.2.0 -Dpackaging=jar -Dfile=D:\app\ojdbc6-11.2.0.2.0.jar

注意:红色的地方就是你下的jar包的名字、版本和你放的地址。这个要匹配

如果显示bulid success就证明本地安装成功。然后就在pom.xml中添加

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.2.0</version>
</dependency>

这样就不会报红了。

3.引入mybatis:我采用的是mybatis mybatis-generator 代码自动生成工具(自动生成mapper.xml、dao、实体类)。maven添加依赖(加在<build><plugins> </plugins></build>里面)

<!-- mybatis generator 自动生成代码插件 -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.2.0</version>
        </dependency>
    </dependencies>
    <configuration>
        <overwrite>true</overwrite>
    </configuration>
</plugin>
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
</plugin>

然后按照这个弄:edit configuration

填好就可以apply后点ok。

然后在resources文件夹中添加自动生成文件的配置文件:generatorConfig.xml  (填写你的oracle地址,用户名密码,然后生成规则,按照文件夹的命名来改,红色是你自己的东西)

<?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>

    <context id="oraclegenerator" targetRuntime="MyBatis3Simple">

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@133.32.62.110:1521:UDPL"
                        userId="****"
                        password="****"/>

        <!--生成实体类-->
        <javaModelGenerator targetPackage="com.hrmanage.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成xml映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.hrmanage.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--生成dao类-->
        <javaClientGenerator targetPackage="com.hrmanage.dao" type="XMLMAPPER" targetProject="src/main/java">
            <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--需要产生实体类的表-->
        <table schema="" tableName="CADRE_EXAMINATION_APPROVAL" domainObjectName="CadreInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!-- 主键生成方式 -->
            <!--<generatedKey column="id" sqlStatement="select seq_t_user.nextval from dual" identity="true" />-->
            <!-- 列名去除前缀 -->
            <!--<columnRenamingRule searchString="^[^_]+" replaceString="" />-->
        </table>
    </context>
</generatorConfiguration>

配置完后选中运行,运行成功就会生成文件,如果没成功,请检查配置,或者去百度mybatis-generator用法。

mybatis还没完。还要配置一些东西,不然后面运行要报错。参考这个(同一项目):

https://blog.csdn.net/gaojiajie333/article/details/81866318

完整application.xml :

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@133.32.62.110:1521:UDPL
    username: C##DM
    password: xxhdm#409
  #thymelea模板配置
  #热部署文件,页面不产生缓存,及时更新
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    cache: false
  resources:
    chain:
      strategy:
        content:
          enabled: true
          paths: /**
server:
  port: 8081
mybatis:
  mapper-locations: classpath:com/hrmanage/mapper/*.xml
  type-aliases-package: com.hrmanage.domain

完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.HRManage</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>com.HRManage</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </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>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>1.3.2</version>
      </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.2.0</version>
        </dependency>


      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>



   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>

            <!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.2.0.2.0</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
      </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
         <resource>
            <directory>src/main/resources</directory>
            <includes>
                    <include>**/*.*</include>
            </includes>
         </resource>
        </resources>
   </build>
</project>

到此项目基本完成。

完整项目代码和Oracle jdbc jar包放在  https://download.csdn.net/download/gaojiajie333/10615118

~不喜欢篮球的摄影师不是一个好程序员~

 

 

  • 3
    点赞
  • 1
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值