81,091
社区成员
发帖
与我相关
我的任务
分享
package sy.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sy.model.Tuser;
import sy.service.UserServiceI;
public class TestHibernate {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] { "classpath:spring.xml", "classpath:spring-hibernate.xml" });
UserServiceI userService=(UserServiceI) ac.getBean("userService");
Tuser t=new Tuser();
t.setId("0014");
t.setName("s3114");
t.setPwd("123");
userService.save(t);
}
}
package sy.action;
import java.util.Date;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import sy.model.Tuser;
import sy.service.UserServiceI;
@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "userAction")
public class UserAction {
private static final Logger logger = Logger.getLogger(UserAction.class);
private UserServiceI userService;
public UserServiceI getUserService() {
return userService;
}
@Autowired
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
public void test() {
logger.info("进入Action");
userService.test();
}
// public void addUser(){
// Tuser t=new Tuser();
// t.setId(UUID.randomUUID().toString());
// t.setName("tt");
// t.setPwd("343");
// t.setCreatedatetime(new Date());
// userService.save(t);
// }
}
package sy.dao.impl;
import java.io.Serializable;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sy.dao.UserDaoI;
import sy.model.Tuser;
@Repository("userDao")
public class UserDaoImpl implements UserDaoI {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Serializable save(Tuser t) {
return this.sessionFactory.getCurrentSession().save(t);
}
}
package sy.dao;
import java.io.Serializable;
import sy.model.Tuser;
public interface UserDaoI {
public Serializable save(Tuser t);
}
package sy.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
/**
* Tuser entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "TUSER", schema = "SSHE", uniqueConstraints = @UniqueConstraint(columnNames = "NAME"))
public class Tuser implements java.io.Serializable {
// Fields
private String id;
private String name;
private String pwd;
private Date createdatetime;
private Date modifydatetime;
// Constructors
/** default constructor */
public Tuser() {
}
/** minimal constructor */
public Tuser(String id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
/** full constructor */
public Tuser(String id, String name, String pwd, Date createdatetime, Date modifydatetime) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.createdatetime = createdatetime;
this.modifydatetime = modifydatetime;
}
// Property accessors
@Id
@Column(name = "ID", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "NAME", unique = true, nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "PWD", nullable = false, length = 32)
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATEDATETIME", length = 7)
public Date getCreatedatetime() {
return this.createdatetime;
}
public void setCreatedatetime(Date createdatetime) {
this.createdatetime = createdatetime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFYDATETIME", length = 7)
public Date getModifydatetime() {
return this.modifydatetime;
}
public void setModifydatetime(Date modifydatetime) {
this.modifydatetime = modifydatetime;
}
}
package sy.service.impl;
import java.io.Serializable;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sy.dao.UserDaoI;
import sy.model.Tuser;
import sy.service.UserServiceI;
@Service(value="userService")
public class UserServiceImpl implements UserServiceI {
private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
private UserDaoI userDao;
public UserDaoI getUserDao() {
return userDao;
}
@Autowired
public void setUserDao(UserDaoI userDao) {
this.userDao = userDao;
}
@Override
public void test() {
logger.info("wwww");
}
@Override
public Serializable save(Tuser t) {
return userDao.save(t);
}
}
package sy.service;
import java.io.Serializable;
import sy.model.Tuser;
public interface UserServiceI {
public void test();
public Serializable save(Tuser t);
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="sy.dao,sy.service" />
</beans>
<?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>
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" />
<package name="basePackage" extends="struts-default">
</package>
</struts>