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

这次是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. MFC中常用的内容

    在程序中更改静态文本内容. GetDlgItem(IDC_STATIC)->SetWindowText("欢迎"); 不用UpdateData(false); 如果提示con ...

  2. cloudstack 修改显示名称

    http://192.168.153.245:8900/client/api?command=updateVirtualMachineid=922d15e1-9be0-44ac-9494-ce5afc ...

  3. ios8,xcode6 周边

    NSBundle.mainBundle().infoDictionary iOS 8中带按钮的推送代码 ") ){ application.registerForRemoteNotifica ...

  4. linux 查看当前路径命令:pwd

    查看当前路径命令:pwd pwd命令能够显示当前所处的路径. 这个命令比较简单,如果有时在操作过程中忘记了当前的路径,则可以通过此命令来查看路径,其执行方式为: # pwd /home/samlee ...

  5. cocos2d-x CCListView

    转自:http://blog.csdn.net/onerain88/article/details/7641126 cocos2d-x 2.0 版更新了,把opengl 1.1 替换为opengl 2 ...

  6. zookeeper使用和原理探究(一)

      zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<The Chubby lock serv ...

  7. [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad

    We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...

  8. iOS开发——语法篇OC篇&高级语法精讲二

    Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和 ...

  9. Android Dalvik虚拟机初识(转)

    原文地址:http://blog.csdn.net/andyxm/article/details/6126907 android虚拟机jvmjava优化linux内核 首先,让我们来思考下面几个问题: ...

  10. Jquery_异步上传文件多种方式归纳

    1.不用任何插件,利用iframe,将form的taget设为iframe的name,注意设为iframe的id是没用的,跟网上很多说的不太一致 iframe_upload.htm <!DOCT ...