配置准备
引入依赖
在pom.xml文件中引入数据库和mybatis相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/forum22?serverTimezone=Asia/Shanghai
username: 数据库登录账号
password: 数据库登录密码
#下面这些内容是为了让MyBatis映射
mybatis:
mapper-locations: classpath:com/example/forum/dao/*xml #指定Mybatis的Mapper文件
type-aliases-package: com.example.forum.pojo #指定Mybatis的实体目录
使用
创建实体类
引入依赖(可选)
引入lombook依赖,此依赖可以简化实体类的创建与维护
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
实体类
package com.example.forum.pojo;
import lombok.Data;
import java.util.Date;
/**
* @author gs_huang
*/
@Data
public class User {
private Integer id;
private String username;
private String password;
private Date createTime;
private String avatar;
private String nickname;
private String signature;
private String introduction;
private Integer rankExperience;
private Integer rank;
private Integer articleCount;
private Integer commentCount;
private Integer roleId;
private String status;
}
创建映射接口
package com.example.forum.dao;
import com.example.forum22.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author gs_huang
*/
@Mapper
public interface UserDao {
/**
* 查询所有用户信息
* @return 所有用户信息
*/
@Select("select * from user")
@ResultMap("UserMap")
List<User> findAll();
}
创建映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.forum.dao.UserDao">
<resultMap id="UserMap" type="User">
<result column="id" property="id"/>
<result column="username" property="username" />
<result column="password" property="password" />
<result column="create_time" property="createTime"/>
<result column="avatar" property="avatar" />
<result column="nickname" property="nickname" />
<result column="signature" property="signature" />
<result column="introduction" property="introduction" />
<result column="rank_experience" property="rankExperience" />
<result column="rank" property="rank" />
<result column="article_count" property="articleCount" />
<result column="comment_count" property="commentCount" />
<result column="role_id" property="roleId" />
<result column="status" property="status" />
</resultMap>
</mapper>
测试
package com.example.forum.dao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
void findAll() {
System.out.println(userDao.findAll());
}
}
总结
- 引入依赖
- 创建实体类
- 创建接口
- 测试