Skip to content

Commit 262c87f

Browse files
committed
add mybatis
1 parent 5109253 commit 262c87f

File tree

9 files changed

+101
-0
lines changed

9 files changed

+101
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

languages/java/jvm/jvm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
> 工具是建立在应用场景之上的,那么是什么原因让你使用了mybatis?
2+
3+
# 1.传统的JDBC的优缺点
4+
5+
## 1.2 传统的JDBC的使用
6+
7+
```java
8+
Connection connection = null;
9+
PreparedStatement preparedStatement = null;
10+
ResultSet resultSet = null;//返回结果集
11+
User user = new User();
12+
try{
13+
// 加载数据库驱动
14+
Class.forName("com.mysql.jdbc.Driver");
15+
// 通过驱动管理类获取数据库链接
16+
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test? characterEncoding=utf-8", "root", "root");
17+
// 定义sql语句?表示占位符
18+
String sql = "select * from user where username = ?";
19+
// 获取预处理statement
20+
preparedStatement = connection.prepareStatement(sql);
21+
// 设置参数,第⼀个参数为sql语句中参数的序号(从1开始),第⼆个参数为设置的参数值
22+
preparedStatement.setString(1, "tom");
23+
// 向数据库发出sql执⾏查询,查询出结果集
24+
resultSet = preparedStatement.executeQuery();
25+
// 遍历查询结果集
26+
while(resultSet.next()){
27+
int id = resultSet.getInt("id");
28+
String username = resultSet.getString("username");
29+
//封装user
30+
user.setId(id);
31+
user.setUsername(username);
32+
}
33+
System.out.println(user);
34+
}catch (Exception e){
35+
e.printStackTrace();
36+
}finally {
37+
//释放资源
38+
if(resultSet != null){
39+
try{
40+
resultSet.close();
41+
}catch (SQLException e){
42+
e.printStackTrace();
43+
}
44+
}
45+
}
46+
47+
if(preparedStatement != null){
48+
try{
49+
preparedStatement.close();
50+
}catch (Exception e){
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
if(connection != null){
56+
try{
57+
connection.close();
58+
}catch (SQLException e){
59+
e.printStackTrace();
60+
}
61+
}
62+
63+
```
64+
65+
---
66+
67+
存在的问题:
68+
69+
* 数据库配置信息存在硬编码(硬编码一级关联,软编码多级关联)
70+
71+
+ 需要频繁创建与释放连接(oracle都是tcp,tcp三次握手)
72+
73+
解决方法:
74+
75+
76+
# 2.使用mybatis带来了哪些好处
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1.mybatis应用的设计模式
2+
3+
1.1 构建者模式
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
> 对mybatis项目逐渐拆分
2+
3+
# 1.mybatis项目需要的依赖
4+
5+
resource下文件
6+
classpath
7+
8+
## 1.2
9+
10+
# 2.mybatis传统DAO开发方式
11+
12+
13+
14+
# 3.动态代理方式
15+
16+
17+
18+
# mybatis项目架构
19+
20+
>好的项目是需要分析和理解的,如果是你,你会怎么设计这个项目?
21+

0 commit comments

Comments
 (0)