美文网首页工作专题
spring-boot mybatis 连接Oracle

spring-boot mybatis 连接Oracle

作者: 帅哥_刷哥 | 来源:发表于2017-09-04 06:58 被阅读2456次

1.mybatis1.0.0 连接方式

1.1pom.xml配置

<!-- oracle mybatis -->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.2.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

1.2application.properties配置

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5


mybatis.mapperLocations=classpath:mapper/*.xml

1.3项目结构

image.png

1.4OneController.java

package com.shuai.spring_boot_1.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shuai.spring_boot_1.service.UserService;

@Controller
public class OneController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/")
    @ResponseBody
    public String index(){
        
        userService.insert();
        
        return "hello spring boot";
    }
}
image.png

1.5UserService.java

package com.shuai.spring_boot_1.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shuai.spring_boot_1.domain.User;
import com.shuai.spring_boot_1.mapper.UserMapper;

@Service
public class UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    public void insert(){
        User user = new User();
        user.setId(2);
        user.setName("lisi");
        userMapper.insert(user);
        System.out.println("插入成功");
    }

}
image.png

1.6UserMapper.java

package com.shuai.spring_boot_1.mapper;

import com.shuai.spring_boot_1.domain.User;

public interface UserMapper {
    
    void insert(User user);

}
image.png

1.7User.java

package com.shuai.spring_boot_1.domain;

public class User {
    
    private int id;
    
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}
image.png

1.8App.java

package com.shuai.spring_boot_1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
image.png

1.8UserMapper.xml

<?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="com.shuai.spring_boot_1.mapper.UserMapper" >

  <insert id="insert" parameterType="com.shuai.spring_boot_1.domain.User" >
    insert into user (id,name)
    values (#{id}, #{name})
  </insert>
  
</mapper>
image.png

1.9运行项目

运行App.java中的main方法

1.10访问项目

http://localhost:8080/

相关文章

网友评论

    本文标题:spring-boot mybatis 连接Oracle

    本文链接:https://www.haomeiwen.com/subject/fonzdxtx.html