jsp作业

jsp第五次上机

题目1

实现Hibernate的增加数据功能

题目2

实现Hibernate的查找数据功能

项目构成

image-20181111155849991

DAO是项目的功能接口类,DAOImpl是DAO功能接口的实现类。AddStudent是主要的Action类,有两个方法,一个是增加学生数据一个是显示学生数据。Student和hbm由IDE自动根据数据库生成。

java类函数代码

接口类

1
2
3
4
5
6
7
8
package njtech.edu.DAO;
import java.util.List;
import njtech.edu.model.Student;

public interface StudentDAO {
public void saveStudent(Student student);
public List<Student> getAll();
}

实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package njtech.edu.DAO.impl;


import njtech.edu.model.Student;
import njtech.edu.DAO.StudentDAO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Iterator;
import java.util.List;

public class StudentDAOImpl extends BaseDAOImpl implements StudentDAO{
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
public StudentDAOImpl(){}
public void init() {
sessionFactory = new Configuration().
configure("hibernate.cfg.xml").
buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
}
@Override
public void saveStudent(Student student) {
init();
session.save(student);
tx.commit();
}

@Override
public List<Student> getAll() {
init();
List<Student> students = session.createQuery("from Student").list();
return students;
}
}

Action类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.listAction;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import njtech.edu.DAO.StudentDAO;
import njtech.edu.DAO.impl.StudentDAOImpl;
import njtech.edu.model.Student;

public class AddStudent extends ActionSupport implements ModelDriven {
private Student student = new Student();
private List<Student> students = new ArrayList<Student>();
private StudentDAO dao = new StudentDAOImpl();
public Object getModel() {
return student;
}
public String execute()
{
dao.saveStudent(student);
return "success";
}
public String listStudents()
{
students = dao.getAll();
return "success";
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}

model类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package njtech.edu.model;

import javax.persistence.*;
import java.util.Objects;
public class Student {
private int id;
private String name;
private String address;
public Student(){}
public Student(int id, String name, String address){
this.id = id;
this.name = name;
this.address = address;
}
public int getId() {
return id;
}

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

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student that = (Student) o;
return id == that.id &&
Objects.equals(name, that.name) &&
Objects.equals(address, that.address);
}

@Override
public int hashCode() {
return Objects.hash(id, name, address);
}
}

对应的数据库mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="njtech.edu.model.Student" table="student" schema="test">
<id name="id">
<column name="id" sql-type="int(11)"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(100)" length="100" not-null="true"/>
</property>
<property name="address">
<column name="address" sql-type="varchar(100)" length="100" not-null="true"/>
</property>
</class>
</hibernate-mapping>

Hibernate配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useSSL=false</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>


<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="njtech.edu.model.Student"/>
<mapping resource="njtech/edu/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

struts2配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" extends="struts-default">
<action name="addStudent" method="execute"
class="com.listAction.AddStudent">
<result name="success" type="redirect">
listStudents.action
</result>
</action>
<action name="listStudents" method="listStudents"
class="com.listAction.AddStudent">
<result name="success">index.jsp</result>
</action>
</package>
</struts>

测试类,可以用来测试Hibernate是否运行

需要加载对应的Junit的jar包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import njtech.edu.model.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;

public class DAOtest {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
@Before
public void init() {
sessionFactory = new Configuration().
configure("hibernate.cfg.xml").
buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
}
//增加
@Test
public void insert() {
Student ue = new Student();
ue.setName("Anny");
ue.setId(7777);
ue.setAddress("wodediercichangshi");
session.save(ue);
tx.commit();
}
//修改
@Test
public void update() {
Student user = (Student) session.get(Student.class, new Integer(2));
user.setName("Penny");
session.update(user);
tx.commit();
session.close();
}
//查找
@Test
public void getById() {
Student user = (Student) session.get(Student.class, new Integer(8));
tx.commit();
session.close();
System.out.println("ID号:" + user.getId() + ";用户名:" + user.getName() +
";密码:" + user.getAddress() );
}
//删除
@Test
public void delete() {
Student user = (Student) session.get(Student.class, new Integer(6));
if(user != null){
session.delete(user);
}
tx.commit();
session.close();
}

@Test
public void gogogo(){
List<Student> students = students = session.createQuery("from Student").list();
for (Iterator iter = students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
}
}

显示页面

写了两种显示方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
<s:head />
</head>
<body>
<s:form action="addStudent">
<s:textfield name="student.id" label="ID"/>
<s:textfield name="student.name" label="Name"/>
<s:textfield name="student.address" label="Address"/>
<s:submit/>
<hr/>
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>
</tr>
<s:iterator value="students">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="address"/></td>
</tr>
</s:iterator>
</table>
</s:form>
<s:if test="students.size() > 0">
<table border="1px" cellpadding="8px">
<tr>
<th>Student Id</th>
<th>Name</th>
<th>Address</th>
</tr>
<s:iterator value="students">
<tr>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="address" /></td></td>
</tr>
</s:iterator>
</table>
</s:if>
</body>
</html>

运行界面

image-20181111160639164

image-20181111160757748

输入信息之后submit加载到数据库中,并显示数据库中所有数据。

image-20181111160739052