本篇内容介绍了“怎么使用ScalikeJDBC操作MySQL数据库”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
环境:
IDEA+Maven+ScalikeJDBC+MySQL5.6
1 新建Maven的Scala工程
2 pom.xml文件添加依赖:
<properties>
<scala.version>2.11.8</scala.version>
<!-- 添加scalikejdbc 和 mysql 的version 定义 -->
<scalikejdbc.version>2.5.2</scalikejdbc.version>
<mysql.version>5.6.23</mysql.version>
</properties>
<dependencies>
<!-- 添加scalikejdbc的dependency -->
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc_2.11</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc-config_2.11</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<!-- 添加mysql的dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
3 新增Scala对象
package com.ruozedata
import scalikejdbc._
object ScalaLikeJdbc {
def main(args: Array[String]): Unit = {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver")
// 指定数据库连接url,userName,password
val url = "jdbc:mysql://192.168.1.108:3306/ruozedata?useUnicode=true&characterEncoding=UTF8"
val userName = "root"
val password = "root"
// 设置数据库连接参数
val settings = ConnectionPoolSettings(
initialSize = 5,
maxSize = 20,
connectionTimeoutMillis = 3000L,
validationQuery = " select 1 from test ") // test表是专门创建的一个测试表
// 构建数据库连接池
ConnectionPool.singleton(url, userName, password, settings)
// 新增数据
val insertResult: Int = DB autoCommit { implicit session =>
SQL("insert into user(id, name,age) values(?,?,?)").bind(1, "Zhangsan",15).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(2, "Ruoze",16).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(3, "Jepson",17).update().apply()
}
println(insertResult) // 打印 insertResult 变量的值,如果新增成功,返回1,否则返回0
// 定义User类
case class User(id: Int, name: String, age: Int)
// 查询数据操作
val allColumns = (rs: WrappedResultSet) => User(
id = rs.int("id"),
name = rs.string("name"),
age = rs.int("age"))
val users: List[User] = DB readOnly { implicit session =>
SQL("select * from user ").map(allColumns).list.apply()
}
// 循环遍历 users对象 并输出
for(user <- users) {
println(user.id + "," + user.name + "," + user.age)
}
}
}
4 添加jdbc的MySQL驱动包
mysql-connector-java-commercial-5.1.25-bin.jar
5 运行结果如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (scalikejdbc.ConnectionPool$).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
1
1,Zhangsan,15
2,Ruoze,16
3,Jepson,17
Process finished with exit code 0
“怎么使用ScalikeJDBC操作MySQL数据库”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。