Skip to content

Commit

Permalink
Recipe設定檔
Browse files Browse the repository at this point in the history
  • Loading branch information
hevinhsu committed Nov 12, 2019
1 parent 1bd99f5 commit faecf4c
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 13 deletions.
174 changes: 174 additions & 0 deletions src/main/java/Recipe/createRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package Recipe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Recipe.model.MateralBean;
import Recipe.model.MethodBean;
import Recipe.model.RecipeBean;
import Recipe.service.MateralService;
import Recipe.service.MethodService;
import Recipe.service.RecipeService;
import createAccount.model.MemberBean;

public class createRecipe {
public static final String UTF8_BOM = "\uFEFF"; // 定義 UTF-8的BOM字元
private static ApplicationContext ctx;

public static void main(String[] args) throws IOException, SQLException, ParseException {

ctx = new ClassPathXmlApplicationContext("beans.xml");
SessionFactory factory = ctx.getBean(SessionFactory.class);
Session session = factory.getCurrentSession();

RecipeService recipeService = ctx.getBean(RecipeService.class);
MateralService materalService = ctx.getBean(MateralService.class);
// MethodService methodService = ctx.getBean(MethodService.class);

File file = new File("C:/ezfitData/createTable/recipe.data");
File file2 = new File("C:/ezfitData/createTable/method.data");
File file3 = new File("C:/ezfitData/createTable/materal.data");
// File file = new File("C:/Users/tmps8/Desktop/ezfitData/createTable/recipe.data");
// File file2 = new File("C:/Users/tmps8/Desktop/ezfitData/createTable/method.data");
// File file3 = new File("C:/Users/tmps8/Desktop/ezfitData/createTable/materal.data");

int n = 0;
String line = "";

Transaction tx = session.beginTransaction();
// recipe
try (FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);) {

MemberBean mb = new MemberBean();
session.save(mb);

while ((line = br.readLine()) != null) {
System.out.println("line=" + line);
// 去除 UTF8_BOM: \uFEFF
// if (line.startsWith(UTF8_BOM)) {
// line = line.substring(1);
try {

String[] token = line.split("\\|");
RecipeBean recipe = new RecipeBean();
recipe.setChat(Integer.parseInt(token[0]));
recipe.setFileName(token[1]);
recipe.setGood(Integer.parseInt(token[2]));
recipe.setIntroduction(token[3]);
recipe.setPublished(Boolean.valueOf(token[4]));
recipe.setRecipeName(token[5]);
recipe.setSave(Integer.parseInt(token[6]));
recipe.setServings(Integer.parseInt(token[7]));
recipe.setSpendTime((token[8]));
// recipe.setMember(recipeDao.getMemberBeanByMemberId(token[9]));
recipe.setMember(recipeService.getMemberByMemberId(token[9]));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2019-11-11 10:54:17.943000");
recipe.setRecipeCreateTime(date);
// session.save(recipe);
recipeService.insertRecipe(recipe);
} catch (Exception e) {
e.printStackTrace();
}
++n;
// }
}
tx.commit();
System.out.println("新增Recipe紀錄,共成功=" + n);
}

// method
List<MethodBean> list = new ArrayList<>();
line = "";
factory = ctx.getBean(SessionFactory.class);
session = factory.getCurrentSession();
tx = session.beginTransaction();
try (FileInputStream fis = new FileInputStream(file2);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);) {

while ((line = br.readLine()) != null) {
System.out.println("line=" + line);
// 去除 UTF8_BOM: \uFEFF
// if (line.startsWith(UTF8_BOM)) {
// line = line.substring(1);

String[] token = line.split("\\|");
MethodBean method = new MethodBean();
method.setDetail(token[0]);
method.setFileName(token[1]);
RecipeBean recipe = new RecipeBean();
recipe.setRecipeId(Integer.parseInt(token[2]));
method.setRecipe(recipe);

// session.save(method);
// list.add(method);
++n;
session.save(method);
// }
// methodService.insertMethod(list);
}
} catch (Exception e) {
e.printStackTrace();
}
// tx.commit();
System.out.println("新增Method紀錄,共成功=" + n);

// materal
// factory = ctx.getBean(SessionFactory.class);
// session = factory.getCurrentSession();
// tx = session.beginTransaction();
line = "";
// tx = session.beginTransaction();
try (FileInputStream fis = new FileInputStream(file3);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);) {

while ((line = br.readLine()) != null) {
System.out.println("line=" + line);
// 去除 UTF8_BOM: \uFEFF
// if (line.startsWith(UTF8_BOM)) {
// line = line.substring(1);

String[] token = line.split("\\|");
MateralBean materal = new MateralBean();
materal.setMateralName(token[0]);
materal.setUnit(token[1]);

RecipeBean recipe = new RecipeBean();
recipe.setRecipeId(Integer.valueOf(token[2]));
materal.setRecipe(recipe);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2019-11-11 10:54:17.943000");
recipe.setRecipeCreateTime(date);

materalService.insertMateral(materal);
++n;

// }
}
System.out.println("新增Materal紀錄,共成功=" + n);
}
tx.commit();
}

}
2 changes: 1 addition & 1 deletion src/main/java/config/RootAppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RootAppConfig {
public DataSource dataSource() {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setUser("root");
ds.setPassword("1qaz@WSX");
ds.setPassword("tmps880445");
try {
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
} catch (PropertyVetoException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="shopping,createAccount,login"></context:component-scan>
<context:component-scan base-package="shopping,createAccount,login,Recipe"></context:component-scan>


<bean id="dataSourceMySQL"
Expand All @@ -15,7 +15,7 @@
<property name="url"
value="jdbc:mysql://localhost:3306/ezfit?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=Asia/Taipei&amp;useSSL=false" />
<property name="username" value="root" />
<property name="password" value="1qaz@WSX" />
<property name="password" value="tmps880445" />
</bean>


Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1qaz@WSX</property>
<property name="hibernate.connection.password">tmps880445</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ezfit?useUnicode=yes&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=Asia/Taipei</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 2. 其他與資料存取有關的設定 -->
<!-- 自動產生資料庫的Schema -->
<property name="hbm2ddl.import_files">update.sql</property>
<property name="hbm2ddl.auto">update</property>
<property name="hbm2ddl.auto">create</property>
<!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">false</property>
<!-- 將顯示的SQL排版,方便觀看 -->
Expand Down
Binary file added target/classes/Recipe/createRecipe.class
Binary file not shown.
4 changes: 2 additions & 2 deletions target/classes/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="shopping,createAccount,login"></context:component-scan>
<context:component-scan base-package="shopping,createAccount,login,Recipe"></context:component-scan>


<bean id="dataSourceMySQL"
Expand All @@ -15,7 +15,7 @@
<property name="url"
value="jdbc:mysql://localhost:3306/ezfit?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=Asia/Taipei&amp;useSSL=false" />
<property name="username" value="root" />
<property name="password" value="1qaz@WSX" />
<property name="password" value="tmps880445" />
</bean>


Expand Down
Binary file modified target/classes/config/RootAppConfig.class
Binary file not shown.
4 changes: 2 additions & 2 deletions target/classes/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1qaz@WSX</property>
<property name="hibernate.connection.password">tmps880445</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ezfit?useUnicode=yes&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=Asia/Taipei</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 2. 其他與資料存取有關的設定 -->
<!-- 自動產生資料庫的Schema -->
<property name="hbm2ddl.import_files">update.sql</property>
<property name="hbm2ddl.auto">update</property>
<property name="hbm2ddl.auto">create</property>
<!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">false</property>
<!-- 將顯示的SQL排版,方便觀看 -->
Expand Down
4 changes: 2 additions & 2 deletions target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
Built-By: ben
Build-Jdk: 11.0.2
Built-By: HevinHsu
Build-Jdk: 11.0.3-ojdkbuild
Created-By: Maven Integration for Eclipse

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Generated by Maven Integration for Eclipse
#Tue Nov 12 10:20:00 CST 2019
m2e.projectLocation=/Users/ben/eclipse-workspace/ezfit
#Tue Nov 12 15:01:12 CST 2019
m2e.projectLocation=C\:\\_JSP\\workspaceJDBC\\ezfit
m2e.projectName=ezfit
groupId=ntut.edu.com.java012
artifactId=ezfit
Expand Down

0 comments on commit faecf4c

Please sign in to comment.