SpringBoot实践(⼀):基于springboot的web开发和实例
这⾥直接使⽤idea的new project⽅式新建⼀个spirng init,然后基于当前配置完成spring mvc的web页⾯开发测试,包含⼏个⼩的实例;
⼀、环境配置
使⽤springboot进⾏web开发,需了解⼀些常规知识:
前端知识:Html,CSS,JavaScript
后端框架:springboot,SpringMVC,thymeleaf
持久层及数据交互:MyBatis,RDB
资源负载:H5,Ng
开发⼯具:IDEA,MAVEN
添加thymeleaf的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
标准⽬录如下 ,其中package的controller下放⼀些⾃定义的restAPI接⼝定义,mapper⽬录下放⼀些mybatis的查询接⼝(interface),⽤于定义查询sql的返回类型和⼊参类型,model⽬录下放⼀些与后端表⼀⼀对应的beans,service中存放mapper中的接⼝实例和接⼝⽅法的实现,xxspringApplication.j
ava是程序的主类⼊⼝,不⽤动;
resources/mybatis下放⼀些mybatis的xml⽂件,⽂件名与mapper中的接⼝⼀⼀对应,static下放图⽚等静态资源,template下放html页⾯;
简单实例本地不需要装mysql,idea⾃带了h2数据库,使⽤jdbc连接⽅式,⾸先创建数据库、schema、table,在创建h2数据库时候connection type选择Embedded,path路径替换成~/.xxxx.mv.db,xxxx为你当前的项⽬名称,jdbc的连接串则相应变化了,然后设置user和password就可以测试通过;
resources下的application.properties是整个项⽬的配置⽂件,这⾥因为存储使⽤h2,持久层⽤的mybatis,所以需要增加application.properties配置:
spring.datasource.url=jdbc:h2:~/testspring
spring.datasource.username=yzg
spring.datasource.password=123456
spring.datasource.driver-class-name=org.h2.Driver
mybatis.mapper-locations=classpath:mybatis/*l
⼆、简单实例
springboot中有必要知道的常⽤注解:
@SpringBootApplication springboot程序⼊⼝
@RestController:控制器,只反馈return后的字符串
@RequestMapping("/first"):请求映射器,返回页⾯return后的html;
第1个例⼦是⽆DAO交互的,实现前端请求传参name,页⾯得到name后显⽰,controller类是SecondController.java如下,返回second页⾯;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SecondController {
@GetMapping("/second")
public String second(@RequestParam(name="name") String name,Model model){
model.addAttribute("name",name);
return "second";
}
}
second静态页⾯:second.html
<!DOCTYPE HTML>
<html xmlns:th="">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
第2个例⼦涉及到数据库交互,⾸先model/⽬录下有个类Student.java如下,只有四个字段ID,NAME,AGE,SEX,写⼏条测试数据;
del;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
public class Students {
private int id;
private String name;
private int age;
private String sex;
}
在controller/路径下,写⼀个StudentController.java,实现对于数据库Student表的查询,包括有id和⽆id的全部查询,这⾥使⽤的是@RestController注释⽽⾮上个例⼦中的@Controller,因为后者会返回页⾯,⽽前者是⼀个rest接⼝,通过
localhost:8080/testspring/getStudentAll这个http请求,实现查询;
del.Students;
stspring.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.*;
@RestController
@RequestMapping("/testspring")
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/getStudent/{id}")
public Students getStudent(@PathVariable int id ){
return studentService.SelectByID(id);
}
@RequestMapping(value = {"/getStudentsAll"})
@ResponseBody
public List<Map<String,Object>> getStudents(){
List<Students> listStudent=(List<Students>) studentService.SelectAll();
List<Map<String, Object>> studentLists = new ArrayList<>();
Iterator iterator=listStudent.iterator();
while(iterator.hasNext()){
Map<String, Object> map = new HashMap<>();
Students st= (Students) ();
map.put("id",st.getId());
map.put("name",st.getName());
map.put("sex",st.getSex());
map.put("age",st.getAge());
studentLists.add(map);
}
return studentLists;
}
}
mapper/路径下写⼀个针对StudentMapper.interface的接⼝,定义查询函数的返回类型,这⾥相对应的写2个⽅法;
stspring.mapper;
del.Students;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface StudentMapper {
List<Students> SelectAll();
Students SelectByID(int id);
}
在 service/路径下写⼀个StudentService.java,⽤来继承接⼝,实现接⼝中预定义的函数,使⽤Autowired注解;
stspring.mapper.StudentMapper;
del.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public List<Students> SelectAll(){
return studentMapper.SelectAll();
}
public Students SelectByID(int id){
return studentMapper.SelectByID(id);
}
}
在resources/mybatis下新建⼀个l⽂件,⽤于具体的sql语句查询⽤:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd">
<mapper namespace="stspring.mapper.StudentMapper">
<!-- 可根据⾃⼰的需求,是否要使⽤ -->
<resultMap type="del.Students" id="StudentMap">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<select id="SelectAll" resultMap="StudentMap">
select *
from STUDENTS
</select>jquery框架定义
<select id="SelectByID" parameterType="INTEGER" resultMap="StudentMap">
select *
from STUDENTS
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
第3个例⼦使⽤静态页⾯显⽰地图,controller类ThirdController.java如下,与数据库中Student表字段⼀⼀对应,属性使⽤lombok增加set和get⽅法;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论