当前位置:   article > 正文

使用mybatis来操作oracle

使用mybatis来操作oracle

1.使用spring的初始化工具来构建项目-
在这里插入图片描述-
2.添加依赖-
在这里插入图片描述-
3.项目结构-
在这里插入图片描述-
4.详细文件-
4.1配置文件

server:
  port: 8888
spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@127.0.0.1:11521:helowin
    username: mark
    password: 123456
  flyway:
    enabled: false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.2DTO

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDto implements Serializable {
    private Integer id;
    private String  name;
    private String  pwd;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.3Controller

@RestController
@RequestMapping("/hel")
public class HelloOracle {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public List<UserDto> getUsers(){
        List<UserDto> users= userService.getAllUsers();
        return users;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.4Service

public interface UserService {
    /**
     * 获取所有用户
     * @return
     */
    List<UserDto> getAllUsers();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;

    @Override
    public List<UserDto> getAllUsers() {
        List<UserDto> users=userDao.selectAllUsers();
        return users;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4.5Dao

@Mapper
public interface UserDao {
    /**
     * 获取所有用户
     * @return
     */
    List<UserDto> selectAllUsers();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.6Mapper

<?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.example.oraclewithmybatis.dao.UserDao">
    <!--    配置查询所有 id为方法名 resultType指定封装的实体类型-->
    <select id="selectAllUsers" resultType="com.example.oraclewithmybatis.dto.UserDto">
        select
               ID id,
               PWD pwd,
               USER_NAME name
        from USER_INFO
    </select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.7Sql脚本

CREATE TABLE USER_INFO(
    ID INT NOT NULL AUTO_INCREMENT  COMMENT 'ID' ,
    PWD VARCHAR(200) NOT NULL   COMMENT '密码' ,
    USER_NAME VARCHAR(200) NOT NULL   COMMENT '用户名' ,
    PRIMARY KEY (ID)
)  COMMENT = '用户表';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.8测试结果

GET http://localhost:8888/hel/user

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 25 Oct 2021 13:14:56 GMT
Keep-Alive: timeout=60
Connection: keep-alive

[
  {
    "id": 1,
    "name": "mark",
    "pwd": "123456"
  },
  {
    "id": 2,
    "name": "张安",
    "pwd": "123456"
  }
]

Response code: 200; Time: 1040ms; Content length: 75 bytes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/154163
推荐阅读
相关标签
  

闽ICP备14008679号