struts2 hibernate mysql 实例_Struts2 + Spring + hibernate 框架搭成实例

1、准备Jar包:

2、配置beans.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

destroy-method="close">

value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />

cn/pp/bean/Employee.hbm.xml

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.hbm2ddl.auto=update

hibernate.show_sql=true

hibernate.format_sql=false

3、Spring实例

import java.util.List;

import cn.pp.params.Employee;

public interface EmployeeIService {

public boolean save(Employee employee);

public boolean update(Employee employee);

public Employee find(String userId);

public boolean delete(String... userIds);

public List findAll();

}

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import org.hibernate.Criteria;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import cn.pp.params.Employee;

import cn.pp.service.EmployeeIService;

@Service

@Transactional

public class EmployeeServiceImpl implements EmployeeIService {

private static Logger logger = Logger.getLogger(Employee.class);

@Resource(name="sessionFactory")

SessionFactory factory;

@Override

public boolean save(Employee employee) {

try {

factory.getCurrentSession().save(employee);

} catch (Exception e) {

logger.error(e.getMessage());

return false;

}

return true;

}

@Override

public boolean update(Employee employee) {

try {

factory.getCurrentSession().update(employee);

} catch (Exception e) {

logger.error(e.getMessage());

return false;

}

return true;

}

//@Transactional(propagation=Propagation.NOT_SUPPORTED)

@Override

public Employee find(String userId) {

try {

return (Employee)factory.getCurrentSession().get(Employee.class,userId);

} catch (Exception e) {

logger.error(e.getMessage());

}

return null;

}

@Override

public boolean delete(String... userIds) {

try {

for (String userId : userIds) {

Employee employee=(Employee)factory.getCurrentSession().load(Employee.class,userId);

factory.getCurrentSession().delete(employee);

}

} catch (Exception e) {

logger.error(e.getMessage());

return false;

}

return true;

}

@SuppressWarnings("unchecked")

//@Transactional(propagation=Propagation.NOT_SUPPORTED)

@Override

public List findAll() {

try {

//return factory.getCurrentSession().createQuery("from Employee").list();

Criteria criteria=factory.getCurrentSession().createCriteria(Employee.class);

return criteria.list();

} catch (Exception e) {

logger.error(e.getMessage());

}

return null;

}

}

数据库表映射文件

/p>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

测试用例:

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.List;

import org.junit.BeforeClass;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.pp.params.Employee;

import cn.pp.service.EmployeeIService;

public class EmployeeIServiceTest {

private static EmployeeIService employeeIService;

@BeforeClass

public static void initContext() {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

employeeIService = (EmployeeIService) context.getBean("employeeServiceImpl");

}

@Test

public void testSave() {

Employee e=new Employee();

e.setUserName("杰克");

e.setAddress("北京");

Calendar c = new GregorianCalendar(1991, 5, 5, 0,0,0);

Date date=c.getTime();

e.setBirthday(date);

employeeIService.save(e);

}

@Test

public void testUpdate() {

Employee e=employeeIService.find("4028826a507f660001507f6601620000");

e.setUserName("螺丝");

employeeIService.update(e);

}

@Test

public void testFind() {

Employee e=employeeIService.find("4028826a507f660001507f6601620000");

System.out.println(e.getUserName());

}

@Test

public void testDelete() {

employeeIService.delete("4028826a507f660001507f6601620000");

}

@Test

public void testFindAll() {

List list=employeeIService.findAll();

for(Employee e : list){

System.out.println(e.getUserName());

employeeIService.delete(e.getUserId());

}

}

}

4、Struts配置:

/WEB-INF/message.jsp

/WEB-INF/list.jsp

测试实例:

action:

// spring 默认scope 是单例模式 @Scope("prototype") 表示每次接收一个请求创建一个Action对象

@Controller @Scope("prototype")

public class EmployeeAction {

@Resource EmployeeIService employeeIService;

private String message;

private HttpServletRequest request;

private ServletContext context;

private Employee employee;

public EmployeeAction(){

request=ServletActionContext.getRequest();

context=ServletActionContext.getServletContext();

}

public String list(){

List list=employeeIService.findAll();

request.setAttribute("list", list);

return "list";

}

public String add(){

if(employee!=null){

employeeIService.save(employee);

}else{

setMessage("部分人员信息为空!");

return "message";

}

setMessage("添加成功");

return "message";

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public Employee getEmployee() {

return employee;

}

public void setEmployee(Employee employee) {

this.employee = employee;

}

}

package cn.pp.params;

import java.util.Date;

public class Employee {

private String userId;

private String userName;

private String address;

private Date birthday;

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

list.jsp:

人员列表

用户名住址生日

${item.userName }${item.address }${item.birthday }

暂无数据

add.jsp:

添加人员
用户名
住址
生日

message.jsp:

${message }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值