点击蓝色“IT乾坤”关注我哟
加个“星标”,一起成长
开发环境:idea
框架:Springboot
工具:Maven
前端:Html,Thymeleaf
后台:Mybatis
数据库:Mysql
【01】点击idea的File->new->Project,点击next
【02】选择Spring Initializr,点击next
【03】配置springboot项目的坐标,项目的package,点击next
【04】配置项目所需要jar包依赖,这个地方选择会自动带入pom.xml中(可以创建项目之后自行进行配置,这里我什么也不选择,等到在项目里面配置,这样更清楚些),点击next
【05】项目名字(名字是在上面第三步已经配置好的)和项目路径配置,点击finish
【06】配置项目的maven路径,点击preferences->maven->配置maven的settings.xml文件
【07】选中右下角的自动导入依赖jar包,然后就等待依赖jar包下载完成吧
【08】查看此时的pom.xml文件,我们可以看到下面的groupId,artifactId,version,name,description正是我们在上面的第三步配置的
4.0.0modelVersion>
org.springframework.bootgroupId>
spring-boot-starter-parentartifactId>
2.3.0.RELEASEversion>
relativePath>
parent>
com.itqiankungroupId>
itqiankunartifactId>
0.0.1-SNAPSHOTversion>
itqiankunname>
Demo project for Spring Bootdescription>
1.8java.version>
properties>
org.springframework.bootgroupId>
spring-boot-starterartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-testartifactId>
testscope>
org.junit.vintagegroupId>
junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
org.springframework.bootgroupId>
spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
这里我们把它简单化,就保留最基本的,下面的java.version表示我们的项目是使用jdk1.8的,parent元素是定义父类jar包的,这个也不能少,至于为什么先不要问,以后讲springboot技术的时候会讲到
4.0.0modelVersion>
org.springframework.bootgroupId>
spring-boot-starter-parentartifactId>
2.3.0.RELEASEversion>
relativePath>
parent>
com.itqiankungroupId>
itqiankunartifactId>
0.0.1-SNAPSHOTversion>
itqiankunname>
Demo project for Spring Bootdescription>
1.8java.version>
properties>
project>
【09】查看项目结构(注意:ItqiankunApplication一定要在路径的最外层(这是因为springboot默认启动会扫描和ItqiankunApplication同一个等级的包)),resources为资源文件路径:存放propertie文件,我们会在resources文件里面设置static、templates目录,(static、templates目录存放前端页面文件:static存放 js、css文件,templates存放html等文件。)
在pom.xml里面添加web层jar包和springboot核心jar包和测试包
org.springframework.bootgroupId>
spring-boot-starterartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-webartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-testartifactId>
testscope>
org.junit.vintagegroupId>
junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
然后编写controller层代码,
package com.itqiankun.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello world";
}
}
此时可以看到controller层和ItqiankunApplication是同一个目录级别的,这一点不要忽略了
然后启动springboot,直接运行ItqiankunApplication里面的main方法,如果看到tomcat started就表示启动成功了,然后可以看到访问接口是8080
然后直接访问,可以看到成功了,这就说明springboot的基本搭建就成功了
1.首先引入jar包
org.springframework.bootgroupId>
spring-boot-starter-thymeleafartifactId>
dependency>
2.配置application.properties文件里面的Thymeleaf,告诉springboot我们的thymeleaf位置在那里
spring.thymeleaf.prefix: classpath:/templates/
2.在resources创建templates目录,在templates目录里面创建hello.html文件
html>
Indextitle>
head>
姓名:
年龄:
body>
html>
然后修改controller层代码
package com.itqiankun.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String index(Model model) {
model.addAttribute("name", "itqiankun");
model.addAttribute("age", 2);
return "hello";
}
}
重新启动结果如下所示
1.首先引入jar包
mysql和mybatis和lombok
mysqlgroupId>
mysql-connector-javaartifactId>
runtimescope>
dependency>
org.mybatis.spring.bootgroupId>
mybatis-spring-boot-starterartifactId>
1.3.2version>
dependency>
org.projectlombokgroupId>
lombokartifactId>
trueoptional>
dependency>
2.配置application.properties文件里面
2.1 mysql配置
#mysql
spring.datasource.url=jdbc:mysql://x.x.x.x:3306/myblog?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.password=xxx
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
2.2 mybatis配置,配置mapper.xml的位置所在,一般我们放在resources里面的mpper文件夹里面
#mybatis
mybatis.type-aliases-package=com.itqiankun.demo.model
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
3.在resources创建mapper目录,在mapper目录里面创建PersonMapper.xml文件
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
id>
result>
result>
resultMap>
id, name, age
sql>
4.编写mapper接口和Person类
4.1Person类
package com.itqiankun.demo.model;
import lombok.Data;
@Data
public class Person {
private Integer id;
private String name;
private Integer age;
}
4.2 PersonMapper接口
package com.itqiankun.demo.mapper;
import com.itqiankun.demo.model.Person;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface PersonMapper {
Person select(Person person);
}
5.修改controller层代码
package com.itqiankun.demo.controller;
import javax.annotation.Resource;
import com.itqiankun.demo.mapper.PersonMapper;
import com.itqiankun.demo.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@Resource
private PersonMapper personMapper;
@GetMapping("/hello")
public String index(Model model) {
Person person = new Person();
person.setId(1);
model.addAttribute("person",personMapper.select(person));
return "hello";
}
}
6.修改html代码
html>
Indextitle>
head>
姓名:
年龄:
body>
html>
7.数据库数据
CREATE TABLE `person` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `person` (`id`, `name`, `age`)
VALUES
(1, 'itqiankun', 200);
此时在重启springboot,结果如下所示:
到这里从前端到数据库的spring入门教程就全部讲解完成来,希望对眼前对你有所帮助,看的高兴的同学就点个喜欢吧,感谢。