[] Scala Scala scalikejdbc use tool to connect MySQL (recommended)

scalikejdbc official website: HTTP: //scalikejdbc.org/
ScalikeJDBC is a simple database access library. The library of natural casing JDBC API.

Requirements: Use scalikejdbc3.3.2 version of the MySQL database to operate
1) Prepare a table by SQL user, there are id, name, age three fields
2) Add 10 records to the user table by scalikejdbc
3) query the user table all by scalikejdbc data
4) by the scalikejdbc id = 8 data age make changes, plus 10 

mysql> use ruoze_g6;
mysql> create table user( id int, name varchar(100), age int );

1) pom need to add ** ** dependent scalikejdbc of:

 <dependency>
            <groupId>org.scalikejdbc</groupId>
            <artifactId>scalikejdbc_2.11</artifactId>
            <version>3.3.2</version>
</dependency>
<dependency>
            <groupId>org.scalikejdbc</groupId>
            <artifactId>scalikejdbc-config_2.11</artifactId>
            <version>3.3.2</version>
</dependency>

2) Configuration

Right Directory, name resources:


Modify cascade drive, scala is Sources, resources are Resources profile directory:



Create a configuration file, and the file contents:


db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://hadoop001:3306/ruoze_g6"
db.default.user="root"
db.default.password="123456"

3) Code:

package com.ruoze
import scalikejdbc._
import scalikejdbc.config._
object ScalalikeJdbc{
      def main(args:Array[String]):Unit = {

            // loaded by default db.default. * Configuration information
            DBs.setupAll ()

             1. scalikejdbc query the user table all the data, encapsulated into objects
              Val memberIds: List [the User] = {Implicit the session DB readOnly =>
            SQL "SELECT * from user" .map (RS => the User (rs.int ( " ID "), rs.string (" name "), rs.int (" Age "))) list.apply ().
              }
              the println (memberIds)
              printed results: List (User (1, tom , 20), User (2, Merry, 20 is), the User (. 3, Mike, 20 is), the User (. 4, Jeff, 21 is), the User (. 5, FF, 22 is), the User (. 6, ddd, 23 is))
          
          
              2. insert data into a successful return 1
              Val the above mentioned id = {the Implicit DB.localTx the session =>
            SQL "INSERT INTO the User (the above mentioned id, name, Age) VALUES (?,?,?)." the bind (2, "Merry", 20) .Update () .apply ()
              }
              the println (ID)
          
          
              3. insert a plurality of data
              val users:List[User]= List(
            User(4,"jeff",21),
            User(5,"ff",22),
            User(6,"ddd",23)
              )
              val id = DB.localTx { implicit session =>
            for(x <- users){
              sql"INSERT INTO user(id, name, age) VALUES (?,?,?)".bind(x.id, x.name, x.age).update().apply()
            }
              }
          
          
              4.插入数据   使用自动提交
              val id = DB.autoCommit { implicit session =>
                  sql"INSERT INTO user(id, name, age) VALUES (?,?,?)".bind(7, "merry", 20).update().apply()
               }
              println(id)


              5. Modify data id = 1 data plus age 10
              Val DB.autoCommit = {Implicit Update the session =>
             the SQL ( "Update User SET age = 10 + age WHERE id =?"). The bind ( "1"). Update (). Apply ()
              }

              DBs.closeAll ()
      }
      
      // user entity:
      Case the User class (ID: Int, name: String, Age: Int)
}

Guess you like

Origin www.cnblogs.com/huomei/p/12103736.html