学习目标:
完成前端访问后端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博客