跟上一篇差不多,一些基本的东西。

这次是JPA + Spring MVC 3.0

1.建立Project

2.Add JPA Support

3.我们以Hibernate为例,设置JPA的Provider为Hibernate

4.添加persistence.xml,这里标准的位置应该是src/main/resources/META-INF,所以我们要建立resource和META-INF的文件夹

5.回到project structure->modules->JPA,给JPA添加我们刚刚建立的persistence.xml文件

6.修改POM.XML添加两个jar,一个是hibernate-entitymanager,一个是mysql connector

7.修改一下我们的persistence.xml,(这里你也可以先不添加persistence.xml,只是把META-INF建好,然后从第5步那生成也可以)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="personDB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="9ijn)OKM"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>

7.View->Tools windows->DataBase建立一个DataSource

8.View->Tools Windows->Persistence->Generate Persistence Mapping->By Database Schema

选定位置

9.这时候我们已经有了生成出来的Entity

10. 注意这个时候你如果Console->来做hql查询的时候,他会显示错误,这个现在你可以不用理会,等build之后自己就好了

11.这个时候检查一下persistence.xml文件,主要看一下,class是不是已经加到xml里面,完整的persistence.xml应该是这样

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="personDB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.springapp.modlels.OfficeEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="9ijn)OKM"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>

12.加一个add.jsp,在修改一下controller

package com.springapp.mvc;

import com.springapp.modlels.OfficeEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; @Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
} @RequestMapping(method = RequestMethod.GET, value="add")
public String add(ModelMap model) {
model.addAttribute("message", "Hello world!");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("personDB");
EntityManager mgr = emf.createEntityManager();
mgr.getTransaction().begin();
OfficeEntity officeEntity = new OfficeEntity();
officeEntity.setOfficeName("test");
mgr.persist(officeEntity);
mgr.getTransaction().commit();
return "add";
} }

运行就可以了。

这时候如果我们返回persistence,console hql的话,就没有问题了

注意,在JBoss下会出现No suitable Driver 的问题,是从getTransaction()开始,不知道为什么会出现这种情况,在Tomcat下运行正常。

IntellJ 13.x JPA Persistence Sample的更多相关文章

  1. JPA persistence

    Play provides a set of very useful helpers to simplify the management of your JPA entities. Note tha ...

  2. Spring data jpa persistence .xml 配置文件

    <?xml version="1.0" encoding="UTF-8"?><persistence xmlns="http://j ...

  3. 摘抄JPA官方文档原文 防呆

    Spring Data JPA - Reference Documentation Oliver GierkeThomas DarimontChristoph StroblMark PaluchVer ...

  4. jpa注解

    http://www.oracle.com/technetwork/cn/middleware/ias/toplink-jpa-annotations-100895-zhs.html#ManyToOn ...

  5. JPA 批注参考

    body, p, th, td, li, ul, ol, h1, h2, h3, h4, h5, h6, pre { font-family: simsun; line-height: 1.4; } ...

  6. jpa table主键生成策略

    用 table 来生成主键详解 它是在不影响性能情况下,通用性最强的 JPA 主键生成器.这种方法生成主键的策略可以适用于任何数据库,不必担心不同数据库不兼容造成的问题. initialValue不起 ...

  7. JPA 相关API (一)

    [Query 接口下的常用API] [API 测试类:Test_QueryAPI.java] 1 package org.zgf.jpa.entity; 2 3 import java.math.Bi ...

  8. SpringData JPA详解

    Spring Data JPA 1.    概述 Spring JPA通过为用户统一创建和销毁EntityManager,进行事务管理,简化JPA的配置等使用户的开发更加简便. Spring Data ...

  9. hdu3652(含有13且能被13整除的数)数位DP基础

    B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. cloudstack4.2+xenserver6.0.2 详细配置攻略

    搭建一台安装了XenServer的服务器 搭建一台安装了CloudStack的服务器用以管理云平台 可以使用CloudStack云平台进行虚拟机管理 使用远程桌面访问windows虚拟机 由于最近实验 ...

  2. Cloud Computing Deployment Models

    Cloud computing can broadly be broken down into three main categories based on the deployment model. ...

  3. mysql 内外链接

    外联结:分为外左联结和外右联结. 左联结A.B表的意思就是将表A中的全部记录和表B中联结的字段与表A的联结字段符合联结条件的那些记录形成的记录集的联结,这里注意的是最后出来的记录集会包括表A的全部记录 ...

  4. Java:浅谈InputStream的close方法

    原则:最好在任何时候使用InputStream或者OutputStream的时候,在finally中调用close()方法,显式关闭. 一个典型的示例 InputStream in = null; t ...

  5. 解决Linux下sqlplus中文乱码问题

    错误现象:在windows下用其他工具访问oracle,确认中文正常显示.在Linux下使用sqlplus查询数据表中文内容出现乱码. 分析及解决:因为windows下正常,所以问题应出现在Linux ...

  6. cocos2d 制作动态光晕效果基础 —— blendFunc

    转自:http://blog.csdn.net/yang3wei/article/details/7795764 最近的项目要求动态光晕的效果. 何谓动态光晕?之前不知道别人怎么称呼这个效果, 不过在 ...

  7. stl lower_bound upper_bound binary_search equal_range

    自己按照stl实现了一个:   http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同: Two e ...

  8. 【转】一个安全测试的CheckList

    转自:http://hi.baidu.com/dontcry/item/90c2bc466558c217886d1075 不登录系统,直接输入登录后的页面的URL是否可以访问: 不登录系统,直接输入下 ...

  9. java 搭建webservice服务+testclient測试

    整理别人的日志: 一.什么是webservice 一种构建应用程序的普遍模型,能够在不论什么支持网络通信的操作系统中执行.一种新的web应用程序分支,能够公布.定位通过web调用.它是一个应用组件,为 ...

  10. 【WinForm】C# 采用POST登录京东

    C# POST 传值登录 京东 想做一个DEMO 练练html的传值和接收,就用Winform 做了一个登录京东的程序. 首先参考的网址是: 艹蛋的青春じ 让我蛋疼ミ:http://www.cnblo ...