項目結構

1新建Spring Start Project
找到File——>New ——>Other…spring start project點擊next新建項目

輸入自己的包名項目名

根據個人情況選擇

2.配置文件
配置默認端口號
server.port=8888
配置數據源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
配置掃描包
mybatis.type-aliases-package=com.boot.pojo
mybatis.mapper-locations=classpath:Mapper/*.xml
spring.thymeleaf.prefix=classpath:/template/
POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.boot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.啟動類以及controller
啟動類
@RestController
@MapperScan(basePackages= {"com.boot.Mapper"})
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
Controller
@Controller
public class TestController {//controller和SSM的一樣
@Autowired private TestService testService; //查詢數據 @RequestMapping("/findAll") public String findAll(Model model) { List<TestPojo> list = testService.findAll(); System.out.println("進入方法" + list.get(0)); model.addAttribute("li", list); return "index"; } //測試 @ResponseBody @RequestMapping("/getname") public String userList(@RequestParam(value = "id") String id) { return testService.UserList(Long.parseLong(id)); } //刪除數據 @RequestMapping("/del") public String del(@RequestParam("id") int id) { testService.del(id);//調用service中的del方法 return "redirect:/findAll";//重定向到findAll方法 } //跳轉到添加頁面 @RequestMapping("/addindex") public String del() { return "Add"; } //添加數據 @RequestMapping("/add") public String Add(TestPojo testpojo){ this.testService.Add(testpojo); return "redirect:/findAll"; } //跳轉到數據更新頁面 @RequestMapping("/updateuser") public String Update() { return "update"; } @RequestMapping("/updateselect") public String select(Model model,int id) { TestPojo test =testService.select(id); model.addAttribute("li", test); return "update"; } //更新數據 @RequestMapping("/upda") public String family(TestPojo testpojo ) { System.out.println(testpojo.getAge()); testService.update(testpojo); return "redirect:/findAll"; } }
4.HTML頁面
<body>
<table border="1" >
<tr>
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
<td>入學時間</td>
<td id="yingcang">地址</td>
<td colspan="2" >操作</td>
</tr>
<tr th:each="list:${li}">
<td th:text=${list.id} > </td>
<td th:text="${list.name}"></td>
<td th:text="${list.age}"></td>
<td th:text="${list.birthday}"></td>
<td id="cang" th:text="${list.adress}"></td>
<td>
<a th:href="@{/del(id=${list.id})}" > 刪除</a>
</td>
<td>
<a th:href="@{/updateselect(id=${list.id})}">修改</a>
</td>
</tr>
</table>
<input type="button" value="顯示" onclick="ceshow()" >
<a th:href="@{/addindex}">增加數據</a>
</body>
剩余接口以及實現類與SSM框架無異
