1. 程式人生 > >關於mybatis的理解

關於mybatis的理解

pmap 本地倉庫 col RF 什麽 別名 遠程倉庫 mda maven


1.IJ Intellij Idea 智能的想法

2.MyEclipse 啟動後占用的內存是300mb---500mb
Idea的內存 400---700mb
MyEclipse中的一個工作區(workspace)可以有N個project
idea 一個Project有多個Module

3.創建一個Project
選擇的是Maven--->archetype(骨架)

4.創建工程的時候,有一個快速的設置
設置如下
archetypeCatalog = internal

5.保存code 的時候一定要保存在非系統盤

6.一定要註意右下角的提示,auto import

7.需要創建一個模塊

8.pom.xml
pom Project Object Model 項目對象模型

9.maven的標準目錄
src main java
src main resources

src test java
src test resources

倉庫位置:c:\users\**\.m2\repository

10.maven的三種倉庫
本地倉庫 中央倉庫 遠程倉庫

11.SSM Spring(春天,泉水) 管理各種業務bean IOC 和 AOP思想
SpringMVC 子框架 請求的調度和派發 mvc框架
Mybatis 半自動化ORM框架,手動建表,手動寫SQL

SSH Spring java不死就是因為Spring的存在 Spring新穎的理念:Spring Boot SpringCloud
Struts2 (Struts1) 請求的調度和派發 mvc框架
Hibernate 全自動化ORM框架 自動建表 HQL自動的生成SQL

12.ORM Object Relational Mapping 對象(java對象 實體類)關系(MySQL Oracle SQL Server 關系型 二維表)映射(xml)

NoSQL

Redis

Mongodb

巨杉數據庫

13.Mybatis配置文件
名稱 Mybatis-Config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件的根節點-->
<configuration>
<!--節點是有順序的-->
<!--讓xml識別到jdbc.properties配置文件-->
<properties resource="jdbc.properties"></properties>
<!--數據庫的連接信息-->
<environments default="development">
<environment id="development">
<!--事務管理JDBC
tx.commit() tx.rollback()
取值: JDBC MANAGED
-->
<transactionManager type="JDBC" />
<!--數據源:
POOLED:連接池
UNPOOLED:不啟用連接池
JNDI: Java Naming And Directory Interface java命名與目錄接口
-->
<dataSource type="POOLED">
<!--數據庫的連接屬性-->
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>

14.實體類,創建接口,接口對應的配置文件,主配置中去關聯小配置
註意點:如果大家將小配置配置到了dao層,你必須手動改變build節點。
build節點的重新設置,就是讓src下的xml文件參與編譯到Target目錄。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

15.測試類
SqlSession:java程序和DB通信的入口。
SqlSessionFactory
SqlSessionBuilder:

@Test
public void findAll() throws IOException {
//1.根據大配置,裝載數據庫的連接信息
String resource="mybatis-config.xml";
//xxxxxxxxxxxxxxxxxxxxxxxxxx流
//2.將硬盤上的一個文件和輸入流進行綁定
InputStream is= Resources.getResourceAsStream(resource);
//3.構建Session工廠
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//4.根據工廠去獲取會話對象
SqlSession session = factory.openSession();
//5.執行SQL語句,獲得結果
List<Dept> list = session.selectList("cn.happy.dao.IDeptDAO.findAll");
for (Dept dept:list)
{
System.out.println(dept.getDeptname());
}
//6.關閉會話
session.close();

}

16.日誌集成
1.需要在resources目錄下添加一個名稱為log4j.properties的物理文件
2.pom.xml文件中必須有一個節點,日誌的配置
3.日誌的5大級別;Fatal>Error>Info>Debug>Trace
17.getMapper()

18.添加和修改
添加和修改一定要在事物環境中運行

19.作業
1.預習作業:
resultMap
刪除
別名 alias
resultMap
過程:1.現在Mybatis-config.xml設置一個
<settings>
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
2.在小配置中啟用
<resultMap id="myEmpMapper" type="Emp">
<id property="empno" column="empno"></id>
<result property="empname" column="empname"></result>
</resultMap>

<select id="findAll" resultMap="myEmpMapper">
select * from emp
</select>
結果:
23:58:44,830 DEBUG findAll:139 - ==> Preparing: select * from emp
23:58:44,908 DEBUG findAll:139 - ==> Parameters:
23:58:44,930 DEBUG findAll:139 - <== Total: 5
1 微冷的雨 null
2 老原 null
3 群主 null
4 房山一哥 null
5 拳王春暉 null

結果說明:即使使用*檢查所有列,那麽最終展示的結果仍然是在resultMap中用戶手動映射的列。


工具類

20. openSession底層到底做了什麽
1.openSession是SqlSessionFactory的方法,SqlSessionFactory是一個接口
2.尋址SqlSessionFactory實現類:DefaultSqlSessionFactory
3.尋找DefaultSqlSessionFactory.openSession()裏面調度了
openSessionFromDatasource(ExecutorType,TransactionIsolationLevel level,autoCommit)
4.讀取大配置文件,將配置文件的節點,變成對象進行分析。
final Environment environment =configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment)
tx= transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
final Executor executor = configuration.newExecutor(tx,execType,autoCommit);
return new DefaultSqlSession(configuration,executor);
5.
public DefaultSqlSession(Configuration configuration, Executor executor) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
}
結論:對sqlSession接口的實現類DefaultSqlSession的成員變量做初始化工作,並且dirty=false;默認
java程序內存中Data和DB中的Data保持一致。

21. insert和delete底層到底是什麽
public int insert(String statement) {
return insert(statement, null);
}

public int insert(String statement, Object parameter) {
return update(statement, parameter);
}

public int delete(String statement) {
return update(statement, null);
}

public int delete(String statement, Object parameter) {
return update(statement, parameter);
}

為什麽session.commit()能引起事物的提交
session.close()為什麽可以回滾事務

2.所有源代碼 電子版-----形成博客 idea。。。Mybatis案例 log4j集成的
3.手寫第一張上機練習++++++課後簡答代碼

*** //框架學習網站(內附有框架的各種流程圖)
https://github.com/

所有人需要從U3開始維護自己的一個項目
審批流轉
權限管理
Redis

作業:1.上午講解的內容博客+代碼
2.模糊查詢+多條件查詢
3.智能標簽,手寫一遍
4.調試成功
5.自己的項目,必須雛形,easyui 前端框架登錄,調整好

1. 模糊查詢 按照雇員姓名進行模糊查詢
2. 多條件查詢: 雇員姓名(模糊匹配) 部門名稱(完全匹配)

重點內容回顧:
1.SqlSession生命周期和作用域
1.1 單次會話,說白了就是和數據庫服務器的一個連接。

2.SqlSessionFactory接口,實現類是DefaultSqlSessionFactory
openSession() 可以構建出一個Session對象(和數據庫進行交互的上下文入口)。

3.resultMap和resultType
resultMap作用:解決屬性名和字段名不匹配的問題

<settings> 默認的value值是PARTIAL 是否自動映射
<property name="autoMappingBehavior" value="NONE"></property>
</settings>

4.模糊查詢
where studentName like ‘%‘ #{studentName} ‘%‘ 能防止SQL註入
where studentName like concat(‘%‘,#{studentName},‘%‘) 能防止SQL註入
where studentName like ‘%${studentName}%‘ 不能防止SQL註入 不推薦使用

5.多條件查詢:多個參數封裝成Map
dao:
public List<Emp> findListByManyCondition(Map<String,Object> map);
xml:
<select id="findListByManyCondition" resultType="Emp">
select Emp.* from emp,dept where emp.deptno=dept.deptno and empname like ‘%‘ #{empname} ‘%‘ and deptname=#{deptname}
</select>

6.delete 和 insert 底層 使用的都是update

7.別名的配置
<typeAliases>
<typeAlias>
<package name="con.happy.entity"></package>
<typeAlias>
</typeAliases>
-------------------------------------------------------------------------
1.多條件查詢的第二種方式,索引號
<!--多條件查詢索引版-->
<select id="findListByManyConditionIndex" resultType="Emp">

select * from emp where empname like ‘%‘ #{0} ‘%‘ and deptno>#{1}

</select>

List<Emp> list = mapper.findListByManyConditionIndex("雨",2);

2.添加完成後獲取ID自增列值 手段
<!--添加完成後,返回自增列的值-->
<insert id="returnAutoIncrementValue">
insert into emp(empname,deptno) values(#{empname},#{deptno})
<selectKey resultType="int" keyProperty="empno" order="AFTER">
select @@IDENTITY
</selectKey>
</insert>

3.智能標簽
1. if
根據員工的姓名和員工的部門編號,查找符合條件的記錄
Preparing: select * from emp WHERE empname like ‘%‘ ? ‘%‘ and deptno=?
Preparing: select * from emp WHERE empname like ‘%‘ ? ‘%‘
Preparing: select * from emp
2. where
3.choose
java switch 選擇結構====只走一個分支
4.foreach
數組
List Integer
自定義List泛型

5.例子
增刪改+分頁

關於mybatis的理解