Technical note playframework_documentation_working with play - java_vnAsahina Infotech
Technical note , Playframework Documentation
"Working with play - java" Translation
Tài liệu ghi chép kĩ thuật về Playframework. Do không có tài liệu về playframwork bằng tiếng Việt, nên chúng tôi đã tạo tài liệu này với mục đích để học tập.
Về nội dung nếu có gì sai sót xin hãy chỉ ra cho chúng tôi. Nếu bản dịch này có giá trị, chúng tôi sẽ đăng tải nó lên cộng đồng.
Technical note playframework_documentation_working with play - java_vnAsahina Infotech
Technical note , Playframework Documentation
"Working with play - java" Translation
Tài liệu ghi chép kĩ thuật về Playframework. Do không có tài liệu về playframwork bằng tiếng Việt, nên chúng tôi đã tạo tài liệu này với mục đích để học tập.
Về nội dung nếu có gì sai sót xin hãy chỉ ra cho chúng tôi. Nếu bản dịch này có giá trị, chúng tôi sẽ đăng tải nó lên cộng đồng.
báo cáo chương lý thuyết trong powerbi.docxteddy04092006
CHƯƠNG 1: TỔNG QUAN
1.1. Lý do chọn đề tài
Trong thời đại công nghệ số, dữ liệu được coi là tài sản quý giá của mỗi tổ chức, doanh nghiệp. Khối lượng dữ liệu thu thập được ngày càng lớn với tốc độ tăng trưởng mạnh mẽ, kéo theo nhu cầu xử lý, phân tích và khai thác dữ liệu trở thành một yếu tố then chốt trong việc nâng cao hiệu quả kinh doanh và cạnh tranh trên thị trường. Các công cụ truyền thống như Excel, SQL đang dần bộc lộ những hạn chế khi xử lý dữ liệu lớn và phức tạp.
2. Database
● Go doesn't provide any database drivers, but it does have a driver interface defined in the
database/sql package
– The advantage is that if your code is developed according to these interface standards, you will not
need to change any code if your database changes.
● We always see the following code when we use third-party drivers:
– Import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
– Here the underscore (also known as a 'blank') “_”:
● “_” được dùng trong các hàm (func) lúc trả về các giá trị mà chúng ta không cần thì chúng ta sẽ loại bỏ nó (discarding)
Khi bạn import một thư viện thì bạn sẽ phải dùng tất cả các package đó (nếu không dùng package đó trong đoạn code nào đó thì Go
sẽ biên dịch và cảnh báo lại).
→ khi sử dụng underscore “_” khi import thư viện thì có nghĩa là bạn chỉ cần dùng function init() mà không cần sử dụng trực tiếp
→ tính năng này cần thiết cho registering database drivers vì nó sẽ tự động đăng ký database drivers mà không cần phải gọi nó
3. Database
● Function:
– sql.Register(): registering database drivers when you use
third-party database drivers.
● Gọi hàm này trong hàm init()
– driver.Driver: is an interface containing an Open(name
string) method that returns a Conn interface.
– driver.Conn: This is a database connection interface with
some methods the same Conn can only be used in one goroutine.
– driver.Stmt
– driver.Tx
4. Database
● Quy trình:
– Step 0: Importing a Database Driver
● L y package:ấ
– go get github.com/go-sql-driver/mysql
● Import trong mã ngu nồ
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
5. Database
● Quy trình:
– Step 1: s d ngử ụ sql.Open đ chu n b k t n iể ẩ ị ế ố
● db, err := sql.Open(<driver name>, <command_to_connect>)
– <command_to_connect>: driver-specific syntax that tells the driver how to
access the underlying datastore
– Giá tr tr v : c n ki m traị ả ề ầ ể handle errors
● Ví d :ụ
– db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/hello")
– db, err := sql.Open("mysql", "<user>:<password>@/test?charset=utf8")
● Sql.Open ch a t o k t n i ngayư ạ ế ố
– Nó t o k t n i ch khi nào có yêu c u (query) đ u tiên.ạ ế ố ỉ ầ ầ
– db.Ping() dùng đ ki m tra database is available and accessible (forể ể
example, check that you can establish a network connection and log in)
6. Database
● Quy trình:
– Step 2: T o m i d li u, g m 3 b c:ạ ớ ữ ệ ồ ướ
● stmt, err := db.Prepare(<command>): chu n b đ nh d ng d li u đ chèn vào DBẩ ị ị ạ ữ ệ ể
– returns a SQL operation that is going to be executed. It also returns the execution status after executing SQL.
– db là giá tr tr v t hàm slq.Open()ị ả ề ừ
– Ví d : db.Prepare("INSERT userinfo SETụ username=?,departname=?,created=?")
● res, err := stmt.Exec(<data>): th c hi n l nhự ệ ệ
– Stmt là giá tr tr v t hàm db.Prepare()ị ả ề ừ
– <data>: là m u chu n theo th t đã xác đ nh trên b c chu n b trênẫ ẩ ứ ự ị ướ ẩ ị
– Ví d : stmt.Exec("astaxie", "a72", "2016-12-09") → tuân theo chu n <username,departname,created> trênụ ẩ ở
● res.<Action>: đ c giá tr tr v sau khi th c hi n l nhọ ị ả ề ự ệ ệ
– res là giá tr tr v t hàmị ả ề ừ stmt.Exec()
– Có các Action khác nhau:
● id, err := res.LastInsertId()
● affect, err := res.RowsAffected()
7. Database
● Quy trình:
– Step 3: Truy v n tìm ki m d li uấ ế ữ ệ
● L nh truy v n:ệ ấ db.Query(<command_to_query>)
– rows, err := db.Query("SELECT * FROM userinfo")
● Duy t danh sách tr v :ệ ả ề
– rows.Next():
– rows.Scan(): read the columns in each row into variables
● defer rows.Close(): đóng rows
for rows.Next() { → l y l n l t các row tr vấ ầ ượ ả ề
err = rows.Scan(&uid, &username, &department, &created)
}
8. Database
● Quy trình:
– Step 4: Xóa d li u (t ng t nh Step2)ữ ệ ươ ự ư
● stmt, err = db.Prepare(<command_to_del>)
– <command_to_del>: là l nh mysql đ xóa d li uệ ể ữ ệ
– stmt, err = db.Prepare("delete from userinfo where uid=?")
● res, err = stmt.Exec(id)
– Stmt thi u giá tr d li uế ị ữ ệ id nên l nh này đ a id vào và th c thiệ ư ự
l nh xóaệ
● affect, err = res.RowsAffected()
Note that we use the format =? to pass arguments. This
is necessary for preventing SQL injection attacks.
9. Database
●
Ví d đ c DB Mysql:ụ ọ
– K t n i:ế ố
●
db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
● db.Close()
– Ghi d li u:ữ ệ
● stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
● res, err := stmt.Exec("jace", "jace in corp", "2015-12-09")
● id, err := res.LastInsertId() → ki m ch ng l n ghi d li u v a r iể ứ ầ ữ ệ ừ ồ
– C p nh p/S a d li uậ ậ ử ữ ệ
●
stmt, err = db.Prepare("update userinfo set username=? where uid=?")
●
res, err = stmt.Exec("astaxieupdate", id)
● affect, err := res.RowsAffected() → ki m ch ng l nh update có đúng đ i t ng (rows)ể ứ ệ ố ượ
– Tìm ki m d li u:ế ữ ệ
● rows, err := db.Query("SELECT * FROM userinfo")
● for rows.Next() {err = rows.Scan(&uid, &username, &department, &created)}
– Xóa d li u:ữ ệ
● stmt, err = db.Prepare("delete from userinfo where uid=?")
●
res, err = stmt.Exec(id) → th c thi l nh xóaự ệ
● affect, err = res.RowsAffected() → ki m ch ng l nh xóa có đúng đ i t ng khôngể ứ ệ ố ượ
10. Database
● Làm vi c v i Redisệ ớ
– redis is a key-value storage system like Memcached, that
supports the string, list, set and zset(ordered set) value types.
– Download: http://redis.io/download
– Cài đ t và th nghi m:ặ ử ệ
● Ch y server: src/redis-serverạ
● T ng tác, thao tác d li u v i server: src/redis-cliươ ữ ệ ớ
– set foo bar
– get foo
– L y th vi n database driver:ấ ư ệ
● go get github.com/astaxie/goredis
11. Database
● Làm vi c v i Redisệ ớ
– Các th vi n giao ti p v i redis:ư ệ ế ớ
● https://github.com/fzzy/radix
● https://github.com/alphazero/Go-Redis
– Current status is compatible with Redis 2.4.n (2.4.9 tested) and Go 1
– đã lâu không có b n c p nh tả ậ ậ
● https://github.com/simonz05/godis → b n c p nh t đã khá cũ (2012)ả ậ ậ
● https://github.com/hoisie/redis → Designed for Redis 2.6.x.
● https://github.com/garyburd/redigo
– B n m i nh t vào Tháng 5/2016ả ớ ấ
–
12. Database
● Làm vi c v i Redis:ệ ớ
– M t s ví d t ng tác v i Redis:ộ ố ụ ươ ớ
● https://github.com/ovr/go-redis
● https://github.com/wen866595/gredis
● https://github.com/hongruiqi/redis.go
●
13. cookies
●
Cookies g m các tr ng:ồ ườ
– Name: string
– Value: string
– Path: string
– Domain: string
– Expires: time.Time
– RawExpires: string
– MaxAge: int
– Secure: bool
– HttpOnly: bool
– Raw: string
– Unparsed: []string
●
Go uses the SetCookie function in the net/http package to set cookies:
– http.SetCookie(w ResponseWriter, cookie *Cookie)
– Hàm này có th dùng cho c 2 tr ng h p t o cookies và xóa cookies.ể ả ườ ợ ạ
14. cookies
func login(w http.ResponseWriter, r *http.Request) {
SetCookie(w)
}
login là 1 Handler c a web.ủ
Hàm Setcookie s d ng ReponseWriter đ tr v cho clientử ụ ể ả ề
func sayhelloName(w http.ResponseWriter, r *http.Request) {
GetCookie(r)
}
sayhelloName là 1 Handler c a webủ
Hàm Getcookie s d ng Request do nh n các thông s t clientử ụ ậ ố ừ
17. Network service
●
we need an IP address and port number to have a unique socket.
●
TCP socket
– TCPConn struct đ mô t 1 k t n i socket.ể ả ế ố
● TCPConn can be used by either client or server for reading and writing data.
– after a connection is established, the server has the same type of connection object for the
current connection → c phía client và server cùng có 1 object đ mô t k t n i.ả ể ả ế ố
– clients send requests to servers through a TCPConn and receive information from the server
response
– servers read and parse client requests, then return feedback
● two key functions:
– func (c *TCPConn) Write(b []byte) (n int, err os.Error)
– func (c *TCPConn) Read(b []byte) (n int, err os.Error)
– TCPAddr struct to represent TCP address information:
● ResolveTCPAddr function to get a TCPAddr
18. Network service
● T o k t n i TCP:ạ ế ố
– DialTCP function in the net package to create a
TCP connection
● returns a TCPConn object
● func DialTCP(net string, laddr, raddr *TCPAddr) (c
*TCPConn, err os.Error)
–
19. Tham kh oả
● https://github.com/suyesh/go_training
●
#15: Mỗi 1 webserver chỉ có 1 loại handler nên các hàm lấy và tạo cookie thường đưa vào Package hoặc Func để gọi từ bên trong các Handler, phục vụ việc kiểm tra điều kiện cho các Handler