使用注解开发-mybatis

面向接口编程
1、之前学过面向对象编程,也学习过接口,但在真正的开发中,很多时候会选择面向接口编程。
2、根本原因:解耦,可拓展,提高复用,分层开发中,上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性更好
3、在一个面向对象的系统中,系统的各种功能是由许许多多的不同对象协作完成的。在这种情况下,各个对象内部是如何实现自己的,对系统设计人员来讲就不那么重要了
4、而各个对象之间的协作关系则成为系统设计的关键。小到不同类之间的通信,大到各模块之间的交互,在系统设计之初都是要着重考虑的,这也是系统设计的主要工作内容。面向接口编程就是指按照这种思想来编程。

使用注解开发
1、注解在UserMapper接口上实现,并删除UserMapper.xml文件

@Select("select * from user")
List<User> getUsers();

2、需要在mybatis-config.xml核心配置文件中绑定接口

<!--绑定接口!注解开发-->
<mappers>
    <mapper class="com.kuang.dao.UserMapper" 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于使用注解开发MyBatis的Spring Boot项目,你可以按以下步骤进行操作: 1. 首先,在Spring Boot项目中添加MyBatisMyBatis-Spring的依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 创建一个数据源配置类,用于配置数据库连接信息,例如: ```java @Configuration @MapperScan(basePackages = "com.example.mapper") public class DataSourceConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } } ``` 3. 创建Mapper接口,并使用@Mapper注解标识该接口为MyBatis的Mapper接口,例如: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user") List<User> getAllUsers(); // 其他CRUD方法 } ``` 4. 在application.properties或application.yml文件中配置数据库连接信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: password ``` 5. 在Spring Boot的启动类上添加@MapperScan注解,指定要扫描的Mapper接口所在的包,例如: ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,你就可以在Spring Boot项目中使用注解开发MyBatis了。通过在Mapper接口的方法上使用注解实现CRUD操作,Spring Boot会自动扫描并注册这些Mapper接口的实现,无需手动编写映射文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marlboro~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值