打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringBoot之Mybatis连接MySQL进行CRUD(注解&配置文件)(简测试版)

pom.xml中添加依赖

        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.1.1</version>        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

说是简单测试版,所有的文件都在Application.java中,不适合正经的开发。

在application.properties中配置数据库信息:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver#根据自己环境配置可连接的数据库信息spring.datasource.url=jdbc:mysql://172.16.14.40:3306/springbootspring.datasource.username=devgroupspring.datasource.password=devgroup
  • 1
  • 2
  • 3
  • 4
  • 5

sql语句:

drop table if exists car;create table car (    id int(10) auto_increment not null,    username varchar(50) not null,    primary key(id))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注解版

Application.java

package com.zhu;import java.util.Arrays;import java.util.Collection;import java.util.List;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import lombok.AllArgsConstructor;import lombok.Data;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import lombok.ToString;@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @Bean    CommandLineRunner demo(CarMapper carMapper) {        return args -> {            List<Car> cars = Arrays.asList(new Car("zhangsan"), new Car("lisi"), new Car("wangwu"));            cars.forEach(car -> {                carMapper.insert(car);                System.err.println("插入数据:" + car.toString());            });            System.err.println("-----------查询所有的数据-----------------");            carMapper.selectAll().forEach(System.out::println);            System.err.println("----------删除数据---------------");            carMapper.RemoveOne(carMapper.selectAll().size());            System.err.println("----------修改数据---------------");            carMapper.UpdateOne(135,"zhaoliu");        };    }}@Mapperinterface CarMapper {    @Options(useGeneratedKeys = true)    @Insert("insert into car(username) values(#{username})")    void insert(Car car);    @Update("update car set username = #{username} where id = #{id}")    void UpdateOne(@Param("id") int id,@Param("username") String username);    @Insert("delete from car where id = #{id}")    void RemoveOne(int id);    @Select("select * from car")    Collection<Car> selectAll();}@Data@AllArgsConstructor@NoArgsConstructorclass Car {    public Car(String username) {        this.username = username;    }    public Car(Integer id, String username) {        this.id = id;        this.username = username;    }    private int id;    private String username;    @Override    public String toString() {        return "Car [id=" + id + ", username=" + username + "]";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

点击run Application:

配置文件版

src/main/resources/com/zhu/CarMapper.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.zhu.CarMapper">    <select id="searchOne" resultType="com.zhu.Car">        SELECT * FROM car        <where>            <if test="username != null">                username = #{username}            </if>        </where>    </select></mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Application.java

package com.zhu;import java.util.Arrays;import java.util.Collection;import java.util.List;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import lombok.AllArgsConstructor;import lombok.Data;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import lombok.ToString;@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @Bean    CommandLineRunner demo(CarMapper carMapper) {        return args -> {            List<Car> cars = Arrays.asList(new Car("zhangsan"), new Car("lisi"), new Car("wangwu"));            cars.forEach(car -> {                carMapper.insert(car);                System.err.println("插入数据:" + car.toString());            });            System.err.println("-----------查询所有的数据-----------------");            carMapper.selectAll().forEach(System.out::println);            System.err.println("----------删除数据---------------");            carMapper.RemoveOne(carMapper.selectAll().size());            System.err.println("----------修改数据---------------");            carMapper.UpdateOne(135,"zhaoliu");            System.err.println("---------配置文件查询---------------------");            carMapper.searchOne("zhangsan").forEach(System.out::println);        };    }}@Mapperinterface CarMapper {    @Options(useGeneratedKeys = true)    @Insert("insert into car(username) values(#{username})")    void insert(Car car);    @Update("update car set username = #{username} where id = #{id}")    void UpdateOne(@Param("id") int id,@Param("username") String username);    @Insert("delete from car where id = #{id}")    void RemoveOne(int id);    @Select("select * from car")    Collection<Car> selectAll();    Collection<Car> searchOne(@Param("username") String username);}@Data@AllArgsConstructor@NoArgsConstructorclass Car {    public Car(String username) {        this.username = username;    }    public Car(Integer id, String username) {        this.id = id;        this.username = username;    }    private int id;    private String username;    @Override    public String toString() {        return "Car [id=" + id + ", username=" + username + "]";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

OK

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis试用
基于SSM之Mybatis接口实现增删改查(CRUD)功能
MyBatis 简单入门
mybatis入门基础(四)
MyBatis教程(5):缓存
SpringBoot整合MyBatis
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服