idea下servlet+jsp+mybatis项目演示

第一次写博客,不懂排版,不喜勿喷。

idea下的servlet+jsp项目,映射框架为mybatis,使用了maven进行项目管理。

idea下servlet+jsp+mybatis项目演示_第1张图片
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xyy</groupId>
    <artifactId>restful</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml
                
            
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
            
        
    

mabatis.config.xml如下:

<?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>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true"/>
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>

User类

package bean;

import java.io.Serializable;

public class User implements Serializable {

    private Integer uid;

    private String username;

    private  String password;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

UserDao如下:

package dao;

import bean.User;
import java.util.List;

public interface UserDao {
    List<User> findUser();
}

UserMapper.xml如下:

<?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">
<!-- 首先来解析 namespace: 命名空间,此属性通常用来映射Dao层接口. -->
<mapper namespace="dao.UserDao">
    <!-- id: 对应Dao层接口方法名 parameterType:指定输入参数类型 -->
    <!-- useGeneratedKeys="true"把新增加的主键赋值到自己定义的keyProperty(id)中 -->
    <select id="findUser" resultType="bean.User">
        select * from User
    </select>
</mapper>

UserServlet如下:

package servlet;


import bean.User;
import dao.UserDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;


/**
 * Userservlet
 */
@WebServlet(name="testServlet", urlPatterns={"/testServlet"},loadOnStartup=1)
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----------------");
        req.setCharacterEncoding("utf-8");
        try {
            String resource = "mybatis-config.xml";
            URL o = Thread.currentThread().getContextClassLoader().getResource("");
            String path = o.getPath();
            InputStream inputStream;
            inputStream = new FileInputStream(new File(path + resource));
            // 2、根据配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            UserDao sutdentDao = (UserDao)sqlSessionFactory.openSession().getMapper(UserDao.class);
            List<User> list = sutdentDao.findUser();
            System.out.println(list);
            sqlSessionFactory.openSession().close();
            req.setAttribute("user",list);
            req.getRequestDispatcher( "welcom.jsp").forward(req, resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("---------1--------");
        req.setCharacterEncoding("utf-8");
        super.doGet(req, resp);
    }
}

Web.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

index.jsp如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    hello,jsp
</body>
</html>

welcom.jsp如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    登陆成功
<div>
    <table>
        <c:forEach items="${user}" var="u">
            <tr>
                <td>${u.uid}</td>
                <td>${u.username}</td>
                <td>${u.password}</td>
            </tr>
        </c:forEach>
    </table>
</div>
</body>
</html>

表数据:
idea下servlet+jsp+mybatis项目演示_第2张图片
结果演示:
idea下servlet+jsp+mybatis项目演示_第3张图片

idea下servlet+jsp+mybatis项目演示_第4张图片
idea下servlet+jsp+mybatis项目演示_第5张图片

其他想要说的话。

先建立一个maven项目,然后在main目录下建一个文件下命名为web。
在web目录下建WEB-INF,web.xml,index.jsp,welcom.jsp等文件和目录(参考本文最上面的项目目录结构图),建好之后按idea快捷键(ctrl+shift+alt+s),把web目录变成web项目。点击+号,在目录列表里找到web,点击,然后apply。这样你的项目结构就和博主的一样了。
如图:
idea下servlet+jsp+mybatis项目演示_第6张图片
引入tomcat

idea下servlet+jsp+mybatis项目演示_第7张图片
看第一张项目目录图右上角有个tomcat的标志,这是加进来之后显示的,不加之前是edit configarations,点进去,点击+号,选择自己的tomcat的目录,点击apply。(博主讲的不详细,可以百度参考其他人的,有专门讲tomcat怎么加进idea里)

至此,所有工作已经完成。

本文链接:https://blog.csdn.net/qq_44909430/article/details/102901895

你可能感兴趣的:(项目演示示例,servlet,jsp)