Loading

SpringBoot + mybatis + oracle简单使用

添加依赖

热部署

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

热部署配置

勾选自动构建

双击shift查找Registry,勾选 automake allow when app running

mybatis

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

mybatis-generator

 <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

mybatis-generator插件

<!--添加mybatis generator maven插件-->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
        <!--generatorConfig.xml位置-->
        <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
        </execution>
    </executions>
    <!--此处必须添加oracle驱动包-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>19.3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.oracle.database.nls/orai18n -->
        <dependency>
            <groupId>com.oracle.database.nls</groupId>
            <artifactId>orai18n</artifactId>
            <version>21.1.0.0</version>
        </dependency>
    </dependencies>
</plugin>

Oracle连接驱动

 <!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
<dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>19.3.0.0</version>
</dependency>        

配置mybatis

mybatis-config.xml

详情见:https://www.cnblogs.com/YC-L/p/14620714.html

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

    <!-- 引用db.properties配置文件 -->
    <properties resource="db.properties"/>
    <!--
        development : 开发模式
        work : 工作模式
     -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <!-- value属性值引用db.properties配置文件中配置的值 -->
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${name}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
</configuration>

db.properties

url=jdbc:oracle:thin:@//*.*.*.*:1521/ORA19C
username=sys as sysdba
password=root
driver=oracle.jdbc.driver.OracleDriver

application.properties

mybatis.config-location=classpath:/mybatis-config.xml
mybatis.mapper-locations=classpath:/mapper/*.xml

SpringBoot中classpath:https://www.cnblogs.com/YC-L/p/14628113.html

使用mybatis-generator生成dao,entity和mapper

参考文章:https://www.cnblogs.com/YC-L/p/14620323.html

我的mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.techny.demo.dao.StuInfoDao">
  <resultMap id="BaseResultMap" type="cn.techny.demo.entity.StuInfoEntity">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <constructor>
      <arg column="STUID" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="STUNAME" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="SEX" javaType="java.lang.String" jdbcType="CHAR" />
      <arg column="AGE" javaType="java.lang.Short" jdbcType="NUMERIC" />
      <arg column="CLASSNO" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="STUADDRESS" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="GRADE" javaType="java.lang.String" jdbcType="CHAR" />
      <arg column="ENROLDATE" javaType="java.util.Date" jdbcType="TIMESTAMP" />
      <arg column="IDNUMBER" javaType="java.lang.String" jdbcType="VARCHAR" />
    </constructor>
  </resultMap>
  <insert id="insert" parameterType="cn.techny.demo.entity.StuInfoEntity">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into STUINFO (STUID, STUNAME, SEX, 
      AGE, CLASSNO, STUADDRESS, 
      GRADE, ENROLDATE, IDNUMBER
      )
    values (#{stuid,jdbcType=VARCHAR}, #{stuname,jdbcType=VARCHAR}, #{sex,jdbcType=CHAR}, 
      #{age,jdbcType=NUMERIC}, #{classno,jdbcType=VARCHAR}, #{stuaddress,jdbcType=VARCHAR}, 
      #{grade,jdbcType=CHAR}, #{enroldate,jdbcType=TIMESTAMP}, #{idnumber,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="cn.techny.demo.entity.StuInfoEntity">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into STUINFO
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="stuid != null">
        STUID,
      </if>
      <if test="stuname != null">
        STUNAME,
      </if>
      <if test="sex != null">
        SEX,
      </if>
      <if test="age != null">
        AGE,
      </if>
      <if test="classno != null">
        CLASSNO,
      </if>
      <if test="stuaddress != null">
        STUADDRESS,
      </if>
      <if test="grade != null">
        GRADE,
      </if>
      <if test="enroldate != null">
        ENROLDATE,
      </if>
      <if test="idnumber != null">
        IDNUMBER,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="stuid != null">
        #{stuid,jdbcType=VARCHAR},
      </if>
      <if test="stuname != null">
        #{stuname,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=CHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=NUMERIC},
      </if>
      <if test="classno != null">
        #{classno,jdbcType=VARCHAR},
      </if>
      <if test="stuaddress != null">
        #{stuaddress,jdbcType=VARCHAR},
      </if>
      <if test="grade != null">
        #{grade,jdbcType=CHAR},
      </if>
      <if test="enroldate != null">
        #{enroldate,jdbcType=TIMESTAMP},
      </if>
      <if test="idnumber != null">
        #{idnumber,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
</mapper>

我的dao

package cn.techny.demo.dao;

import cn.techny.demo.entity.StuInfoEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StuInfoDao {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table STUINFO
     *
     * @mbg.generated
     */
    int insert(StuInfoEntity record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table STUINFO
     *
     * @mbg.generated
     */
    int insertSelective(StuInfoEntity record);
}

我的entity

package cn.techny.demo.entity;

import java.util.Date;

public class StuInfoEntity {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.STUID
     *
     * @mbg.generated
     */
    private String stuid;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.STUNAME
     *
     * @mbg.generated
     */
    private String stuname;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.SEX
     *
     * @mbg.generated
     */
    private String sex;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.AGE
     *
     * @mbg.generated
     */
    private Short age;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.CLASSNO
     *
     * @mbg.generated
     */
    private String classno;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.STUADDRESS
     *
     * @mbg.generated
     */
    private String stuaddress;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.GRADE
     *
     * @mbg.generated
     */
    private String grade;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.ENROLDATE
     *
     * @mbg.generated
     */
    private Date enroldate;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column STUINFO.IDNUMBER
     *
     * @mbg.generated
     */
    private String idnumber;

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table STUINFO
     *
     * @mbg.generated
     */
    public StuInfoEntity(String stuid, String stuname, String sex, Short age, String classno, String stuaddress, String grade, Date enroldate, String idnumber) {
        this.stuid = stuid;
        this.stuname = stuname;
        this.sex = sex;
        this.age = age;
        this.classno = classno;
        this.stuaddress = stuaddress;
        this.grade = grade;
        this.enroldate = enroldate;
        this.idnumber = idnumber;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table STUINFO
     *
     * @mbg.generated
     */
    public StuInfoEntity() {
        super();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.STUID
     *
     * @return the value of STUINFO.STUID
     *
     * @mbg.generated
     */
    public String getStuid() {
        return stuid;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.STUID
     *
     * @param stuid the value for STUINFO.STUID
     *
     * @mbg.generated
     */
    public void setStuid(String stuid) {
        this.stuid = stuid == null ? null : stuid.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.STUNAME
     *
     * @return the value of STUINFO.STUNAME
     *
     * @mbg.generated
     */
    public String getStuname() {
        return stuname;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.STUNAME
     *
     * @param stuname the value for STUINFO.STUNAME
     *
     * @mbg.generated
     */
    public void setStuname(String stuname) {
        this.stuname = stuname == null ? null : stuname.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.SEX
     *
     * @return the value of STUINFO.SEX
     *
     * @mbg.generated
     */
    public String getSex() {
        return sex;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.SEX
     *
     * @param sex the value for STUINFO.SEX
     *
     * @mbg.generated
     */
    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.AGE
     *
     * @return the value of STUINFO.AGE
     *
     * @mbg.generated
     */
    public Short getAge() {
        return age;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.AGE
     *
     * @param age the value for STUINFO.AGE
     *
     * @mbg.generated
     */
    public void setAge(Short age) {
        this.age = age;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.CLASSNO
     *
     * @return the value of STUINFO.CLASSNO
     *
     * @mbg.generated
     */
    public String getClassno() {
        return classno;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.CLASSNO
     *
     * @param classno the value for STUINFO.CLASSNO
     *
     * @mbg.generated
     */
    public void setClassno(String classno) {
        this.classno = classno == null ? null : classno.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.STUADDRESS
     *
     * @return the value of STUINFO.STUADDRESS
     *
     * @mbg.generated
     */
    public String getStuaddress() {
        return stuaddress;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.STUADDRESS
     *
     * @param stuaddress the value for STUINFO.STUADDRESS
     *
     * @mbg.generated
     */
    public void setStuaddress(String stuaddress) {
        this.stuaddress = stuaddress == null ? null : stuaddress.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.GRADE
     *
     * @return the value of STUINFO.GRADE
     *
     * @mbg.generated
     */
    public String getGrade() {
        return grade;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.GRADE
     *
     * @param grade the value for STUINFO.GRADE
     *
     * @mbg.generated
     */
    public void setGrade(String grade) {
        this.grade = grade == null ? null : grade.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.ENROLDATE
     *
     * @return the value of STUINFO.ENROLDATE
     *
     * @mbg.generated
     */
    public Date getEnroldate() {
        return enroldate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.ENROLDATE
     *
     * @param enroldate the value for STUINFO.ENROLDATE
     *
     * @mbg.generated
     */
    public void setEnroldate(Date enroldate) {
        this.enroldate = enroldate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column STUINFO.IDNUMBER
     *
     * @return the value of STUINFO.IDNUMBER
     *
     * @mbg.generated
     */
    public String getIdnumber() {
        return idnumber;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column STUINFO.IDNUMBER
     *
     * @param idnumber the value for STUINFO.IDNUMBER
     *
     * @mbg.generated
     */
    public void setIdnumber(String idnumber) {
        this.idnumber = idnumber == null ? null : idnumber.trim();
    }
}

使用JavaAPI实现数据库基本操作

Controller

package cn.techny.demo.Controller;

import cn.techny.demo.Service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    public DemoService demoService;

    @Autowired
    public DemoController(DemoService demoService){
        this.demoService = demoService;
    }
    @GetMapping(path = "/demo")
    public int demo(){
        return demoService.DemoInsert();
    }
}

Service

package cn.techny.demo.Service;

import cn.techny.demo.dao.StuInfoDao;
import cn.techny.demo.entity.StuInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DemoService {

    private final StuInfoDao stuInfoDao;

    @Autowired
    public DemoService(StuInfoDao stuInfoDao) {
        this.stuInfoDao = stuInfoDao;
    }

    public int DemoInsert() {
        StuInfoDao stuInfoDao = this.stuInfoDao;
        int res = 1;
        Date date = new Date();
        StuInfoEntity stuInfoEntity = new StuInfoEntity(
                "test", "test", "1", (short) 1, "test", "test", "test", date, "test");
        res = stuInfoDao.insert(stuInfoEntity);
        return res;
    }
}

封装sql语句到mapper中,直接调用执行即可

posted @ 2021-04-07 16:17  BigBender  阅读(622)  评论(0编辑  收藏  举报