springboot整合mybatis与oracle

时间:2025-04-28 07:12:25

 之前一直没有时间写点总结,对于一些细节总是查阅资料把握不够,今天梳理下,完善下方便后续快速迭代。

准备工作。创建一张表

--单表操作 CREATE TABLE tb_user ( 
userid number(4) NOT NULL primary key,
 user_name varchar2(100) unique not null,
 pwd varchar2(100), 
 age number(3) , 
 sex varchar(2), 
 birthday date
 );
 create sequence seq_user; --插入测试数据 
 insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'张三','123456',10,'男',sysdate);
 insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'李四','123456',10,'男',sysdate); 
 insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'王五','123456',10,'男',sysdate); 
 insert into tb_user(userid,user_name,pwd,age,sex,birthday) values(seq_user.nextval,'赵六','123456',10,'男',sysdate); 
 select * from tb_user;

既然是springboot整合mybatis + oracle

0 添加环境信息,贴上

-class-name=
=jdbc:oracle:thin:@127.0.0.1:1521:orcl
=scott
=****

-locations=classpath:mapper/*
-aliases-package=

1 贴上pom依赖 ,oracle使用此版本可以直接maven下载。

  <parent>
    <groupId></groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.</version>
  </parent>


    <dependency>
          <groupId></groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.3.2</version>
      </dependency>
      <dependency>
          <groupId></groupId>
          <artifactId>ojdbc6</artifactId>
          <version>11.2.0.3</version>
      </dependency>

2  创建dto实体

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User implements Serializable {
    private int userid ;
    private String userName;
    private String pwd ;
    private int age;
    private String sex;
    private Date birthday;
}

3 创建dao层,使用mapper注解,springboot会自动创建实体类

/**
 * @Auther: Administrator
 * @Date: 2020/2/8 0008 20:43
 * @Description:
        */
@Mapper
public interface UserMapper {
    //增
    User selectById(int userId);
}

4 创建service层

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User selectById(int userId){
        return (userId);
    }
}

5 常见测试方法

/**
 * @Auther: Administrator
 * @Date: 2020/2/8 0008 21:14
 * @Description:
 */
@RunWith()
@SpringBootTest(classes = {})
public class AppTest {

    @Autowired
    private UserService userService;

    @Test
    public void testSelectById(){
        User user = (2);
        (user);
    }

}

6 直接测试开始,测试结果

0208 220431.810- WARN[           main]  -68: Registered driver with driverClassName= was not found, trying direct instantiation.
0208 220432.057- INFO[           main]          -516: HikariPool-1 - Driver does not support get/set network timeout for connections. (.()I)
0208 220432.063- INFO[           main]       -123: HikariPool-1 - Start completed.
User(userid=2, userName=null, pwd=123456, age=10, sex=男, birthday=Sat Feb 08 20:40:45 CST 2020)
0208 220432.239- INFO[       Thread-2]   -993: Closing @30b6ffe0: startup date [Sat Feb 08 22:04:27 CST 2020]; root of context hierarchy
0208 220432.264- INFO[       Thread-2]       -381: HikariPool-1 - Shutdown initiated...
0208 220432.280- INFO[       Thread-2]       -383: HikariPool-1 - Shutdown completed.

完美!!! 

总结,传统的mybaits需要,springboot整合后,直接在配置参数就可以了。

@mapper注解自动创建接口类。另外直接在启动类添加

@MapperScan("") 也是直接可以的,@mapper注解就可以省略了。