MySQL database operations using scalikejdbc
About scalikejdbc :
An import-dependent scalikejdbc in the IDEA
Second, add files in Resources :
Inside editor:
Third, create a user table in the database:
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()
}
}
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()
}
}
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()
}
}
Take a look at the database to the virtual machine:
Seven, written on the back of:
Do not forget to define a class user