SpringMVC3.2+JPA使用注解的方式环境搭建
==============================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>
SpringMVC3.2+JPA使用注解的方式环境搭建的更多相关文章
- SpringMVC+JPA使用注入的方式环境搭建
----------------------------------------------------DAO--------------------------------------------- ...
- SpringData JPA的学习笔记之环境搭建
一.环境搭建 1.加入jar包 spring jar+jpa jar +springData jar >>SpringData jar包 2.配置applicationCont ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_1 mybatis注解开发的环境搭建
注解开发是省了IUserDao.xml这个映射文件里面的配置 环境搭建 首先是packaging标签.输入jar 需要准备一个实体类.生成getter和setter还有toString方法 创建dao ...
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
1.定义接口 package org.WebService.ws.annotation; import javax.jws.WebService; @WebService public interfa ...
- webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端
1.Define the interface import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebRe ...
- SpringBoot系列教程JPA之基础环境搭建
JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Spring Data JPA是在 Hibernat ...
- JPA Annotation注解
JPA & Hibernate 注解 先说说JPA和Hibernate的关系 JPA(Java Persistence API),是Java EE 5的标准ORM接口,也是ejb3规范的一部分 ...
- SpringBoot24 SpringDataJPA环境搭建、实体类注解、关联查询
1 版本说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 IDEA:旗舰版207.2 MySQL:5.5 2 SpringDataJPA环境搭建(SpringBoot版本) ...
- 【Spring Data 系列学习】Spring Data JPA @Query 注解查询
[Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...
随机推荐
- BZOJ 3694 最短路
233333想简单了.... 题解参见http://hzwer.com/3710.html #include<iostream> #include<cstdio> #inclu ...
- hibernate中使用fetch来决策性能方案
什么时候用子查询,或者连接查询 一般多个数据的对应用子查询,单一行的数据用连接 (若要查询每个学生分别学了什么课程 ,若要fetch=join.fetch=select) 则是这种情况 Hiberna ...
- ORACLE学习笔记 索引和约束
/*** 约束 ***/ * 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约 束,但如果某个约束作用于多个字段, 必须在表级定义约束* 在定义约束时可以通过CONSTRA ...
- ORACLE变量定义及使用(另,T-SQL EXISTS的PLSQL替代写法)
1. 简单变量 declare v_cnt NUMBER(10,0) := 0; BEGIN SELECT COUNT(1) INTO v_cnt FROM concept.Decoction W ...
- AE+C# 图层中增加相应属性标注
原文 AE+C# 图层中增加相应属性标注 ) { IGeoFeatureLayer pGeoFeatureLayer; ILineLabelPosition pLineLabelPosition; I ...
- Effective java笔记4--方法
一.检查参数的有效性 极大多数方法和构造函数都会对于传递给它们的参数值有某些限制. 对于公有的方法,使用Javadoc @throws标签(tag)可以使文档中记录下“一旦针对参数值的限制被违反之后将 ...
- 计算器显示e-005什么意思
计算器显示e-005什么意思 1e-005是科学表达式,即 =1e-5 =0.00001e+005就是乘以10的5次方 就是-1.4989*10^5 这是科学计数法(也叫指数计数法) 这是科学计数 ...
- js String方法集合
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String//返回对应索引的字符 ...
- selenium python (七)层级定位(二次定位)
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,一个页面可能有多个属性基本相同的元素,如果要定位到其 ...
- 我常用的VBS方法(QTP)
这些是4年前在HP用QTP做自动化测试时候总结的一些,现在贴出来,说不准以后会不会用到 当初花了2天时间写的一个自动生成的Excel Report Public Function Report (st ...