IDEA Spring Boot 整合Mybatis

10 篇文章 0 订阅
9 篇文章 0 订阅

1.创建Spring Boot项目,添加Mybatis依赖

pom.xml

		<!--Mybatis Spring Boot-->
		<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        
		<!--MySQL数据库驱动-->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        
		<!--用于方法测试-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        
        <!--单元测试框架-->
		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

2.Spring Boot配置文件

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC
    username: root
    password: root
    
mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.cx.mybatis.bean

3.数据库

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`username`  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 ,
`role_id`  int(11) NULL DEFAULT NULL ,
`createtime`  timestamp NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=13
ROW_FORMAT=DYNAMIC

在这里插入图片描述

4.Spring Boot相关类

User类

public class User {
    private Integer id;
    private String username;
    private String password;
    // set/get,构造函数及toString省略...
}

UserMapper数据库映射接口类

@Repository
public interface UserMapper {
    /**
     * 根据ID查User
     * @param id
     * @return
     */
    User getUserById(@Param("id") Integer id);
}

IUserService服务层接口类

public interface IUserService {
    /**
     * 根据ID获取User记录
     * @param id
     * @return
     */
    User getUserById(Integer id);
}

UserServiceImpl服务层实现类

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }
}

MybatisApplication Spring Boot应用启动入口

@SpringBootApplication
@MapperScan("com.cx.mybatis.mapper") // 扫描映射类文件位置
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

5.Mybatis映射配置文件

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 不写会报错 -->
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org/DTD Mapper 3.0"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cx.mybatis.mapper.UserMapper">
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>
</mapper>

6.测试功能

UserTest测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
    @Autowired
    IUserService userService;

    @Test
    public void test() {
        User user = userService.getUserById(2);
        System.out.println(user);
    }
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ww空ww

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

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

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

打赏作者

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

抵扣说明:

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

余额充值