springBoot 快速入手
server:
port: 8888
spring:
datasource:
# 数据库连接配置
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mskk?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password:
# mybatis的相关配置
mybatis:
# mapper 配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.geng.demo.mybatis.entity
package com.example.demo.entity;
public class Company {
private int id;
private String name;
public Company(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo.mapper;
import com.example.demo.entity.Company;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CompanyMapper {
List<Company> findAll();
}
<?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.demo.mapper.CompanyMapper">
<resultMap id="CompanyMap" type="com.example.demo.entity.Company">
<result column="id" property="id"/>
</resultMap>
<select id="findAll" resultMap="CompanyMap">
SELECT * FROM company
</select>
<!-- <select id="getUserById" resultMap="userResultMap">-->
<!-- SELECT * FROM user WHERE id = #{id}-->
<!-- </select>-->
</mapper>
package com.example.demo.service;
import com.example.demo.entity.Company;
import com.example.demo.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CompanyService {
@Autowired
private CompanyMapper companyMapper;
public List<Company> findAll(){
return companyMapper.findAll();
}
}
package com.example.demo.controller;
import com.example.demo.entity.Company;
import com.example.demo.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CompanyController {
@Autowired
private CompanyService companyService;
@RequestMapping("/abc")
public List<Company> findAll(){
return companyService.findAll();
}
}
原文地址:https://blog.csdn.net/czxvzxzczcc/article/details/137544134
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!