With scalikejdbc3.3.2 version uses MySQL database to operate the scala

MySQL database operations using scalikejdbc

About scalikejdbc :

Here Insert Picture Description

An import-dependent scalikejdbc in the IDEA

Here Insert Picture Description

Second, add files in Resources :

Here Insert Picture Description
Inside editor:
Here Insert Picture Description

Third, create a user table in the database:

Here Insert Picture Description

Fourth, the first data is inserted into 10:

DBs.setupAll()

val person:List[user] = List (

  user(1,"simayi",54),
  user(2,"zhugeliang",56),
  user(3,"caocao",68),
  user(4,"liubei",66),
  user(5,"sunquan",53),
  user(6,"zhanyun",45),
  user(7,"lvbu",55),
  user(8,"yuanshao",69),
  user(9,"zhouyu",45),
  user(10,"machao",57)

)
val insert = DB.localTx{implicit session => {

  for (x<-person){

    SQL("insert into user values(?,?,?)").bind(x.id,x.name,x.age).update().apply()

  }

}

Here Insert Picture Description

Fifth, all the data query:

 val memberIDs = DB readOnly{implicit session =>{
    sql"select * from user ".map(rs=>user(rs.int("id"),rs.string("name"),rs.int("age"))).list.apply()
  }
  }

Here Insert Picture Description
Here Insert Picture Description

Sixth, the 108 plus ID = age of:

val update = DB.autoCommit{implicit session =>{
    SQL("update user set age = age + 10 where id = ?").bind("8").update().apply()
  }
  }

Here Insert Picture Description
Take a look at the database to the virtual machine:
Here Insert Picture Description

Seven, written on the back of:

Here Insert Picture Description
Do not forget to define a class user

Guess you like

Origin blog.csdn.net/weixin_37761111/article/details/89962959