标签:static this action java ann war util tomcat ltm
<?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 http://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.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bl</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>test</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-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.47</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aaa spring.datasource.username=root spring.datasource.password=aaa spring.thymeleaf.cache=false
package com.bl.test.pojo; public class Test { private int id; private int y; private int w; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getW() { return w; } public void setW(int w) { this.w = w; } }
接口
package com.bl.test.mapper; import com.bl.test.pojo.Test; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface TestMapper { public List<Test> queryAll(); public Test queryOne(int id); public void update(Test test); public void delete(int id); public void add(Test test); }
映射文件
<?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.bl.test.mapper.TestMapper"> <resultMap id="BaseResultMap" type="com.bl.test.pojo.Test"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="y" jdbcType="INTEGER" property="y" /> <result column="w" jdbcType="INTEGER" property="w" /> </resultMap> <select id="queryAll" resultMap="BaseResultMap"> select * from test </select> <select id="queryOne" resultMap="BaseResultMap"> select * from test where id=#{id} </select> <update id="update"> update test set y=#{y},w=#{w} where id=#{id} </update> <insert id="add"> insert into test values(null,#{y},#{w}) </insert> <delete id="delete"> delete from test where id=#{id} </delete> </mapper>
接口(TestService)
package com.bl.test.service; import com.bl.test.pojo.Test; import java.util.List; public interface TestService { public List<Test> selectAll(); public Test queryOne(int id); public void update(Test t); public void delete(int id); public void add(Test t); }
实现类(TestServiceImpl)
package com.bl.test.service.impl; import com.bl.test.mapper.TestMapper; import com.bl.test.pojo.Test; import com.bl.test.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override public List<Test> selectAll() { return testMapper.queryAll(); } @Override public Test queryOne(int id) { return testMapper.queryOne(id); } @Override public void update(Test t) { testMapper.update(t); } @Override public void delete(int id) { testMapper.delete(id); } @Override public void add(Test t) { testMapper.add(t); } }
package com.bl.test.controller; import com.bl.test.pojo.Test; import com.bl.test.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Controller public class TestController { @Autowired private TestService testService; @RequestMapping("/test/queryAll") public String queryAll(Model model){ List<Test> list=testService.selectAll(); model.addAttribute("list",list); return "test"; } @RequestMapping("/test/queryOne") public String queryOne(Model model,int id){ Test t=testService.queryOne(id); model.addAttribute("test",t); return "testOne"; } @RequestMapping("/test/update") public String update(Test test){ testService.update(test); return "redirect:/test/queryAll"; } @RequestMapping("/test/add") public String add(Test test){ testService.add(test); return "redirect:/test/queryAll"; } @RequestMapping("/test/delete") public String delete(int id){ testService.delete(id); return "redirect:/test/queryAll"; } @RequestMapping("{test}") public String test(@PathVariable("test") String test){ return test; } }
package com.bl.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
1.显示全部
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1" cellpadding="0"> <tr> <th>编号</th> <th>y</th> <th>w</th> <th>操作</th> </tr> <tr th:each="list : ${list}"> <td th:text="${list.id}"></td> <td th:text="${list.y}"></td> <td th:text="${list.w}"></td> <td> <a th:href="@{/test/queryOne(id=${list.id})}">修改</a> <a th:href="@{/test/delete(id=${list.id})}">删除</a> </td> </tr> <tr> <td colspan="4"> <a th:href="@{/testAdd}">添加</a> </td> </tr> </table> </body> <script> function a(id){ alert(id); } </script> </html>
2.添加
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>添加</title> </head> <body> <form th:action="@{/test/add}"> <table border="1" cellpadding="0"> <tr> <th>y</th> <th>w</th> <th>操作</th> </tr> <tr > <td><input name="y" /></td> <td><input name="w"/></td> <td><input type="submit" value="提交" /> ></td> </tr> </table> </form> </body> <script> function a(id){ alert(id); } </script> </html>
3.修改
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>修改</title> </head> <body> <form th:action="@{/test/update}"> <table border="1" cellpadding="0"> <tr> <th>编号</th> <th>y</th> <th>w</th> <th>操作</th> </tr> <tr > <td> <input name="id" readonly th:value="${test.id}" /> </td> <td><input name="y" th:value="${test.y}" /></td> <td><input name="w" th:value="${test.w}" /></td> <td><input type="submit" value="提交" /> ></td> </tr> </table> </form> </body> <script> function a(id){ alert(id); } </script> </html>
标签:static this action java ann war util tomcat ltm
原文地址:https://www.cnblogs.com/tysl/p/10849914.html