项目结构
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框架无异