引入依赖项
在 pom.xml 文件中引入 mybatis-spring-boot-starter 的依赖:
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
配置 MySQL 连接数据
在 application.yml 文件中配置数据库连接:
# MySQL连接配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://172.30.88.71:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
Mapper 配置
方式一:【推荐】在启动类上添加包扫描注解 @MapperScan(“com.example.mapper”)
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
方式二:在每个 Mapper 类上面添加 @Mapper 注解
Mapper 编写
官方文档对注解功能的说明:https://mybatis.org/mybatis-3/zh/java-api.html
User 实体类:
@Data
@Builder
public class User implements Serializable {
private static final long serialVersionUID = -1602774207630314992L;
private Integer id;
private String username;
private String password;
}
t_user 用户表 sql:
CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
增删改查示例:
// mapper
public interface UserMapper {
@Select("select * from t_user where id=#{id}")
@Results({
@Result(property = "id", column = "id", javaType = Integer.class),
@Result(property = "username", column = "username", javaType = String.class),
@Result(property = "password", column = "password", javaType = String.class)
})
User selectById(Integer id);
@Select("select * from t_user limit #{limit} offset #{offset}")
List<User> selectAll(@Param("limit") int limit ,@Param("offset") int offset);
@Insert("insert into t_user (id,username,password) values (#{id},#{username},#{password})")
@SelectKey(statement="select last_insert_id()", keyProperty="id", before=false, resultType=int.class)
int insert(User user);
@Update("update t_user set username=#{username},password=#{password} where id=#{id}")
int update(User user);
@Delete("delete from t_user where id=#{tid}")
int delete(@Param("tid") int id);
}
测试:
@SpringBootTest
@RunWith(SpringRunner.class)
public class ApplicationTest {
@Resource
private UserMapper userMapper;
// 查
@Test
public void test() {
User user = userMapper.selectById(1);
System.out.println(user);
}
// 查询全部
@Test
public void testSelectAll() {
List<User> users = userMapper.selectAll(2, 2);
users.forEach(System.out::println);
}
// 增
@Test
public void testInsert() {
User user = User.builder().username("张三").password("dfsfknl").build();
int i = userMapper.insert(user);
System.out.println(i);
System.out.println(user);
}
// 更新
@Test
public void testUpdate() {
User user = User.builder().username("张三").password("dfsfknl").build();
int i = userMapper.update(user);
System.out.println(i);
}
// 删除
@Test
public void testDelete() {
int i = userMapper.delete(2);
System.out.println(i);
}
}