IntellJ 13.x JPA Persistence Sample

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

这次是JPA + Spring MVC 3.0

1.建立Project

SNAGHTML5f1c62a

2.Add JPA Support

SNAGHTML5f51218

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

SNAGHTML5f65ef0

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

image

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

image

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

image

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

image

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

SNAGHTML604daa4

选定位置

SNAGHTML60548a2

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

image

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

image

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的话,就没有问题了

image

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

原文地址:https://www.cnblogs.com/ruijiang21/p/3543652.html