- 1.1、通过SQL准备一张表,有id,name,age三个字段
- 1.2、通过Scalikejdbc往user表中添加10条记录
- 1.3、通过Scalikejdbc查询表中所有的数据
- 1.4、通过Scalikejdbc将id=8的数据的age加10
一、Scalikejdbc操作MySQL数据库
- Scalikejdbc官网:http://scalikejdbc.org/documentation/configuration.html
1、pom文件中添加scalikejdbc的依赖&&MySQL的驱动
<scalikejdbc.version>3.3.2</scalikejdbc.version>
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc-mapper-generator</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc-config_2.11</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<!--MySQL的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
2、IDEA中创建一个ScalikeJdbcApp:
import scalikejdbc._
import scalikejdbc.config.DBs
case class Employer(name: String, age: Int, salary: Long)
val config = DBs.config
val employers = List(Employer("zhangsan", 20, 18000),Employer("zhangliu", 50, 300000), Employer("lisi", 22, 22000))
//批量插入
insert(employers)
println("----------------insert执行完毕---------------")
DBs.closeAll()
}
def insert(employers: List[