L06——MyBatis读取数据库_01


学习目标:

        完成前端访问后端URL,从数据库内读取数据完成显示

项目新建

        新建项目

             

        新建模块

        

                添加依赖项

        添加为maven项目

 

        数据库建立

        代码编写

                建立包

                        java文件下新建包

                        包的结构如下:

                

                建立目录

                                resource文件下新建目录

                        

                建立对应Java文件

                                其中UserMapper是Java类接口

                        

        编写代码

                controller/HelloController

                        

package com.example.mybatis.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 {

//注入 UserService
@Autowired
private UserService userService;

/**
* 响应 GET 请求,返回所有用户信息
*
* @return 返回所有用户信息的列表
*/
@GetMapping()
public List<User> hello() {
//调用 UserService 中的 selectAllUser 方法获取所有用户信息
return userService.selectAllUser();
}

}

                domain/User

package com.example.mybatis.domain;

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;
}
}

                mapper/UserMapper

package com.example.mybatis.mapper;

import com.example.mybatis.domain.User;

import java.util.List;

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

                service/UserService

package com.example.mybatis.service;

import com.example.mybatis.domain.User;
import com.example.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> selectAllUser(){
return userMapper.selectAllUser();
}
}

                MybatisApplication

package com.example.mybatis;

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);
}

}

                application.yml

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username: Mysql账号
password: Mysql密码

mybatis:
typeAliasesPackage: com.example.mybatis.domain
mapperLocations: classpath:mapper/*.xml

                UserMapper.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.mybatis.mapper.UserMapper">
<resultMap id="UserResult" type="User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="createTime" column="create_time" />
</resultMap>

<select id="selectAllUser" resultMap="UserResult">
select * from user
</select>

</mapper>

问题

        新建文件选项中没有.yml文件

               看这个解决的: idea如何创建yml文件_idea创建yml文件-CSDN博客

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值