其他分享
首页 > 其他分享> > springboot整合mybatis

springboot整合mybatis

作者:互联网

1、导入pom依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2、在yaml(properties)中配置数据库链接,配置mybatis

spring:
  datasource:
    username: root
    password: '123456'
    url: jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC&useUnicode=true?characterEconding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver


#配置mybatis
mybatis:
  type-aliases-package: com.example.demo8.Pojo
  mapper-locations: classpath:Mapper/*.xml

3、创建pojo实体类

4、创建Mapper接口,@Mapper、@Repository

5、***.xml实现相应Mapper,绑定namespace,id,确定返回类型。

<?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.example.demo8.Mapper.UserMapper">
    <select id="queryUserList" resultType="User" >  <!--useCache="true"  第二次查询时开启缓存-->
     select * from user 
  </select>

<delete id="deleteUserById" parameterType="int">
  delete from user where id=#{id}
</delete>

</mapper>

 

标签:xml,Mapper,springboot,spring,整合,mysql,mybatis,id
来源: https://www.cnblogs.com/theng/p/16669704.html