使用springboot进行增删改查

以你之姓@ 2023-06-13 03:27 35阅读 0赞

场景启动器:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.mybatis.spring.boot</groupId>
  11. <artifactId>mybatis-spring-boot-starter</artifactId>
  12. <version>2.1.1</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.github.pagehelper</groupId>
  16. <artifactId>pagehelper-spring-boot-starter</artifactId>
  17. <version>1.2.12</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <scope>runtime</scope>
  23. </dependency>

配置:

  1. server.port=8080
  2. server.servlet.context-path=/sboot1113_xjx
  3. #mysql
  4. spring.datasource.url=jdbc:mysql://localhost:3306/ms?serverTimezone=UTC&characterEncoding=utf-8
  5. spring.datasource.username=root
  6. spring.datasource.password=123456
  7. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  8. #mybatis
  9. mybatis.type-aliases-package=com.neusoft.sboot1113_xjx.domain
  10. mybatis.mapperlocations=classpath:mybatis/mapper/*Mapper.xml
  11. pagehelper.helper-dialect=mysql
  12. pagehelper.params=count=countSql
  13. pagehelper.reasonable=true
  14. pagehelper.support-methods-arguments=true

实体类

  1. private Integer id;
  2. private String name;
  3. private String sex;
  4. private String idcard;
  5. private String emcard;
  6. private String phone;
  7. private String post;
  8. private Integer sal;
  9. private String bkcard;

mapper.java

  1. public void insertEmployee(Employee employee);
  2. int deleteByPrimaryKey(Integer id);
  3. int updateByPrimaryKey(Employee record);
  4. List<Employee> selectByExample(EmployeeExample example);
  5. Employee selectByPrimaryKey(Integer id);

mapper.java 类 注意在类上加 @Mapper 注解

mapper.xml

  1. <insert id="insertEmployee" parameterType="employee">
  2. INSERT INTO employee (name,sex,idcard,emcard,phone,post,sal,bkcard) VALUES(#{name},#
  3. {sex},#{idcard},#{emcard},#{phone},#{post},#{sal},#{bkcard})
  4. </insert>
  5. <delete id="deleteByPrimaryKey" parameterType="Integer" >
  6. delete from employee
  7. where id = #{id,jdbcType=INTEGER}
  8. </delete>
  9. <update id="updateByPrimaryKeySelective" parameterType="com.neusoft.sboot1113_xjx.domain.Employee" >
  10. update employee
  11. <set >
  12. <if test="name != null" >
  13. name = #{name,jdbcType=VARCHAR},
  14. </if>
  15. <if test="sex != null" >
  16. sex = #{sex,jdbcType=CHAR},
  17. </if>
  18. <if test="idcard != null" >
  19. idcard = #{idcard,jdbcType=VARCHAR},
  20. </if>
  21. <if test="emcard != null" >
  22. emcard = #{emcard,jdbcType=VARCHAR},
  23. </if>
  24. <if test="phone != null" >
  25. phone = #{phone,jdbcType=VARCHAR},
  26. </if>
  27. <if test="post != null" >
  28. post = #{post,jdbcType=VARCHAR},
  29. </if>
  30. <if test="sal != null" >
  31. sal = #{sal,jdbcType=INTEGER},
  32. </if>
  33. <if test="bkcard != null" >
  34. bkcard = #{bkcard,jdbcType=VARCHAR},
  35. </if>
  36. </set>
  37. where id = #{id,jdbcType=INTEGER}</update>
  38. <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.neusoft.sboot1113_xjx.domain.EmployeeExample" >
  39. select
  40. <if test="distinct" >
  41. distinct
  42. </if>
  43. <include refid="Base_Column_List" />
  44. from employee
  45. <if test="_parameter != null" >
  46. <include refid="Example_Where_Clause" />
  47. </if>
  48. <if test="orderByClause != null" >
  49. order by ${orderByClause}
  50. </if>
  51. </select>
  52. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Integer" >
  53. select
  54. <include refid="Base_Column_List" />
  55. from employee
  56. where id = #{id,jdbcType=INTEGER}
  57. </select>

service.java

  1. public interface EmployeeService {
  2. List<Employee> selectByExample(EmployeeExample example);
  3. public void insertEmployee(Employee employee);
  4. Employee selectByPrimaryKey(Integer id);
  5. int updateByPrimaryKeySelective(Employee record);
  6. int deleteByPrimaryKey(Integer id);
  7. }

service实现类

  1. @Service
  2. public class EmployeeServiceImpl implements EmployeeService{
  3. @Autowired
  4. public EmployeeMapper employeeMapper;
  5. @Override
  6. public List<Employee> selectByExample(EmployeeExample example) {
  7. // TODO Auto-generated method stub
  8. return employeeMapper.selectByExample(example);
  9. }
  10. @Override
  11. public void insertEmployee(Employee employee) {
  12. // TODO Auto-generated method stub
  13. employeeMapper.insertEmployee(employee);
  14. }
  15. @Override
  16. public Employee selectByPrimaryKey(Integer id) {
  17. // TODO Auto-generated method stub
  18. return employeeMapper.selectByPrimaryKey(id);
  19. }
  20. @Override
  21. public int updateByPrimaryKeySelective(Employee record) {
  22. // TODO Auto-generated method stub
  23. employeeMapper.updateByPrimaryKeySelective(record);
  24. return 1;
  25. }
  26. @Override
  27. public int deleteByPrimaryKey(Integer id) {
  28. // TODO Auto-generated method stub
  29. employeeMapper.deleteByPrimaryKey(id);
  30. return 1;
  31. }
  32. }

controller

  1. @Controller
  2. public class EmployeeController {
  3. @Autowired
  4. private EmployeeService employeeService;
  5. @RequestMapping("/list")
  6. public String list(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
  7. System.out.println("list():");
  8. Page page = PageHelper.startPage(pageNum, 5);
  9. List<Employee> employeeList = employeeService.selectByExample(null);
  10. PageInfo<Employee> pageInfo = new PageInfo<>(employeeList,5);
  11. model.addAttribute("pageCount", page.getPages());
  12. model.addAttribute("pageNum", page.getPageNum());
  13. model.addAttribute("pageSize", page.getPageSize());
  14. model.addAttribute("pageInfo",pageInfo);
  15. return "list";
  16. }
  17. @RequestMapping("/add")
  18. public String add() {
  19. return "add";
  20. }
  21. @RequestMapping("/addSeckill")
  22. public String addProduct(Employee employee) {
  23. System.out.println("seckill"+employee);
  24. employeeService.insertEmployee(employee);
  25. return "redirect:list";
  26. }
  27. @RequestMapping("/toUpdate")
  28. public ModelAndView toUpdate(Integer id) {
  29. System.out.println("update id:"+id);
  30. ModelAndView mav=new ModelAndView();
  31. mav.addObject("employee",employeeService.selectByPrimaryKey(id));
  32. mav.setViewName("update");
  33. return mav;
  34. }
  35. @RequestMapping("/update")
  36. public String update(Employee employee) {
  37. System.out.println("update id:"+employee);
  38. employeeService.updateByPrimaryKeySelective(employee);
  39. return "redirect:list";
  40. }
  41. @RequestMapping("/delete")
  42. public String delete(Integer id) {
  43. System.out.println("delete id:"+id);
  44. employeeService.deleteByPrimaryKey(id);
  45. return "redirect:list";
  46. }
  47. }

list.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <table>
  9. <a th:href="@{/add}">添加数据</a>
  10. <tr>
  11. <th>id</th>
  12. <th>name</th>
  13. <th>sex</th>
  14. <th>idcard</th>
  15. <th>emcard</th>
  16. <th>phone</th>
  17. <th>post</th>
  18. <th>sal</th>
  19. <th>bkcard</th>
  20. </tr>
  21. <tr th:each="e:${pageInfo.list}">
  22. <td th:text="${e.id}"></td>
  23. <td th:text="${e.name}"></td>
  24. <td th:text="${e.sex}"></td>
  25. <td th:text="${e.idcard}"></td>
  26. <td th:text="${e.emcard}"></td>
  27. <td th:text="${e.phone}"></td>
  28. <td th:text="${e.post}"></td>
  29. <td th:text="${e.sal}"></td>
  30. <td th:text="${e.bkcard}"></td>
  31. <td><a th:href="@{/delete(id=${e.id})}">delete</a>||<a th:href="@{/toUpdate(id=${e.id})}">toUpdate</a></td><br/>
  32. </tr>
  33. <tr>
  34. <td colspan="5">总页数<span th:text="${pageCount}"></span>,当前第<span th:text="${pageNum}"></span>页,每页显示<span th:text="${pageSize}"></span>条数据</td>
  35. <!-- <a th:href="@{/list(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.pages}:1)}">上一页</a> -->
  36. <th:block th:each="i :${#numbers.sequence(1, pageInfo.pages)}">
  37. <a th:href="@{/list(pageNum=${i})}">
  38. <span th:class="${pageInfo.pageNum == i}">
  39. <th:block th:text="${i}"></th:block>
  40. </span>
  41. </a>
  42. </th:block>
  43. <!-- <a th:href="@{/list(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a> -->
  44. </tr>
  45. </table>
  46. </body>
  47. </html>

add.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form th:action="@{/addSeckill}" method="post">
  9. name:<input type="text" name="name"/><br>
  10. sex:<input type="text" name="sex"/><br>
  11. idcard:<input type="text" name="idcard"/><br>
  12. emcard:<input type="text" name="emcard"/><br>
  13. phone:<input type="text" name="phone"/><br>
  14. post:<input type="text" name="post"/><br>
  15. sal:<input type="text" name="sal"/><br>
  16. bkcard:<input type="text" name="bkcard"/><br>
  17. <input type="submit" value="add">
  18. </form>
  19. </body>
  20. </html>

update.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form action="" th:action="@{/update}" method="post" th:object="${employee}">
  9. id:<input type="text" name="" value="" th:field="*{id}"/><br>
  10. name:<input type="text" name="" value="" th:field="*{name}"/><br>
  11. idcard:<input type="text" name="" value="" th:field="*{idcard}"/><br>
  12. emcard:<input type="text" name="" value="" th:field="*{emcard}"/><br>
  13. phone:<input type="text" name="" value="" th:field="*{phone}"/><br>
  14. post:<input type="text" name="" value="" th:field="*{post}"/><br>
  15. sal:<input type="text" name="" value="" th:field="*{sal}"/><br>
  16. bkcard:<input type="text" name="" value="" th:field="*{bkcard}"/><br>
  17. <input type="submit" value="update"/>
  18. </form>
  19. </body>
  20. </html>

发表评论

表情:
评论列表 (有 0 条评论,35人围观)

还没有评论,来说两句吧...

相关阅读