spring boot整合mybatis+mvc+jsp

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>
    <groupId>com.zg</groupId>
    <artifactId>sboot_jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sboot_jsp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

        <!--jsp解析依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>RELEASE</version>
        </dependency>

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>

        <!--Spring连接Mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--MyBatis驱动-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>

        <!--Redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>RELEASE</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zg.sbootjsp.SbootJspApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

application.yml:

## 应用名称
#spring.application.name=sboot_jsp
## 应用服务 WEB 访问端口
#server.port=8080

server:
  port: 8080

# FreeMarker
spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    #url: jdbc:mysql://192.168.153.101:3306/Book666
    url: jdbc:mysql://192.168.153.101:3306/dormitory?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    #url: jdbc:mysql://92.168.153.101:3306/Book666?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

  redis:
    host: 127.0.0.1
    password: 123456
    port: 6379
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

mybatis:
  config-locations: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.zg.sbootjsp.entities
  configuration:
    map-underscore-to-camel-case: true


logging:
  level:
    com:
      zg:
        springjsp:
          springjsp: debug

mybatis-config:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

DAO:

package com.zg.sbootjsp.Dao;

import com.zg.sbootjsp.entities.Student;
import com.zg.sbootjsp.entities.SystemAdmin;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Auther: Zg
 * @Date: 2022/8/14 - 08 - 14 - 23:17
 * @Description: com.zg.sbootjsp.Dao
 * @version: 1.0
 */
@Mapper
@Component
public interface SystemAdminDao {
    public SystemAdmin findByAccountAndPassword(HashMap<String,Object> par);
}

systemAdminMapper

<?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.zg.sbootjsp.Dao.SystemAdminDao">
    <select id="findByAccountAndPassword" parameterType="HashMap" resultType="SystemAdmin">
        select* from
        system_admin
        where username=#{username} and password=#{password}
    </select>

</mapper>
package com.zg.sbootjsp.entities;

/**
 * @Auther: Zg
 * @Date: 2022/8/14 - 08 - 14 - 17:47
 * @Description: com.zg.springjsp.springjsp.entities
 * @version: 1.0
 */
public class SystemAdmin {
    private Integer id;
    private String username;
    private String password;
    public String name;
    private String telephone;

    public SystemAdmin(Integer id, String username, String password, String name, String telephone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.telephone = telephone;
    }

    public SystemAdmin() {
    }

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "SystemAdmin{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", telephone='" + telephone + '\'' +
                '}';
    }
}

package com.zg.sbootjsp.service;

import com.zg.sbootjsp.entities.SystemAdmin;

import java.util.HashMap;

/**
 * @Auther: Zg
 * @Date: 2022/8/14 - 08 - 14 - 23:18
 * @Description: com.zg.sbootjsp.service
 * @version: 1.0
 */
public interface SystemAdminService {
    public SystemAdmin findByAccountAndPassword(HashMap<String,Object> par);
}

package com.zg.sbootjsp.service.impl;

import com.zg.sbootjsp.Dao.SystemAdminDao;
import com.zg.sbootjsp.entities.SystemAdmin;
import com.zg.sbootjsp.service.SystemAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Auther: Zg
 * @Date: 2022/8/14 - 08 - 14 - 23:19
 * @Description: com.zg.sbootjsp.service.impl
 * @version: 1.0
 */
@Service
public class SystemAdminImpl implements SystemAdminService {

    @Autowired
    SystemAdminDao systemAdminDao;

    @Override
    public SystemAdmin findByAccountAndPassword(HashMap<String, Object> par) {
        return systemAdminDao.findByAccountAndPassword(par);
    }
}

package com.zg.sbootjsp.Controller;

import com.google.common.collect.Maps;
import com.zg.sbootjsp.entities.Student;
import com.zg.sbootjsp.entities.SystemAdmin;
import com.zg.sbootjsp.service.StudentService;
import com.zg.sbootjsp.service.SystemAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Zg
 * @Date: 2022/8/14 - 08 - 14 - 4:38
 * @Description: com.zg.springjsp.springjsp.Controller
 * @version: 1.0
 */

@Controller
public class MyController {

    @Autowired
    private SystemAdminService systemAdminService;


    @RequestMapping("/")
    public String LoginView()
    {
        return "login";
    }

    @RequestMapping(value = "/account",method = RequestMethod.POST)
    //@RequestMapping({"/"},method = RequestMethod.POST)
    public String account(HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username是:"+username+",密码是"+password);
        HashMap<String, Object> pars = Maps.newHashMap();
        pars.put("username",username);
        pars.put("password",password);
        SystemAdmin admin = systemAdminService.findByAccountAndPassword(pars);
        if (admin == null)
        {
            return "index";
        }
        if (admin != null)
        {
            HttpSession session = req.getSession();
            session.setAttribute("systemAdmin", admin);
            System.out.println(admin.name);
            return "systemadmin";
        }
        return "index";
    }

    @RequestMapping(value = "/dormitoryAdmin",method = RequestMethod.GET)
    public String GetAdminmanager()
    {
        return "dormitoryadmin";
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值