---
# System prepended metadata

title: 產學-學習筆記03 小結
tags: [查詢類, MySQL, Hibernate, 學習筆記, Struts2, JAVA]

---

# 產學-學習筆記03 小結
###### tags: `學習筆記` `JAVA` `查詢類` `MySQL` `Hibernate` `Struts2`
---
<!--大標-->
# 總攬
* ### 結合前面所學做一簡易購物車網站 
	
---
# 建立購物⾞網站
### 建立專案與Server(Tomcat)
* #### 1.建立Maven Project專案
![](https://i.imgur.com/TiF54mY.png)
* #### 2.輸入org.apache.maven搜尋找到web-app
![](https://i.imgur.com/glIZlG4.png)
* #### 3.專案出現紅X-插入servlet.jar就好
![](https://i.imgur.com/LyU8zUL.png)
![](https://i.imgur.com/fksl6GO.png)
![](https://i.imgur.com/tQZYDZD.png)
* #### 4.專案new⼀個server
![](https://i.imgur.com/2UzKe7I.png)
* #### 5.選apache8.5
![](https://i.imgur.com/WfX4Yl1.png)
* #### 6.找到apache的⽬錄位址
![](https://i.imgur.com/AP4wsEa.png)
* #### 7.下⽅出現server
![](https://i.imgur.com/2N82bOR.png)

### 掛入Maven
* #### 1.搜尋maven repository
https://mvnrepository.com/
* #### 2.找MySQL Connectory/J 5.1.48
![](https://i.imgur.com/qtp4FLS.png)
![](https://i.imgur.com/WGbdg4i.png)
* #### 3.複製到pom.xml-貼在dependency裡
![](https://i.imgur.com/gJetdhS.png)
* #### 4.搜尋struts2 Core 2.5.22
![](https://i.imgur.com/xa6f9z5.png)
![](https://i.imgur.com/0Ro9U6c.png)
* #### 5.複製到pom.xml
* #### 6.hibernate - 5.4.13
![](https://i.imgur.com/EoFNEKm.png)
![](https://i.imgur.com/6uMWgAp.png)
* #### 7.複製到pom.xml
* #### 8.完成MySQL, Struts2, Hibernate加掛⾄pom.xml
![](https://i.imgur.com/BoEjP3S.png)

### 建立controller, dao, model
* #### 1.新建3個package
![](https://i.imgur.com/iDs85Z9.png)
* #### 2.開啟mySQL-新增schema(表單)-crate Table
![](https://i.imgur.com/iQx5i6x.png)
* #### 3.member table
![](https://i.imgur.com/HpZjuam.png)
* #### 4.porder table
![](https://i.imgur.com/Flja5yc.png)
* #### 5.(package)model>>(class)member>>建立資料庫所擁有的資訊-getter&setter
![](https://i.imgur.com/Qzn4Juz.png)
* #### 6.(package)model>>(class)porder>>建立資料庫所擁有的資訊-getter&setter
![](https://i.imgur.com/laKkTHN.png)
* #### 7.member和porder加掛hibernate
![](https://i.imgur.com/NIA4FIi.png)
![](https://i.imgur.com/xW42PlZ.png)
* #### 8.於src加掛hibernate
![](https://i.imgur.com/KxydZE1.png)
![](https://i.imgur.com/ChHGrzA.png)
相關資料自行輸入
![](https://i.imgur.com/AQEv08Z.png)
![](https://i.imgur.com/dPKLPS7.png)
* #### 9.Hibernate.cfg.xml-Session Factory>>建立Mappings>>member, porder(2個都要掛入)
![](https://i.imgur.com/uFj6JZB.png)
* #### 10.dao建立interface
![](https://i.imgur.com/uMGFPDE.png)
* #### 11.撰寫-出現紅線-Static-專案修改版本為1.8
![](https://i.imgur.com/EZnSkj2.png)
![](https://i.imgur.com/mg6gnfv.png)
![](https://i.imgur.com/HgSHgTa.png)
![](https://i.imgur.com/ulkaz5l.png)
* #### 12.程式碼
implDao
```java=
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public interface implDao {
//連線方法
static Session getSe()
{
Configuration conn=new Configuration().configure();
SessionFactory sf=conn.buildSessionFactory();
Session se=sf.openSession();
return se;
}
//新增
void add(Object o);
//查詢
Object get(int id);
//更新
void update(Object o);
//刪除
void delete(Object o);
}
```
新增memberDao
```java=
package dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import model.member;

public class memberDao implements implDao{

public static void main(String[] args) {

Session se=implDao.getSe();

//Query q=se.createQuery("From member where(id between 2 and 5) and name in('john','allen')");
//Query q=se.createQuery("From member as m where m.id>2 and m.id<5 ");
//SQLQuery q=se.createSQLQuery("select * from member");
//q.addEntity(member.class);
//Query q=se.createQuery("from member where user='aaf' and password='1234'");
//List<member> l=q.list();

System.out.println(checkUserDu("abc2").size());

/*for(member m:l)
{
System.out.println(m.getId()+"\t"+m.getName()+"\t"+m.getUser());
}*/

/*member m=new member();
m.setName("yyy");
m.setUser("pru");
m.setPassword("1234");
m.setPhone("00000");
m.setAddress("tai");
new memberDao().add(m);*/

}
//帳號密碼檢查
public static List<member> checkUser(String user,String password)
{
Session se=implDao.getSe();
Query q=se.createQuery("from member where user='"+user+"' and
password='"+password+"'");
List<member> l=q.list();
return l;
}
//帳號重複檢查
public static List<member> checkUserDu(String user)
{
Session se=implDao.getSe();
Query q=se.createQuery("from member where user='"+user+"'");
List<member> l=q.list();
return l;
}
@Override
public void add(Object o) {
Session se=implDao.getSe();
Transaction tx=se.beginTransaction();
se.save(o);
tx.commit();
se.close();
}
@Override
public Object get(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void update(Object o) {
// TODO Auto-generated method stub
}
@Override
public void delete(Object o) {
// TODO Auto-generated method stub
}
}
```
* #### 13.memberDao可以測試是否有連接資料庫(新增)
⼿動新增資料-測試
```java=
member m=new member();
m.setName("yyy");
m.setUser("pru");
m.setPassword("1234");
m.setPhone("00000");
m.setAddress("tai");
new memberDao().add(m);
```
![](https://i.imgur.com/eVZp74F.png)

* #### 14.memberDao可以測試是否有連接資料庫(查詢)
![](https://i.imgur.com/ShiyfTh.png)
使用HQL語法語SQL語法去查詢資料庫資料
![](https://i.imgur.com/uhjLGob.png)



### 使用Struts
* #### 1.搜尋struts webxml sample
https://struts.apache.org/core-developers/web-xml.html
![](https://i.imgur.com/3SkX7eB.png)
```
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
```
* #### 2.加掛在web.xml
![](https://i.imgur.com/QnXygMB.png)
* #### 3.新增struts.xml(需在src/main/java下)-修改(需對應前端-等等做)
```java=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" class="controller.loginAction">
<result name="loginok" type="redirect">loginok.jsp</result>
<result name="addmember" type="redirect">addmember.jsp</result>
</action>
<action name="checkUser" class="controller.checkUserAction">
<result name="userduplicate" type="redirect">userduplicate.jsp</result>
<result name="addmemberok" type="redirect">addmemberok.jsp</result>
</action>
</package>
</struts>
package controller;
```
### 程式碼區塊
* #### 1.後端-在controller包裡建立checkUserAction.java
```java=
package controller;
import Dao.memberDao;
import model.member;

public class checkUserAction {
private String user;
private String password;

public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//
public String execute() throws Exception
{
int x=memberDao.checkUserDu(getUser()).size();
if(x!=0)
{
return "userduplicate";//重複
}
else
{
member m=new member();
m.setUser(getUser());
m.setPassword(getPassword());
new memberDao().add(m);
return "addmemberok";//建立成功
}
}
}
```
* #### 2.後端-在controller包裡建立loginAction.java
```java=
package controller;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import Dao.memberDao;
import model.member;

public class loginAction extends ActionSupport{
private String user;
private String password;

public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute() throws Exception
{
//if(getUser().equals("abc") && getPassword().equals("1234"))
//int x=memberDao.checkUser(getUser(), getPassword()).size();
List l=memberDao.checkUser(getUser(), getPassword());
if(l.size()!=0)
{
Object[] o=l.toArray();
member m=(member)o[0];
System.out.println(m);
Map session = ActionContext.getContext().getSession();
session.put("M",m);
return "loginok";
}
else
{
return "addmember";
}
}
}
```
* #### 3.前端-index.jsp
```javascript=
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
</head>
<body>
<a href=''>1>購物</a><p>
<a href='login.jsp'>2>登入</a><p>
<a href='addmember.jsp'>3>註冊</a><p>
</body>
</html>
```
* #### 4.前端-login.jsp
```javascript=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
帳號:<input type="text" name="user"><br>
密碼:<input type="text" name="password"><br>
<input type="submit" value="ok">
</form>
</body>
</html>
```
* #### 5.前端-loginok.jsp
```javascript=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登入成功</h1>
<a href="">1)會員管理</a><br>
<a href="">2)購物</a><br>
</body>
</html>
```
* #### 6.前端-userduplicate.jsp
```javascript=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>帳號重複</h1>
<a href="addmember.jsp">重新註冊</a>
</body>
</html>
```
* #### 7.前端-addmamer.jsp
```javascript=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="checkUser" method="post">
帳號:<input type="text" name="user"><br>
密碼:<input type="text" name="password"><br>
<input type="submit" value="ok">
</form>
</body>
</html>
```
* #### 8.前端-addmemberok.jsp
```javascript=
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>帳號建立成功</h1>
<a href="login.jsp">回登入頁</a>
</body>
</html>
```
* #### 9.run-點選"登入"
![](https://i.imgur.com/8zTITW3.png)

* #### 10.輸入資料庫沒有的資料
![](https://i.imgur.com/n0DxXDo.png)

* #### 11.導入新建帳號密碼
![](https://i.imgur.com/n1DkP7U.png)

* #### 12.建立成功
![](https://i.imgur.com/zNIOLmd.png)

* #### 13.資料建置成功
![](https://i.imgur.com/P0wOwm8.png)

* #### 14.新增帳號密碼時重複畫⾯
![](https://i.imgur.com/vGtlB3d.png)

