==============================entity=========================================================
package com.jb.pub.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* 用户
*/
@Entity
public class Pubuser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String aguid; //
private String pguid; //
private Integer pxxh; //
@ManyToOne
@JoinColumn(name="guid_user")
private Pubaccinfo guid_gs; // 所属公司
private String guid_group; // 用户组 停用
private String guid_bm; // 部门
private String guid_job; // 职务
private String guid_rsgz; // 工资计算方式 停用
@Column(length = 30)
private String acode; // 编码
@Column(length = 150)
private String phone; // 联系电话
@Column(length = 150)
private String mobilephone; // 手机
@Column(length = 255)
private String remark; // 备注
private String photo; // 照片
}
@Entity
public class Pubaccinfo implements Serializable {
private static final long serialVersionUID = -5067656436423109109L;
@Id
private String aguid; //
private Integer pxxh; //
private String pguid; //用户
@Column(length = 150)
private String aname; // 名称
@OneToMany(cascade=CascadeType.ALL,mappedBy="guid_user",fetch=FetchType.LAZY)
private Set<Pubuser > guid_user=new HashSet<Pubuser >();//列权限
}
========================================================dao==============================================================
package com.jb.xs.Dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import com.jb.entity.PubComp;
import com.jb.xs.Dao.TestCaseDao;
@Service
public class TestCaseDaoImpl implements TestCaseDao {
private EntityManager em = null;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Override
public void delete(PubComp pc) {
em.remove(pc);// 删除的方法
em.persist(pc);// 添加的方法
em.merge(pc);//更新的方法
em.createQuery("select p from PubComp p ").getResultList();// 查询的方法
}
@Override
public List<PubComp> getPubComp(String sql) {
// em.find("select p from PubComp");
return (List<PubComp>) em.createQuery(sql)
.getResultList();
}
@Override
public Object getMaxLvl() {
// em.find("select p from PubComp");
return em.createQuery("select max(p.status) from PubComp p ");
}
@Override
public void save(PubComp pc) {
em.persist(pc);
}
@Override
public void update(PubComp pc) {
em.merge(pc);
}
}
=====================================================services================================================================
package com.jb.xs.services.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jb.entity.PubComp;
import com.jb.xs.Dao.TestCaseDao;
import com.jb.xs.services.TestCaseServices;
@Service
public class TestCaseServicesImpl implements TestCaseServices {
@Autowired
private TestCaseDao testCaseDao;//需要调用的接口对象(dao)
@Override
public List<PubComp> queryPubComp(String sql) {
return testCaseDao.getPubComp(sql);
}
@Override
public void delete(PubComp pc) {
testCaseDao.delete(pc);
}
@Override
public void save(PubComp pc) {
testCaseDao.save(pc);
}
@Override
public void update(PubComp pc) {
testCaseDao.update(pc);
}
public TestCaseDao getTestCaseDao() {
return testCaseDao;
}
public void setTestCaseDao(TestCaseDao testCaseDao) {
this.testCaseDao = testCaseDao;
}
@Override
public Object getMaxStatus() {
return testCaseDao.getMaxLvl();
}
}
============================================action===========================================================================
package com.jb.xs.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jb.entity.PubComp;
import com.jb.entity.TreeNode;
import com.jb.xs.services.TestCaseServices;
@Controller
@RequestMapping("student")
public class IndexController2 {
@Autowired
//需要调用的业务类的接口对象(services)
private TestCaseServices testCaseDaoImpl;
@RequestMapping
public String load(ModelMap modelMap){
List<PubComp> pcList=testCaseDaoImpl.queryPubComp("select p from PubComp p ");
modelMap.put("list", pcList); //需要返回的数据(pcList)
return "success"; //需要返回的页面,(success.jsp)
}
@RequestMapping("add")
public String add(HttpServletRequest request, ModelMap modelMap) throws Exception{
PubComp pc=new PubComp();
Random rd=new Random();
String aguid=String.valueOf(rd.nextFloat());
pc.setAguid(aguid);
pc.setStatus(1);
pc.setAname("一级菜单:"+aguid);
//一级菜单
testCaseDaoImpl.save(pc);
//二级菜单
String aguid2=String.valueOf(rd.nextFloat());
pc.setAguid(aguid2);
pc.setPguid(aguid);
pc.setAname("二级菜单:"+aguid2);
pc.setStatus(2);
testCaseDaoImpl.save(pc);
//三级菜单
String aguid3=String.valueOf(rd.nextFloat());
pc.setAguid(aguid3);
pc.setPguid(aguid2);
pc.setAname("三级菜单:"+aguid3);
pc.setStatus(3);
testCaseDaoImpl.save(pc);
return "success";//需要返回的页面,(success.jsp)
}
public TestCaseServices getTestCaseDaoImpl() {
return testCaseDaoImpl;
}
public void setTestCaseDaoImpl(TestCaseServices testCaseDaoImpl) {
this.testCaseDaoImpl = testCaseDaoImpl;
}
}
===========================================applicationContext.xml============================================================
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
">
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.jb.*" />
<mvc:annotation-driven />
<!--解决web.xml拦截非action路径的问题-->
<mvc:default-servlet-handler/>
<!--spring管理拦截器 -->
<mvc:interceptors>
<bean class="com.jb.tool.JToolSystemInterceptor" />
</mvc:interceptors>
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- jsp存放的路径,在WebContent文件夹下可以不写 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 指定跳转的页面为.jsp格式 -->
<property name="suffix" value=".jsp" />
</bean>
<!--事物托管 springMVC3.2需要的jar为aspectj-1.6.10.jar、aspectjweaver-1.6.9.jar底版本的会报错 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 事物通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 事物切入 -->
<aop:config>
<aop:pointcut id="cut"
expression="execution(* com.jb.xs.services.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut" />
</aop:config>
</beans>
=====================================================web.xml=========================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>dispather</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispather</servlet-name>
<!-- 访问的路径-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
==========================================persistence.xml这里是hibernate访问数库=
<?xml
version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!--定义持久单元-->
<persistence-unit name="jubcrm" transaction-type="RESOURCE_LOCAL">
<!--这里采用Hibernate提供的持久化类-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!--定义持久化属性-->
<properties>
<!--定义方言、fetch深度、是否显示sql-->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表-->
<property name="hibernate.hbm2ddl.auto" value="update" />
<!--定义数据库连接的相关属性-->
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.url" value="jdbc:postgresql://svr:5432/eqdb" />
<!--定义数据库连接的用户名、密码等-->
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="1.0" />
</properties>
</persistence-unit>
</persistence>
====================================================jsp=========================================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="student/add" method="post">
<input type="submit" value="添加" />
</form>
<form action="student" method="post">
<input type="submit" value="查询" />
</form>
</body>
</html>