First, let’s take a look at the rendering we want to achieve
Next, the blogger’s code structure
Go directly to the topic:
1. Create a database table
2. Create a project
Create a Maven Project with Eclipse
3. Import hibernate-related rack packages into the project
pom.xml
4.0.0
Maven_Hibenate01
Maven_Hibenate01
war
0.0.1-SNAPSHOT
Maven_Hibenate01 Maven Webapp
http://maven.apache.org
-Dfile.encoding=UTF-8
junit
junit
4.12
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.hibernate
hibernate-core
5.2.10.Final
org.apache.struts
struts2-core
2.5.12
org.apache.struts
struts2-convention-plugin
2.5.12
net.sf.json-lib
json-lib
2.4
jdk15
mysql
mysql-connector-java
5.1.18
Maven_Hibenate01
4.Configure hibernate.cfg.xml
root
123
jdbc:mysql://localhost:3306/test?characterEncoding=utf8
com.mysql.jdbc.Driver
5.Create entity class User.java And the mapping file User.hbm.xml (the mapping file and the entity object must be in the same package)
package com.yhl.entity;
public class User {
private int u_id;//number
private String u_name;//User name
private String u_sex;//sex
public User() {
}
public User(int u_id) {
this.u_id = u_id;
}
public User(String u_name, String u_sex) {
super();
this.u_name = u_name;
this.u_sex = u_sex;
}
public User(int u_id, String u_name, String u_sex) {
this.u_id = u_id;
this.u_name = u_name;
this.u_sex = u_sex;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getU_sex() {
return u_sex;
}
public void setU_sex(String u_sex) {
this.u_sex = u_sex;
}
}
6. Write Dao layer
Interface UserDao.java
package com.yhl.dao;
import java.util.List;
import com.yhl.entity.User;
public interface UserDao {
/**
* Query all
*/
public List getAll();
/**
* Add a line of information
*/
public int AddUser(User user);
/**
* Delete a row of information based on ID
*/
public int DeleteUser(int u_id);
/**
* Modify user information
*/
public int UpadteUser(User user);
}
Implementation class UserDaoImp.java
package com.yhl.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.yhl.entity.User;
public class UserDaoImp implements UserDao{
private Configuration configuration;//1. Instantiate the configuration object and load the mapping file hibernate.cfg.xml
private SessionFactory sessionFactory;//2. Create a session factory
private Session session;//3. Create a session
private Transaction transaction;//4. Open the transaction
/**
* view all
*/
public List getAll() {
configuration = new Configuration().configure();
sessionFactory = configuration. buildSessionFactory();
session = sessionFactory. openSession();
transaction = session.beginTransaction();
//Query all
List list=session.createCriteria(User.class).list();
//Submit things
transaction.commit();
session. close();
sessionFactory. close();
return list;
}
/**
* Add to
*/
public int AddUser(User user) {
configuration = new Configuration().configure();
sessionFactory = configuration. buildSessionFactory();
session = sessionFactory. openSession();
transaction = session.beginTransaction();
//Add a line of information
User users=new User(user. getU_name(), user. getU_sex());
Serializable Serializable = session. save(users);
int a=(Integer) Serializable;
System.out.println(a);
//Submit things
transaction.commit();
session. close();
sessionFactory. close();
return 0;
}
/**
* delete
*/
public int DeleteUser(int uid) {
configuration = new Configuration().configure();
sessionFactory = configuration. buildSessionFactory();
session = sessionFactory. openSession();
transaction = session.beginTransaction();
//delete a line of information
User user=new User();
user.setU_id(uid);
session. delete(user);
//Submit things
transaction.commit();
session. close();
sessionFactory. close();
return 0;
}
/**
* Revise
*/
public int UpadteUser(User user) {
configuration = new Configuration().configure();
sessionFactory = configuration. buildSessionFactory();
session = sessionFactory. openSession();
transaction = session.beginTransaction();
//Revise
User users=session.get(User.class, user.getU_id());
users.setU_name(user.getU_name());
users.setU_sex(user.getU_sex());
session. update(users);
//Submit things
transaction.commit();
session. close();
sessionFactory. close();
return 0;
}
}
7. Write Action
package com.yhl.action;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
import net.sf.json.JSONArray;
public class GetAllUserAction extends ActionSupport{
@Override
public String execute(){
System.out.println("View all Ations coming in...");
UserDao userDao=new UserDaoImp();
try {
HttpServletResponse resp = ServletActionContext. getResponse();
resp.setContentType("text/html;charset=UTF-8");
JSONArray jsonArray=JSONArray.fromObject(userDao.getAll());
PrintWriter pw;
pw = resp. getWriter();
pw.write(jsonArray.toString());
pw. close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.yhl.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
import com.yhl.entity.User;
public class AddAction extends ActionSupport{
HttpServletResponse resp=ServletActionContext.getResponse();
HttpServletRequest req=ServletActionContext.getRequest();
UserDao dao=new UserDaoImp();
private int u_id;
private String u_name;
private String u_sex;
@Override
public String execute() throws Exception {
resp.setContentType("text/html; charset=UTF-8");
req.setCharacterEncoding("UTF-8");
dao.AddUser(new User(u_id, u_name, u_sex));
return null;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getU_sex() {
return u_sex;
}
public void setU_sex(String u_sex) {
this.u_sex = u_sex;
}
}
package com.yhl.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
public class DeleteAction extends ActionSupport{
HttpServletResponse resp=ServletActionContext.getResponse();
HttpServletRequest req=ServletActionContext.getRequest();
UserDao dao=new UserDaoImp();
private int u_id;
@Override
public String execute() throws Exception {
System.out.println("Deleted Action came in...");
resp.setContentType("text/html; charset=UTF-8");
req.setCharacterEncoding("UTF-8");
System.out.println(u_id);
dao. DeleteUser(u_id);
return null;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
}
package com.yhl.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
import com.yhl.entity.User;
public class UpdateAction extends ActionSupport {
HttpServletResponse resp=ServletActionContext.getResponse();
HttpServletRequest req=ServletActionContext.getRequest();
UserDao dao=new UserDaoImp();
private int u_id;
private String u_name;
private String u_sex;
@Override
public String execute() throws Exception {
System.out.println("The modified Action came in...");
resp.setContentType("text/html; charset=UTF-8");
req.setCharacterEncoding("UTF-8");
dao.UpadteUser(new User(u_id, u_name, u_sex));
return null;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getU_sex() {
return u_sex;
}
public void setU_sex(String u_sex) {
this.u_sex = u_sex;
}
}
8. Write struts.xml configuration
9. Configure web.xml
Archetype Created Web Application
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
10. Write jsp pages
Hibenate+Struts2+Maven+EasyUI realizes adding, deleting, checking and modifying
var DBindex;
$(function(){
$('#dg').datagrid({
url:'selectuser.action',
loadMsg: 'Desperately loading...',
columns: [[
{field:'u_id',title:'number',width:100,align:'center'},
{field:'u_name',title:'username',width:100,align:'center',"editor":{type:'validatebox'}},
{field:'u_sex',title:'gender',width:100,align:'center',"editor":{type:'validatebox'}},
]],onDblClickRow:function(index,row){
DBindex=index;
$('#dg').datagrid('beginEdit',index);
},onClickRow:function(index,row){
DBindex=index;
}, toolbar: [{
iconCls: 'icon-add',
text: "Add",
handler: function(){
DBindex=0;
$('#dg').datagrid('insertRow',{
index:0, // index starts from 0
row: {}
});
$('#dg').datagrid('beginEdit',0);
}
},'-',{
iconCls: 'icon-remove',
text: "Delete",
handler: function(){
var row=$('#dg').datagrid("getRows")[DBindex];
if(row!=null){
var u_id=row["u_id"];
$.post("deltetuser.action",
{"u_id":u_id},
function(data){
$('#dg').datagrid('reload');
});
}
}
},'-',{
iconCls: 'icon-save',
text: "Save",
handler: function(){
$('#dg').datagrid('endEdit',DBindex);
var row=$('#dg').datagrid("getRows")[DBindex];
if(row!=null){
var u_name=row["u_name"];
var u_sex=row["u_sex"];
$.post("adduser.action",
{"u_name":u_name,"u_sex":u_sex},
function(data){
$('#dg').datagrid('reload');
});
}
}
},'-',{
iconCls: 'icon-edit',
text: "Modify",
handler: function(){
$('#dg').datagrid('endEdit',DBindex);
var row=$('#dg').datagrid("getRows")[DBindex];
if(row!=null){
var u_id=row["u_id"];
var u_name=row["u_name"];
var u_sex=row["u_sex"];
$.post("updateuser.action",
{"u_id":u_id,"u_name":u_name,"u_sex":u_sex},
function(data){
$('#dg').datagrid('reload');
});
}
}
}]
});
});
Attachment: EasyUI download address
function(data){
$(‘#dg’).datagrid(‘reload’);
});
}
}
},’-‘,{
iconCls: ‘icon-edit’,
text: “Modify”,
handler: function(){
$(‘#dg’).datagrid(‘endEdit’,DBindex);
var row=$(‘#dg’).datagrid(“getRows”)[DBindex];
if(row!=null){
var u_id=row[“u_id”];
var u_name=row[“u_name”];
var u_sex=row[“u_sex”];
$.post(“updateuser.action”,
{“u_id”:u_id,”u_name”:u_name,”u_sex”:u_sex},
function(data){
$(‘#dg’).datagrid(‘reload’);
});
}
}
}]
});
});
Attachment: EasyUI download address