文章目录
1.引入依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2.在application.properties里面配置数据库基本连接通信
#数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#库名自行修改
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#账号密码改成自己的
spring.datasource.username=root
spring.datasource.password=1234
#mybatis 扫描mapper文件 xml路径配置
mybatis.mapper-locations=classpath:mapper/*.xml
3.准备一张数据库表,初始化数据
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`pass_word` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ;
-- 初始化语句
INSERT INTO `test`.`t_user`(`id`, `user_name`, `pass_word`) VALUES (1, '李白', 'test123');
INSERT INTO `test`.`t_user`(`id`, `user_name`, `pass_word`) VALUES (2, '韩信', 'test456');
4.创建实体类
public class UserInfo {
private String id;
private String userName;
private String passWord;
//get/set方法省略
}
5.创建controller层
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 获取所有用户
* @return
*/
@GetMapping("/getAllUserInfo")
private List<UserInfo> getUserInfo(){
return userService.getUserInfo();
}
}
6.创建service层
public interface UserService {
/** * 获取所有用户
* @return
*/
List<UserInfo> getUserInfo();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 获取所有用户
*
* @return
*/
@Override
public List<UserInfo> getUserInfo() {
return userMapper.getUserInfo();
}
}
7.创建mapper层
@Repository
public interface UserMapper {
/**
* 获取所有用户
* @return
*/
List<UserInfo> getUserInfo();
}
8.创建对应的xml文件
<?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.wys.mylearning.mapper.UserMapper">
<resultMap id="baseMap" type="com.wys.mylearning.models.UserInfo">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="pass_word" property="passWord"/>
</resultMap>
<!-- 查询所有用户-->
<select id="getUserInfo" resultMap="baseMap">
SELECT id,user_name,pass_word FROM t_user
</select>
</mapper>
9.启动类增加mapper包扫描
@SpringBootApplication
//启动类增加mapper包扫描
@MapperScan("com.wys.mylearning.mapper")
public class MylearningApplication {
public static void main(String[] args) {
SpringApplication.run(MylearningApplication.class, args);
}
}
10.项目整体架构如下图
11.启动项目,postman测试
总结
注意要在启动类上加上包扫描路径(@MapperScan(“com.wys.mylearning.mapper”)),路径为mapper层所在路径,还要在application.properties配置文件中配置xml文件所在路径,最后xml文件中的namespace路径为mapper文件所在路径。
(或者不在启动类上加@MapperScan注解,而是在mapper文件类上加@Mapper注解也可以)