Update.class,用于建立資料庫表
import org.springframework.data.jpa.repository.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.jms.Session;
import javax.persistence.*;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ly on 2016/9/23.
*/
public class Update
private EntityManagerFactory emf;
@PersistenceUnit//使用這個标記來注入EntityManagerFactory
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
public Update() {
// entityManagerFactory = Persistence.createEntityManagerFactory("entityManagerFactory");
}
public void begin() {
createTable();
}
private void createTable() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); //這個不加會報錯
Query query = null;
for (int i = 0; i < sqlList.size(); i++) {
query = em.createNativeQuery(sqlList.get(i));
query.executeUpdate();
}
em.getTransaction().commit();
em.close();
}
private List<String> sqlList = new ArrayList<String>();
{
sqlList.add("CREATE TABLE if not exists t_xfw_rights (\n" +
" id INT not null AUTO_INCREMENT,\n" +
" rightName varchar(45) DEFAULT NULL,\n" +
" iconCls varchar(45) DEFAULT NULL,\n" +
" orderIndex INT DEFAULT NULL,\n" +
" fartherId INT DEFAULT 0,\n" +
" urlPath varchar(45) DEFAULT NULL,\n" +
" rightType INT DEFAULT NULL,\n" +
" PRIMARY KEY (id) \n"+
")\n");
sqlList.add("create table if not exists t_xfw_operator(\n"+
" id INT not null AUTO_INCREMENT,\n" +
" userName varchar(45) default null,\n"+
" passWord varchar(45) default null,\n"+
" userType int default null,\n"+
" PRIMARY KEY (id),\n" +
" UNIQUE KEY UK_username (userName)\n"+
")\n");
sqlList.add("Create table if not exists t_xfw_operator_rights(\n" +
" user_id INT(20),\n " +
" right_id INT(20),\n " +
" PRIMARY KEY (user_id,right_id),\n"+
" KEY FK_right_id (`right_id`),\n"+
" CONSTRAINT FK_user_id foreign key(user_id) references t_xfw_operator(id),\n"+
" CONSTRAINT FK_right_id foreign key(right_id) references t_xfw_rights(id)\n"+
")\n");
}
}
@Controller
@RequestMapping(value = "/")
public class ServerController
Logger logger=Logger.getLogger(ServerController.class);
@Autowired//自動注入
Update update;
@RequestMapping(value = "/update")
@ResponseBody
public JsonResultObject update(){
try {
update.begin();
return new JsonResultObject(EnumState.Ok,"ok");
}
catch (Exception e){
return new
<!-- JPA實體管理工廠的配置 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="packagesToScan" value="com.jiapeng.xfw.server"/><!--待掃描的實體類包,不再需要persistence.xml了 -->
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.formate_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.DefaultNamingStrategy</prop>//這個設定是讓列名采用自定義的名字,免得在使用jpa進行資料庫操作時生成的sql的字段名都是像 user_id 這樣加下劃線的名字
</props>
</property>
</bean>
<beans>
<bean class="com.jiapeng.xfw.server.normalClass.Update">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
</beans>