JSP显示新闻
2021-02-05 13:18
标签:jdb servlet 创建 static name tee data apache tran 使用IDEA作为编译环境,项目结构如下: 数据库结构如下: DBConn类作为连接数据库和显示数据库中存储新闻的方法的设计类 HelloWorld类作为请求映射的响应和逻辑业务的处理类,即Controller。 myXML/EmployeeMapper.xml作为mybatis的mapper的SQL语句映射。 myXML/mybatis-config.xml则是MySQL数据库的连接操作。 登录页面 新闻显示 码云地址:项目码云地址 JSP显示新闻 标签:jdb servlet 创建 static name tee data apache tran 原文地址:https://www.cnblogs.com/tianhaoqi/p/13124548.html项目结构
主要JAVA类介绍
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
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
配置文件介绍
?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>
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">
property name="driver" value="com.mysql.jdbc.Driver"/>
property name="url" value="jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8"/>
property name="username" value="root"/>
property name="password" value="123456"/>
dataSource>
environment>
environments>
mappers>
mapper resource="myXML/EmployeeMapper.xml"/>
mappers>
configuration>
运行效果