【汇智学堂】Springboot操作Redis之二(Springboot+jsp+mybatis数据准备)

pom.xml

<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.huizhi</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>
        <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>

<!--        访问.html。如果是.jsp需要将它注释-->
<!--            <dependency>-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-starter-thymeleaf</artifactId>-->
<!--            </dependency>-->

<!--       添加jsp的maven依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-core</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在这里插入图片描述
Home.jsp

<%--
  Created by IntelliJ IDEA.
  User: soft
  Date: 2020/1/8
  Time: 13:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>列表</title>
</head>
<body>

<div id="div1">
    <a href="user/add">新增</a>
    用户信息<br>
    <table>
        <thead>
        <th>id</th>
        <th>姓名</th>
        <th>地址</th>
        <th>操作</th>
        </thead>
        <tbody>
        <c:forEach items="${userList}" var="user">
            <tr>
                <td>${user.id}&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>${user.userName}&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>${user.address}&nbsp;&nbsp;&nbsp;&nbsp;<br></td>
                <td><a href="/user/delete/${user.id}">删除</a>&nbsp;&nbsp;&nbsp;<a href="/update/${user.id}">修改</a></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

</div>
</body>
</html>

UserController:

package com.huizhi.demo.controller;

import com.huizhi.demo.po.User;
import com.huizhi.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    //注入用户Service
    @Autowired
    private UserService userService;
    //查询所有用户

//    public List<User> getAllUsers(){
//        List<User> list=this.userService.getAllUsers();
//        return list;
//    }
    @RequestMapping("/userList")
    public String getAllUsers(Model model){
        List<User> list=this.userService.getAllUsers();
        model.addAttribute("userList",list);
        //return "user";//静态页面访问
        return "Home";//jsp页面
    }
    //删除用户
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id,Model model){
        this.userService.deleteUser(id);

        List<User> list=this.userService.getAllUsers();
        model.addAttribute("userList",list);
        //return "user";//静态页面访问
        return "Home";//jsp页面
    }
}

UserMapper

package com.huizhi.demo.dao;

import com.huizhi.demo.po.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Mapper
@ResponseBody
public interface UserMapper {
    //查询所有用户
    @Select("select*from tb_user")
    List<User> getAllUsers();
    //删除所有用户
    @Delete("delete from tb_user where id=#{id}")
    void delte(Integer id);
}

User

package com.huizhi.demo.po;

public class User {
    private Integer id;
    private String userName;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

UserService:

package com.huizhi.demo.service;
import com.huizhi.demo.po.User;
import java.util.List;
public interface UserService {
    //查询所有
    List<User> getAllUsers();
    //删除数据
    void deleteUser(Integer id);
}

UserServiceImpl:

package com.huizhi.demo.service.impl;

import com.huizhi.demo.dao.UserMapper;
import com.huizhi.demo.po.User;
import com.huizhi.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    //注入用户Mapper
    @Autowired(required=false)
    private UserMapper userMapper;
    //查询所有用户
    public List<User> getAllUsers(){
        return this.userMapper.getAllUsers();
    }
    //删除用户
    public void deleteUser(Integer id){
        System.out.println("删除了id为"+id+"的用户");
        this.userMapper.delte(id);
    }
}

application.properties:

#数据库指向,school是我的数据库名,改成自己的
spring.datasource.url=jdbc:mysql://localhost:3306/mymis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#出现mysql jdbc标红报错问题------>去pom.xml中删除mysql-connector-java下面的runtime那一行
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database = mysql

#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.huizhi.demo.po
发布了268 篇原创文章 · 获赞 47 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/103890877