springboot的数据库操作
效果:一个restful api服务器,从数据库取出省份名称给前端
1新建一个数据库(这里使用的wamp一波捆绑,反正内存够用还能浏览器操作)
2在application.properties设置数据库连接
server.port=9090
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/weblearn?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
3 springboot的数据库操作有四个层次,分别是dao,mapper,service,controler
用省份来举例子
现在数据库里长这个样子
首先新建一个region类,令每个属性与一个变量对应
publicclassRegion{
privateintid;
privateStringname;
privateintpid;
privateStringsname;
privateintlevel;
privateStringcitycode;
privateStringyzcode;
privateStringmername;
privatefloatLng;
privatefloatLat;
privateStringpinyin;
publicvoidsetName(Stringname) {
this.name=name;
}
publicvoidsetId(intid) {
this.id=id;
}
publicStringgetName() {
returnname;
}
publicintgetId() {
returnid;
}
publicfloatgetLat() {
returnLat;
}
publicfloatgetLng() {
returnLng;
}
publicintgetLevel() {
returnlevel;
}
publicintgetPid() {
returnpid;
}
publicStringgetCitycode() {
returncitycode;
}
publicStringgetMername() {
returnmername;
}
publicStringgetPinyin() {
returnpinyin;
}
publicStringgetSname() {
returnsname;
}
publicStringgetYzcode() {
returnyzcode;
}
publicvoidsetCitycode(Stringcitycode) {
this.citycode=citycode;
}
publicvoidsetLat(floatlat) {
Lat=lat;
}
publicvoidsetLevel(intlevel) {
this.level=level;
}
publicvoidsetLng(floatlng) {
Lng=lng;
}
publicvoidsetMername(Stringmername) {
this.mername=mername;
}
publicvoidsetPid(intpid) {
this.pid=pid;
}
publicvoidsetPinyin(Stringpinyin) {
this.pinyin=pinyin;
}
publicvoidsetSname(Stringsname) {
this.sname=sname;
}
publicvoidsetYzcode(Stringyzcode) {
this.yzcode=yzcode;
}
}
然后建立Mapper接口
importcom.example.demomybasits.dao.Region;
importcom.example.demomybasits.dao.users;
importorg.apache.ibatis.annotations.Select;
importorg.springframework.stereotype.Component;
importjava.util.List;
@Component
publicinterfaceregionMapper{
@Select("SELECT * FROM region WHERE pid='100000'")
List
}
这里要返回所有省份故用此sql语句(标准操作应该是嵌套子查询,这里偷懒了)
这是springboot牛逼的地方,已经帮你都配置好了,如果想了解autoconfigration的原理,推荐b站springboot的网课。
服务层接口:
importcom.example.demomybasits.dao.Region;
importorg.springframework.stereotype.Service;
importjava.util.List;
@Service
publicinterfaceRegionServer{
List
}
实现接口:
importcom.example.demomybasits.Maper.regionMapper;
importcom.example.demomybasits.dao.Region;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Repository;
importjava.util.List;
@Repository
publicclassRegionServerimplimplementsRegionServer{
@Autowired
privateregionMapperregionMapper;
@Override
publicList
try{
List
returnregions;
}catch(Exceptione){
throwe;
}
}
}
controler层
importcom.example.demomybasits.dao.Region;
importcom.example.demomybasits.server.RegionServerimpl;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importjava.util.List;
@RestController
publicclassRegionControler{
@Autowired
privateRegionServerimplregionServerimpl;
@RequestMapping("/sqltest1")
publicList
returnregionServerimpl.GetRegionList();
}
}
运行这个应用,前端用的vue,做的前后端分离项目
运行成功。