Mybatis之使用注解开发CRUD

上一篇演示了如何使用XML来操作Mybatis实现CRUD,但是大量的XML配置文件的编写是非常烦人的。因此

Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。

首先是创建一个接口。

package com.bird.mybatis.bean;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface UserMapper {
	@Insert("insert into users(name, age) values(#{name}, #{age})")
	public int add(Users user);
	
	@Delete("delete from users where id = #{id}")
	public int deleteById(int id);
	
	@Update("update users set name = #{name}, age = #{age} where id = #{id}")
	public int update(Users user);
	
	@Select("select * from users where id = #{id}")
	public Users getUserById(int id);
	
	@Select("select * from users")
	public List<Users> getAllUsers();
}

然后一定不要忘了在conf.xml配置文件中,注册这个类

<mappers>
		<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
		<mapper class="com.bird.mybatis.bean.UserMapper"/>
	</mappers>

下面就是使用这个类了

@Test
	public void testAdd2() {
		SqlSession openSession = factory.openSession();
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		mapper.add(new Users(-1,"娃娃",99));
		openSession.commit();
		openSession.close();
	}


  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
对于使用注解开发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接口的实现,无需手动编写映射文件。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值