栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

华南农业大学Linux课程综合实验——用Java语言,实现从MySQL读取内容并显示在浏览器上

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

华南农业大学Linux课程综合实验——用Java语言,实现从MySQL读取内容并显示在浏览器上

前言

用Java语言实现从Linux的MySQL读取内容并显示在浏览器上

项目结构

项目代码

StudentController.java

package com.controller;

import com.pojo.Student;
import com.service.StudentService;

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.IOException;
import java.util.List;



@WebServlet("/studentServlet")
public class StudentController extends HttpServlet {
    private StudentService service=new StudentService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List student=service.selectStudents();
        req.setAttribute("student",student);
        req.getRequestDispatcher("/student.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}


StudentMapper.java

package com.mapper;

import com.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;



@Mapper
@Repository
public interface StudentMapper {
    @Select("select * from student")
    @ResultMap("studentResultMap")
    List selectStudents();
}


Student.java

package com.pojo;



public class Student {
    private Integer id;
    private String studentId;
    private String studentName;

    public Integer getId() {
        return id;
    }

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

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Student(Integer id, String studentId, String studentName) {
        this.id = id;
        this.studentId = studentId;
        this.studentName = studentName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", studentId='" + studentId + ''' +
                ", studentName='" + studentName + ''' +
                '}';
    }
}


StudentService.java

package com.service;

import com.mapper.StudentMapper;
import com.pojo.Student;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class StudentService {
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    public List selectStudents(){
        SqlSession sqlSession=factory.openSession();

        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        List student=mapper.selectStudents();

        sqlSession.close();

        return student;
    }
}


SqlSessionFactoryUtils.java

package com.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载而自动执行,且只执行一次

        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}

CommunityServletInitializer.java

package com;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class CommunityServletInitializer extends SpringBootServletInitializer {

    //tomcat访问该方法作为入口来运行该项目
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class); //这里指出该项目的main方法所在的类
    }
}


DemoApplication.java

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(com.DemoApplication.class, args);
    }
}


StudentMapper.xml







    
        
        
    

mybatis-config.xml




    
    
        
    

    
        
            
            
                
                
                
                 
                
                
            
        
    
    
        
        
    

student.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title



    ${status.count}
    ${stu.studentId}
    ${stu.studentName}



pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
        
            com.alibaba
            fastjson
            1.2.58
        
        
            jstl
            jstl
            1.2
        
        
            org.mybatis
            mybatis
            3.5.2
        

    

    war
    
        ROOT
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号