【简洁版】springboot整合mybatis

第一步:引入mybatis依赖

<dependency>
   	<groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>

第二步:引入mysql数据库连接驱动依赖

<dependency>
   	<groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

第三步:修改application.xml配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: xxxx
    password: xxxxxx
    url: jdbc:mysql://[数据库ip]:[数据库port]/[数据库名称]?characterEncoding=utf-8&serverTimezone=UTC
mybatis:
  configuration: 
    # 控制台日志配置
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mappers/*.xml

第四步:给mapper接口添加@Mapper注解

@Mapper
public interface UserMapper {
    int insert(User record);
    User selectByPrimaryKey(Integer id);
}

第五步:测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @Autowired
    UserMapper userMapper;

    User user;

    @Test
    public void insert() {
        user = new User();
        user.setUsername("uname");
        user.setPassword("123456");

        int row = userMapper.insert(user);
        System.out.println("此次成功插入了"+row+"条数据!!");
    }

    @Test
    public void selectByPrimaryKey() {
        User user = userMapper.selectByPrimaryKey(1);
        System.out.println(user);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值