跟上一篇差很少,一些基本的东西。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下运行正常。