1.用idea创建项目
2.在main程序的同级下创建一个接口包constroller
1)创建一个名为UserController的类
package com.example.demo.constroller;
/**
*用户模板
*USER表,字段
主键,名字,密码,手机号,创建时间,修改时间
用java工具引用数据库
*/
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class UserController {
@Autowiredprivate UserMapper userMapper;
@RequestMapping("/getName")public String getName(Long id){
return userMapper.selectNameById(id);
}
}
3.在main同级下创建一个mapper的包
创建一个接口UserMapper
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
/**
* 查询
*/
@Select("SELECT `name` FROM user WHERE id= #{id}")
public String selectNameById(Long id);
4.在application.yml里面填入
spring:
datasource:
url:jdbc:mysql://127.0.0.1:3306/restaurant?serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
5.在pox..xml中填入
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version>
</dependency>
填入完成后点击maven中的更新