JSP显示新闻

项目结构

使用IDEA作为编译环境,项目结构如下:

JSP显示新闻

数据库结构如下:

JSP显示新闻

 

 

 主要JAVA类介绍

DBConn类作为连接数据库和显示数据库中存储新闻的方法的设计类

package myJava;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public final class DBConn {
    @Test
    //连接数据库
    public  static SqlSession getSqlSession() {
        String resource = "myXML/mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession session = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return session;
    }

    //校验用户信息
    public static Boolean check(Employee emp){
        SqlSession sqlSession = getSqlSession();
        try {
            //selectOne的第一个参数为Mapper.xml的mapper的namespace+id
            //参数二为映射的需要传入的参数
            Employee employee = sqlSession.selectOne(
                    "EmployeeMapper.selectEmployee_name", emp.getName());
            if (employee!=null && employee.getPassword().equals(emp.getPassword()))
                return true;
            else
                return false;
        } finally {
            free(sqlSession);
        }
    }

    //获取数据库的全部数据
    public static List<News> getAllNews(){
        SqlSession sqlSession = getSqlSession();
        List<News> list = sqlSession.selectList("EmployeeMapper.selectNews");
        free(sqlSession);
        return list;
    }
    public static News getNewsById(int id){
        SqlSession sqlSession = getSqlSession();
        News news = sqlSession.selectOne("EmployeeMapper.selectById",id);
        free(sqlSession);
        return news;
    }


    //释放Sqlsession
    public static void free(SqlSession sqlSession){
        sqlSession.close();
    }
}

 HelloWorld类作为请求映射的响应和逻辑业务的处理类,即Controller。

package mySpringMVC;
import myJava.DBConn;
import myJava.Employee;
import myJava.Modify;
import myJava.News;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/utf8")
public class HelloWorld{

    @RequestMapping("/helloWorld")
    public String showNews(News news){
        System.out.println("Hello World!");
        System.out.println(news);
//        String exec = new ActionEnter(request, rootPath).exec();
        return "success";
    }

    @RequestMapping("/back")
    public ModelAndView back(){
        return getAllData();
    }

    @RequestMapping("/login")
    public ModelAndView checkLogin(Employee employee){
        //判断用户账号和密码
        Boolean isTrue = DBConn.check(employee);
        if (isTrue)
        {
            return getAllData();
        }
        else
            return new ModelAndView("loginAgain");//在视图解析器中添加前缀和后缀
    }

    @RequestMapping("/Info")
    public ModelAndView newsInfo(@RequestParam("id") int id){
        News news =DBConn.getNewsById(id);
        ModelAndView modelAndView = new ModelAndView("info");
        modelAndView.addObject("one",news);
        return modelAndView;
    }

    public ModelAndView getAllData(){
        List<News> list = DBConn.getAllNews();
        //创建返回数据的ModelAndView
        ModelAndView modelAndView = new ModelAndView("newsPublish");
        modelAndView.addObject("list",list);
        return modelAndView;
    }
}

 

配置文件介绍

myXML/EmployeeMapper.xml作为mybatis的mapper的SQL语句映射。

?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="EmployeeMapper">
    <select id="selectEmployee_name" parameterType="String" resultType="myJava.Employee">
        select * from employee where name = #{name}
   </select>

    <select id="selectNews" resultType="myJava.News">
        select * from news
   </select>

    <select id="selectById" parameterType="int" resultType="myJava.News">
        select * from news where id = #{id}
    </select>
</mapper>

myXML/mybatis-config.xml则是MySQL数据库的连接操作。

<?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="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true&amp;serverTimezone=GMT%2B8-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="myXML/EmployeeMapper.xml"/>
    </mappers>
</configuration>

运行效果

 登录页面

JSP显示新闻

 

 新闻显示

JSP显示新闻

 

 码云地址:项目码云地址

JSP显示新闻

上一篇:.NETCore 之 中间件 02


下一篇:Android TV开发总结(五)TV上屏幕适配总结