SpringBoot学习(四)——整合mybatis

78 篇文章 0 订阅
10 篇文章 0 订阅
整合Mybatis
  • 建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `real_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  `sex` tinyint(255) NULL DEFAULT NULL,
  `birthday` timestamp NULL DEFAULT NULL,
  `created` timestamp NULL DEFAULT NULL,
  `updated` timestamp NULL DEFAULT NULL,
  `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'zhangsan', '123456', '张三', 22, 1, '1997-05-13 00:00:00', '2009-06-01 00:00:00', '2011-07-15 00:00:00', '张三同学在学习Java');
INSERT INTO `user` VALUES (2, 'lisi', '123456', '李四', 26, 2, '1993-09-14 07:37:18', '2013-07-11 00:00:00', '2017-07-23 00:00:00', '李斯同学在学习数据结构');
INSERT INTO `user` VALUES (3, 'wangwu', '123456', '王五', 23, 1, '1996-04-13 00:00:00', '2015-09-06 00:00:00', '2017-08-25 00:00:00', '王五同学在学习HTML');

SET FOREIGN_KEY_CHECKS = 1;
  • 创建springboot项目

  • 导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
  • application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:/springbootdb?userUnicode=true&characterEncoding=UTF-8
    hikari:
      maximum-pool-size: 30
      minimum-idle: 10
      idle-timeout: 60000
  • 创建实体类
public class User {
    private Integer id;
    private String userName;
    private String password;
    private Integer age;
    private Date birthday;
    private Date created;
    private Date updated;
    private String remarks;

    public User() {
    }

    public User(Integer id, String userName, String password, Integer age, Date birthday, Date created, Date updated, String remarks) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.age = age;
        this.birthday = birthday;
        this.created = created;
        this.updated = updated;
        this.remarks = remarks;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }
}
  • 测试
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    private DataSource dataSource;
    @Test
    void contextLoads() {
        try {
            System.out.println(dataSource.getConnection());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
  • 测试结果:

在这里插入图片描述

创建UserMapper.java

@Mapper
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    public User findById(int id);
}

测试:

@Resource
private UserMapper userMapper;
@Test
void contextLoads() {
    System.out.println(userMapper.findById(1));
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值