SpringBoot项目中整合Mybatis框架

一、添加pom坐标

<!--mybatis起步依赖-->
<dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.3</version>
</dependency>

<!--MySQL驱动-->
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
</dependency>

二、配置信息

1.application.properties配置数据库连接信息
#数据库连接信息
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
2.application.properties配置mybatis的映射文件路径与实体类别名
  • 此处配置可以使用注解替代
#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=com.wuyiz.springboot_quick.entity
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

三、创建类测试是否搭建成功

1.整体文件结构展示

2.Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("queryUser")
    public List<User> queryUser(){
        List<User> users = userService.queryUser();
        return users;
    }
}
3.Service接口和实现类
//接口
public interface UserService {
    List<User> queryUser();
}
//实现类
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryUser() {
        return userMapper.queryUserList();
    }
}
4.Mapper接口
@Mapper
@Repository //把mapper接口注册为spring的bean,避免@autowired时爆红(无影响)
public interface UserMapper {
    public List<User> queryUserList();
}
5.Mapper映射文件
<?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.wuyiz.springboot_quick.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="User">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <id column="username" jdbcType="VARCHAR" property="username"/>
        <id column="birthday" jdbcType="TIMESTAMP" property="birthday"/>
        <id column="sex" jdbcType="VARCHAR" property="sex"/>
        <id column="address" jdbcType="VARCHAR" property="address"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, username, birthday, sex, address
    </sql>
    <select id="queryUserList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from user
    </select>
</mapper>

6.实体类以及数据库表
//实体类
public class User {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    //getter和setter....
}
-- user数据表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名称',
  `birthday` datetime NULL DEFAULT NULL COMMENT '生日',
  `sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
  `address` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 60 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
7.运行结果展示

posted @ 2020-10-07 14:50  中国制造  阅读(144)  评论(0编辑  收藏  举报