react 前端框架如何驱动企业数字化转型与创新发展
224
2023-12-05
本篇内容介绍了“怎么使用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数据库”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~