跟上一篇差很少,一些基本的東西。java
此次是JPA + Spring MVC 3.0mysql
1.創建Projectweb
2.Add JPA Supportspring
3.咱們以Hibernate爲例,設置JPA的Provider爲Hibernatesql
4.添加persistence.xml,這裏標準的位置應該是src/main/resources/META-INF,因此咱們要創建resource和META-INF的文件夾windows
5.回到project structure->modules->JPA,給JPA添加咱們剛剛創建的persistence.xml文件mvc
6.修改POM.XML添加兩個jar,一個是hibernate-entitymanager,一個是mysql connectorapp
7.修改一下咱們的persistence.xml,(這裏你也能夠先不添加persistence.xml,只是把META-INF建好,而後從第5步那生成也能夠)jsp
<?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創建一個DataSourceide
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下運行正常。