本帖最后由 sunlarry 于 2012-12-04 18:06:23 编辑 表结构如下:
create table SYSUSER
(
loginid VARCHAR2(20) not null,
pwd VARCHAR2(20),
)
MODEL:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "SYSUSER", schema = "TEST")
public class Sysuser implements java.io.Serializable {
private String loginid;
private String pwd;
@Id
@Column(name = "LOGINID", unique = true, nullable = false, length = 20)
public String getLoginid() {
return this.loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
@Column(name = "PWD", length = 20)
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
SERVICE:
import java.util.ArrayList;
import java.util.List;
public interface SysuserService {
public void save(Sysuser sysuser);
public void delete(Sysuser sysuser);
public void update(Sysuser sysuser);
public ArrayList<Sysuser> find(final String username, final String password);
}
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class SysuserServiceImpl implements SysuserService {
private EntityManager entityManager;
public void delete(Sysuser sysuser) {
sysuser = entityManager.getReference(Sysuser.class, sysuser.getLoginid());
entityManager.remove(sysuser);
}
public void save(Sysuser sysuser) {
entityManager.persist(sysuser);
}
public void update(Sysuser sysuser) {
entityManager.merge(sysuser);
}
public ArrayList<Sysuser> find(String username, String password) {
String jpql="select u from Sysuser u where u.loginid like :username and u.pwd like :password";
Query query=getEntityManager().createQuery(jpql);
query.setParameter("username", username);
query.setParameter("password", password);
List<Sysuser> list=query.getResultList();
return (ArrayList<Sysuser>) list;
}
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
action:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;