springboot+mybatis示例

1、新建springboot项目,结构如下

2、pom.xml新增mybatis依赖

<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>
    <scope>runtime</scope>
</dependency>

3、application.properties配置文件代码如下

值得注意的地方为需要指出Mapper.xml的配置路径mybatis.mapperLocations=classpath:MytestMapper.xml

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1/demo?characterEncoding=GBK&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

pring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-while-idle=true

mybatis.mapperLocations=classpath:MytestMapper.xml

MytestMapper.java文件

package com.example.demo.dao;

import com.example.demo.object.Mytest;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface MytestMapper {
    List<Mytest> getMytestList();
    void addMtest(Mytest mytest);
    void updateMytest(Mytest mytest);
    void deleteMytest(Mytest mytest);
}

 Mytest.java文件

package com.example.demo.object;


import lombok.Data;

@Data
public class Mytest {
    int id;
    String name;
    int age;
    int height;
}

 MytestMapper.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">
<mapper namespace="com.example.demo.dao.MytestMapper">

<!--    <resultMap id="BaseResultMap" type="com.example.demo.object.Mytest">-->
<!--        <result column="id" jdbcType="DECIMAL" property="id" />-->
<!--        <result column="name" jdbcType="VARCHAR" property="name" />-->
<!--        <result column="age" jdbcType="DECIMAL" property="age" />-->
<!--        <result column="height" jdbcType="DECIMAL" property="height" />-->
<!--    </resultMap>-->

<!--    <select id="getMytestList" resultType="com.example.demo.object.Mytest">-->
<!--        select id as id, name as name, age as age,height as height from mytest-->
<!--    </select>-->

    <select id="getMytestList" resultType="com.example.demo.object.Mytest">
        select* from mytest
    </select>

    <insert id="addMtest" parameterType="com.example.demo.object.Mytest" >
    insert into mytest (id,name,age,height) values (#{id}, #{name}, #{age},#{height})
    </insert>

    <update id="updateMytest" parameterType="com.example.demo.object.Mytest">
        update mytest set name=#{name}, age=#{age}, height=#{height} where id=#{id}
    </update>

    <delete id="deleteMytest" parameterType="com.example.demo.object.Mytest">
        delete from mytest where id=#{id}
    </delete>

    <!--    <select id="getMytestList" resultMap="BaseResultMap">-->
<!--        select id,name,age,height from demo.mytest-->
<!--    </select>-->

</mapper>

测试类代码:

@SpringBootTest
class DemoApplicationTests {
    @Resource
    MytestMapper mytestMapper;
    
    @Test
    void contextLoads() {
        List<Mytest> list = mytestMapper.getMytestList();
        System.out.println(list);
    }

    @Test
    void intertR() {
        Mytest mytest = new Mytest();
        mytest.setId(3);
        mytest.setName("ding");
        mytest.setAge(15);
        mytest.setHeight(160);
        mytestMapper.addMtest(mytest);
    }

    @Test
    void updateR() {
        Mytest mytest = new Mytest();
        mytest.setId(3);
        mytest.setName("ding");
        mytest.setAge(15);
        mytest.setHeight(180);
        mytestMapper.updateMytest(mytest);
    }

    @Test
    void deleteR() {
        Mytest mytest = new Mytest();
        mytest.setId(3);
        mytestMapper.deleteMytest(mytest);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值