Java后台开发笔记

首先搭建环境,idea+mySQL+mybatis+springBoot创建的demo。

配置文件

resources目录下application.yml文件中

#端口
server:
  port: 8080

#连接本地数据库
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/student_table?useUnicode=true&&characterEncoding=utf-8&&serverTimezone=UTC #注意设置编码格式
    username: root
    password: *****

#配置mybatis 该配置节点为独立的节点
mybatis:
  mapper-locations: classpath:mybatis/*.xml  # 注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.zjh.api.demo.entity  # 注意:对应实体类的路径
  configuration:
    map-underscore-to-camel-case: true # 数据库字段下划线自动转驼峰

然后创建一个mybatis文件夹,再在下面创建UserMapper.xml文件,返回数据类型和sql语句




    

创建entity文件夹,创建UserEntity文件
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserEntity {
    public String name;
    public int year;
    public int teacherId;
}

创建mapper文件夹,创建UserMapper
@Repository
public interface UserMapper {
    //获取用户信息
    List getUserInfo();
}
创建service文件夹,创建UserService
@Repository
public class UserService {

    @Autowired
    UserMapper mMapper;

    public List getUserInfo(){
        return mMapper.getUserInfo();
    }
}
创建controller文件夹,创建UserController文件
@RestController
public class UserController {

    @Autowired
    private UserService mUserService;

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public BaseEntity getUserInfo() {
        List userList = mUserService.getUserInfo();
        Map map = new HashMap<>();
        map.put("student", userList);
        if (userList != null) {
            return BaseEntity.success(map);
        }
        return new BaseEntity().setMsg("请求失败").setState(201);
    }
}

你可能感兴趣的:(Java后台开发笔记)