SSH中的SH配置

IDEA+Hibernate+Struts2

笔者能力有限,只能用老版Hibernate和老版Struts2.

我们在IDEA中选择Struts2中的2.3版本我是直接下了jar包加入进去的,还有自动下载Hibernate3.6中的。之后进行配置。

之后再加入connector我用的是Mysql5.7版本所以用了对应的connector。

之后选择左下角的persistence,右键下面的一个文件之后generate Mapping。

之后选择链接服务器 记得?useSSL=false。之后配置一下hibernate.cfg.xml中的文件,其中username和password和前面四个是自己设置的。

还要选择mapping的class和resource

1
2
3
4
5
6
7
8
9
10
11
12
<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>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="hibernate.StudentEntity"/>
<mapping resource="hibernate/StudentEntity.hbm.xml"/>

之后把jstl中的jar包加入到tomcat文件夹中。这样就不用每次配置了。

之后在preference中搜索dtds

1
http://java.sun.com/jsp/jstl/core

url填写这个 之后再搜索你下载好的jstl中的c.tld文件 慢慢找就在里面

下载链接

http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

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
package hibernate;

import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
@GeneratedValue
private int id;
@Column(name="name")
private String name;
@Column(name="address")
private String address;
public StudentEntity(){}
public StudentEntity(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;
StudentEntity that = (StudentEntity) 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);
}
}

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 com.listAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import hibernate.Student;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class StudentDAO {
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
@SuppressWarnings("unchecked")
public List<Student> getStudents()
{

List<Student> students = new ArrayList<Student>();
try
{
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]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return students;
}
public void addStudent(Student student)
{
session.save(student);
}
}
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 hibernate.Student;

public class AddStudent extends ActionSupport
implements ModelDriven<Student>{
Student student = new Student();
List<Student> students = new ArrayList<Student>();
StudentDAO dao = new StudentDAO();
@Override
public Student getModel() {
return student;
}
public String execute()
{
dao.addStudent(student);
return "success";
}
public String listStudents()
{
students = dao.getStudents();
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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="hibernate-default">
<!--defaultStackHibernate里面的拦截器会识别出@SessionTarget@TransactionTarget等标注,
然后将hibernate注入进去-->
<action name="addStudent" method="execute"
class="com.listAction.AddStudent">
<result name="success" type="redirect">
listStudents
</result>
</action>
<action name="listStudents" method="listStudents"
class="com.listAction.AddStudent">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
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
72

import hibernate.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(123321);
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 = session.createSQLQuery("select *from student").list();
for (Iterator iter = students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
}
}