MyBatis 读取MySQL 数据简单示例

1.确定需求、

  输入:前端访问http://localhost:8080/hello

  输出:前端显示后端从数据库读取的数据

2.新建项目的子模块

新建模块mybatis

3.MySQL 数据库配置、添加数据

创建数据库,  添加表, 添加表的字段, 添加数据

4.编写代码

新建controllerservicemapperdomain

新建mapper 资源目录

  新建controllerservicemapperdomain 对应的Java 文件

  新建mapper 资源目录Java mapper 对应的XML 文件

controller

import com.example.mybatis.domain.User;
import com.example.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public List<User> hello()
    {
        return userService.selectAllUser();
    }
}

 service

public class User {
    public int id;
    private String name;
    private int age;
    public String sex;
    public String createTime;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString(){
        return "User{"+
                "id="+id+
                ", name '" + name + '\''+
                ", age="+ age+
                ", sex="+ sex+
                ",createTime='" + createTime +'\''+
                '}';
    }
}

 mapper

import com.example.mybatis.domain.User;

import java.util.List;

public interface UserMapper {
    public List<User> selectAllUser();
}

domain

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.mybatis.mapper")
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

}

5.编译代码、构建程序、执行SpringBoot 程序

7.测试SpringBoot 后端接口

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Java中使用MyBatis来复制MySQL表,你需要执行以下步骤: 1. 确保你的Java项目中已经包含了MyBatis的依赖。 2. 配置MyBatis的XML映射文件,其中包括源表和目标表的信息。 ```xml <!-- 原始表的映射配置 --> <resultMap id="originalTableMap" type="com.example.OriginalTable"> <!-- 映射原始表的字段 --> <id property="id" column="id"/> <result property="name" column="name"/> <!-- 其他字段映射 --> </resultMap> <!-- 目标表的映射配置 --> <resultMap id="newTableMap" type="com.example.NewTable"> <!-- 映射目标表的字段 --> <id property="id" column="id"/> <result property="name" column="name"/> <!-- 其他字段映射 --> </resultMap> ``` 3. 创建一个接口,定义用于复制表的方法。 ```java public interface TableMapper { @Insert("INSERT INTO new_table SELECT * FROM original_table") void copyTable(); } ``` 4. 创建一个MyBatis的配置文件,配置数据库连接和映射信息。 ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 配置数据库连接信息 --> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/your_database"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> </dataSource> </environment> </environments> <mappers> <!-- 配置接口对应的映射文件 --> <mapper resource="com/example/TableMapper.xml"/> </mappers> </configuration> ``` 5. 使用MyBatis的`SqlSessionFactory`来创建一个会话工厂。 ```java String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); ``` 6. 获取一个会话对象,并执行复制表的方法。 ```java try (SqlSession session = sqlSessionFactory.openSession()) { TableMapper tableMapper = session.getMapper(TableMapper.class); tableMapper.copyTable(); session.commit(); } ``` 这样,你就可以使用MyBatis在Java中复制MySQL表了。请确保替换示例中的数据库URL、用户名、密码以及原始表和目标表的名称,以适应你的实际情况。同时,也要根据实际情况修改映射配置、接口和XML映射文件的路径等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值