springboot整合mybatis

8 篇文章 0 订阅
4 篇文章 0 订阅

1.依赖

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.配置数据库连接

yaml配置:

# 数据库连接相关配置
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

3.创建对应数据库的实体类

4.创建mapper接口类

peoper.java

@Mapper
@Repository
public interface PeoperMapper {

    List<Peoper> queryAllPeoper();

    Peoper queryPeoperById(@Param("id") int id);

    int insertPeoper(Peoper peoper);

    int updatePeoper(Peoper peoper);

    int deletePeoperById(@Param("id") int id);

}
注意,mapper接口,应添加一下注解
@Mapper
@Repository

或在入口类上添加扫描注解

@MapperScan("com.xzb.mapper")

5.让springboot绑定mybatis配置文件,或更改其他配置,如:别名等.

yaml配置:

# mybatis绑定配置文件,或更改其他配置,如:别名等
mybatis:
  type-aliases-package: com.xzb.pojo  #别名
  mapper-locations: classpath:mybatis/mapper/*.xml  #绑定mapper

6.编写对应于接口的mapper.xml文件

<?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">
<!--namespace用来绑定一个dao/mapper接口-->
<mapper namespace="com.xzb.mapper.PeoperMapper">

    <select id="queryAllPeoper" resultType="peoper">
        select *
        from stu.peoper;
    </select>

    <select id="queryPeoperById" resultType="peoper" parameterType="int">
        select *
        from stu.peoper
        where id = #{id};
    </select>

    <insert id="insertPeoper" parameterType="peoper">
        insert into stu.peoper (name, age)
        values (#{name},#{age});
    </insert>

    <update id="updatePeoper" parameterType="peoper">
        update stu.peoper
        set name = #{name},age = #{age}
        where id = #{id};
    </update>

    <delete id="deletePeoperById" parameterType="int">
        delete from stu.peoper where id = #{id}
    </delete>

</mapper>

7.测试

@RestController
public class MybatisController {

    @Autowired
    PeoperMapper peoperMapper;


    @GetMapping("/all")
    public List<Peoper> queryAllPeoper(){
        List<Peoper> peopers = peoperMapper.queryAllPeoper();
        return peopers;
    }

    @GetMapping("/qby/{id}")
    public Peoper queryPeoperById(@PathVariable("id") int id){
        Peoper peoper = peoperMapper.queryPeoperById(id);
        return peoper;
    }

    @GetMapping("/insert")
    public String insertPeoper(Peoper peoper){
        peoperMapper.insertPeoper(peoper);
        return "insert-ok";
    }

    @GetMapping("/update")
    public String updatePeoper(Peoper peoper){
        peoperMapper.updatePeoper(peoper);
        return "update-ok";
    }

    @GetMapping("/delete/{id}")
    public String deletePeoperById(@PathVariable("id") int id){
        peoperMapper.deletePeoperById(id);
        return "delete-ok";
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值