Spring Boots &mybatis连接数据库配置步骤

本文介绍了如何在Spring Boot项目中整合Mybatis并配置连接Oracle数据库的步骤。首先,添加必要的Maven依赖,包括mysql-connector-java、mybatis-spring-boot-starter、Apache Commons库等。然后,在配置文件application.yml中设置数据库连接信息。接着,通过@MapperScan注解指定Mapper接口扫描路径,并创建UserMapper接口,定义SQL查询。最后,通过@Autowired在Controller中注入UserMapper。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



1. 5个包 放到 POM 
<!--mybatis -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

<!--commons -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>
<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>
    <version>1.10</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>


2. 生产项目MAVEN 依赖


3. config  aplication.java
//增加持久层
@MapperScan("com.jm.dao")


4. config application.yml




datasource:
        url: jdbc:oracle:thin:@20.1.10.18:1521:jiumao
        username: jiumao
        password: 123456


    mybatis:
        configuration:
            map-underscore-to-camel-case: true




5.dao 


import com.jm.Model.UserDB;
import org.apache.ibatis.annotations.Select;


//注意这里是interface
public interface UserMapper {
   //@Select("select `username`, `password` from user where username = #{username}")
   // User getByUsername(String username);
    //z这里没有分号
    @Select("select 'userid' , 'username' , 'userno' ,'password'from users where username=#{username}")
    UserDB getByUsername(String username);




}


6.  @Autowired controller
@Autowired
    UserMapper userMapper;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值