准备工作:
安装JDK,安装MySql,安装Idea; 安装Gradle4.7版本, 安装插件:Spring Assistant
1. 新建项目:直接通过IDEA构建
第二步:取名
第三步:选择web。
第四步:点击完成即可
第五步:设置gradle本地,也可以使用默认。
界面会一致下载jar包,过程会中断,大家耐心点,一般30分钟以内。
加载好了之后目录结构:
2. 官网构建,导入IDEA。
点击绿色按钮,生成zip包,将生成的包,导入IDEA即可。
3. 整合SpringBoot和MyBatis
数据库设置一张表:
项目目录结构:
各个文件如下:
mytest
import com.seventh.icecastle.db.Acco; import com.seventh.icecastle.service.AccoInterface; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/mytest") public class mytest { @Autowired AccoInterface accoInterface; @GetMapping(value = "/all") public List<Acco> getall(){ return accoInterface.getAccoAll(); } }
Acco
public class Acco { private int id; private String name; private Double money; 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; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }
AccoDao
import com.seventh.icecastle.db.Acco; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface AccoDao { List<Acco> getAll(); }
AccoInterfaceImpl
import com.seventh.icecastle.db.Acco; import com.seventh.icecastle.mapper.AccoDao; import com.seventh.icecastle.service.AccoInterface; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class AccoInterfaceImpl implements AccoInterface { @Autowired AccoDao accoDao; @Override public List<Acco> getAccoAll() { return accoDao.getAll(); } }
AccInterface
import com.seventh.icecastle.db.Acco; import java.util.List; public interface AccoInterface { List<Acco> getAccoAll(); }
AccountCashMapper.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.seventh.icecastle.mapper.AccoDao"> <select id="getAll" resultType="com.seventh.icecastle.db.Acco"> select a.* from account a </select> </mapper>
application.properties
server.port= 8080 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/work_db?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=********* mybatis.configuration.database-id=mysql mybatis.mapperLocations=classpath:mapper/*.xml
浏览器访问:
ok, 初步框架搭完成。 后续还可以将其他框架加入进来。