tensorflow實現簡單的自編碼器
1、mybatis是什麼,為什麼要用mybatis?
Mybatis是一個優秀的持久層框架,它對jdbc的操作資料庫的過程進行封裝,使開發者只需要關注SQL本身,而不需要花費精力去處理例如註冊驅動、建立connection、建立statment、手動設定引數、結果集檢索等jdbc繁瑣的過程程式碼。
Mybatis通過xml或註解的方式將要執行的各種statement配置起來,並通過Java物件和statement中的sql進行對映生成最終執行的sql語句,最後由mybatis框架執行sql並將結果對映成Java隊形並返回。
Mybatis架構圖
2、第一個Mybatis程式
執行步驟:
-
1、匯入mybatis的相關jar包
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
-
2、編寫核心配置檔案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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hao/dao/UserMapper.xml"/> </mappers> </configuration>
-
3、編寫實體類、介面、工具類
實體類:
package com.hao.pojo; public class User { private int id; private String myname; private int age; private String address; public User() { } public User(int id, String myname, int age, String address) { this.id = id; this.myname = myname; this.age = age; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMyname() { return myname; } public void setMyname(String myname) { this.myname = myname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", myname='" + myname + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }
介面:
package com.hao.dao; import com.hao.pojo.User; import java.util.List; public interface UserMapper { List<User> getUserList(); }
工具類:
package com.hao.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { //獲取sqlSessionFactory物件 try { String resource = "mybatis-config.xml"; InputStream inputStream =Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //獲取SqlSession物件 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
-
4、編寫實體類mapper.xml對映檔案
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hao.dao.UserMapper"> <select id="getUserList" resultType="com.hao.pojo.User"> select * from users </select> </mapper>
-
5、測試
package com.hao.dao;
import com.hao.pojo.User;
import com.hao.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MybatisTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
3、配置解析
核心配置檔案mybatis-config.xml中的配置的設定和屬性
configuration(配置) properties(屬性) settings(設定) typeAliases(類型別名) typeHandlers(型別處理器) objectFactory(物件工廠) plugins(外掛) environments(環境配置) environment(環境變數) transactionManager(事務管理器) type="[JDBC|MANAGED]" dataSource(資料來源) type="[UNPOOLED|POOLED|JNDI]" databaseIdProvider(資料庫廠商標識) mappers(對映器)
3.1 配置環境(environments)
Mybatis可以配置成適應多種環境,不過要記住:儘管可以配置多個環境,但每個sqlsessionFactory例項只能選擇一種環境。
Mybatis預設的事務管理器就是JDBC,連結池:pooled
3.2屬性(properties)
這些屬性可以在外部進行配置,並可以進行動態替換。你既可以在典型的 Java 屬性檔案中配置這些屬性,也可以在 properties 元素的子元素中設定。例如:
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
設定好的屬性可以在整個配置檔案中用來替換需要動態配置的屬性值。比如:
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
3.3類型別名(typeAliases)
類型別名可為 Java 型別設定一個縮寫名字。 它僅用於 XML 配置,意在降低冗餘的全限定類名書寫。例如:
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
<typeAlias alias="Blog" type="domain.blog.Blog"/>
<typeAlias alias="Comment" type="domain.blog.Comment"/>
<typeAlias alias="Post" type="domain.blog.Post"/>
<typeAlias alias="Section" type="domain.blog.Section"/>
<typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
當這樣配置時,Blog
可以用在任何使用 domain.blog.Blog
的地方。
也可以指定一個包名,MyBatis 會在包名下面搜尋需要的 Java Bean,比如:
<typeAliases>
<package name="domain.blog"/>
</typeAliases>
3.4對映器(mappers)
既然 MyBatis 的行為已經由上述元素配置完了,我們現在就要來定義 SQL 對映語句了。 但首先,我們需要告訴 MyBatis 到哪裡去找到這些語句。 在自動查詢資源方面,Java 並沒有提供一個很好的解決方案,所以最好的辦法是直接告訴 MyBatis 到哪裡去找對映檔案。 你可以使用相對於類路徑的資源引用,或完全限定資源定位符(包括 file:///
形式的 URL),或類名和包名等。例如:
方式一:
<!-- 使用相對於類路徑的資源引用 -->
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
<mapper resource="org/mybatis/builder/BlogMapper.xml"/>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
方式二:
注意點:
- 介面和他的mapper配置檔案必須同名
- 介面和他的mapper配置檔案必須在同一包下
<!-- 使用對映器介面實現類的完全限定類名 -->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
方式三:
注意點:
- 介面和他的mapper配置檔案必須同名
- 介面和他的mapper配置檔案必須在同一包下
<!-- 將包內的對映器介面實現全部註冊為對映器 -->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
3.5解決屬性名和欄位名不一致的問題(ResultMap結果集對映)
問題:實體類的屬性名和資料庫表中的欄位名不一樣?該怎麼解決呢?
解決方法:
- 起別名:
<select id="getuser" resultType="User">
select id,name,pwd as password from user where id=#{id}
</select>
- ResultMap結果集對映
<select id="getuser" resultMap="UserMap">
select * from user where id=#{id}
</select>
<resultMap id="UserMap" type="User">
<!--column為資料庫中欄位名,property為實體類中的屬性-->
<result column="id" property="id"/>
<result column="name" property="name"/> <!--如果欄位名和屬性名一樣,對映可有可無-->
<result column="pwd" property="password"/>
</resultMap>
4、日誌
如果一個數據庫操作出現了異常,我們需要排查,日誌就是最好的助手。
在mybatis-config.xml核心配置檔案中配置settings,新增日誌即可
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
4.1Log4j
什麼是Log4j?
- Log4j是Apache的一個開源專案,通過使用Log4j,我們可以控制日誌資訊輸送的目的地是控制檯、檔案、GUI元件
- 我們也可以控制每一條日誌的輸出格式;
- 通過定義每一條日誌資訊的級別,我們能夠更加細緻地控制日誌的生成過程。
- 這些可以通過一個配置檔案來靈活地進行配置,而不需要修改應用的程式碼。
5、分頁
使用limit分頁
語法:select * from user limit startIndex,pagesize;
mybatis中是通過mapper介面方式的呼叫sql層面實現分頁
#{} 和${}的區別
-
{}為佔位符,即sql預編譯,#{} 的變數替換是在DBMS 中,變數替換後,#{} 對應的變數自動加上單引號 ',#{} 能防止sql 注入
-
${}為拼接符,即sql拼接,${} 的變數替換是在 DBMS 外,變數替換後,${} 對應的變數不會加上單引號 '',${} 不能防止sql 注入
6、多對一、一對多
6.1 多對一處理
示例:多個學生關聯一個老師
方式一:
實體類
package com.hao.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
package com.hao.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
按照查詢巢狀處理
<!--思路:
1.查詢所有學生資訊
2.根據查詢出來的tid,找到對應的老師資訊,子查詢
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="com.hao.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="com.hao.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.hao.pojo.Teacher">
select * from teacher where id =#{id}
</select>
方式二:
按照結果巢狀處理
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname,t.id tidd
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="com.hao.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.hao.pojo.Teacher">
<result property="name" column="tname"/>
<result property="id" column="tidd"/>
</association>
</resultMap>
6.2一對多處理
一個老師教多個學生
實體類:
package com.hao.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
}
package com.hao.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
方式一:按照子查詢巢狀處理
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id=#{id}
</select>
<resultMap id="TeacherStudent2" type="teacher">
<result property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudent"/>
</resultMap>
<select id="getStudent" resultType="student">
select * from student where tid=#{id}
</select>
方式二:按照結果集巢狀處理
<select id="getTeacher" resultMap="TeacherStudent">
select t.id tid,t.name tname,s.id sid,s.name sname,s.tid stid
from teacher t ,student s
where t.id=s.tid and t.id=#{id}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="stid"/>
</collection>
</resultMap>
個人推薦使用第二種,方便、快捷,一步到位。
小結
1、關聯-association [多對一]
2、集合-collection [一對多]
3、javaType、ofType
- javaType 用來指定實體類種屬性的型別
- ofType 用來指定對映到List或者集合中的實體類性,泛型種的約束型別。
7、動態SQL
什麼是動態sql:動態sql就是根據不同的條件生成不同的sql語句
if
使用動態 SQL 最常見情景是根據條件包含 where 子句的一部分。比如:
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>
trim(where,set)
where 元素只會在子元素返回任何內容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們去除。
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
用於動態更新語句的類似解決方案叫做 set。set 元素可以用於動態包含需要更新的列,忽略其它不更新的列。比如:
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
如果 where 元素與你期望的不太一樣,你也可以通過自定義 trim 元素來定製 where 元素的功能。比如,和 where 元素等價的自定義 trim 元素為:
prefixOverrides 屬性會忽略通過管道符分隔的文字序列(注意此例中的空格是必要的)。上述例子會移除所有 prefixOverrides 屬性中指定的內容,並且插入 prefix 屬性中指定的內容。
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
choose(when,otherwise)
有時候,我們不想使用所有的條件,而只是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
sql片段
有些時候,我們可能會將一些功能部分抽取出來,方便複用
1、使用sql標籤抽取公共部分
2、在需要使用的地方使用include標籤即可。
注意事項:
- 最好基於單表定義sql片段
- 不要存在where標籤
forEach
動態 SQL 的另一個常見使用場景是對集合進行遍歷(尤其是在構建 IN 條件語句的時候)。比如:
foreach 元素的功能非常強大,它允許你指定一個集合,宣告可以在元素體內使用的集合項(item)和索引(index)變數。它也允許你指定開頭與結尾的字串以及集合項迭代之間的分隔符。這個元素也不會錯誤地新增多餘的分隔符,看它多智慧!
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
8、快取
8.1什麼是快取
1、什麼是快取【Cache】?
- 存在記憶體種的臨時資料
- 將使用者經常查詢的資料放在快取(記憶體)中,使用者去查詢資料就不用從磁碟上(關係型資料庫資料檔案)查詢,從快取中查詢,從而提高查詢效率,解決了高併發系統的效能問題。
2、為什麼使用快取?
- 減少和資料庫的互動次數,減少系統的開銷,提高系統的效率
3、什麼樣的資料能使用快取
- 經常查詢且不經常改變的資料
8.2Mybatis快取
- Mybatis包含了一個非常強大的查詢快取特徵,它可以非常方便地定製和配置快取。快取可以極大的提升查詢效率。
- Mybatis系統中預設定義了兩級快取:一級快取和二級快取
- 預設情況下,只有一級快取開啟。(Sqlsession級別的快取,也稱為本地快取)
- 二級快取需要手動開啟和配置,它是基於namespace級別的快取。
- 為了提高擴充套件性,Mybatis定義了介面Cache。我們可以通過實現Cache介面來自定義二級快取
8.3一級快取
- 一級快取也叫本地快取:Sqlsession
- 與資料庫同一次會話期間查詢到的資料會放在本地快取中。
- 以後如果需要獲取相同的資料,直接從快取中拿,沒必要再去查詢資料庫
- 快取失效的情況:
- 查詢不同的東西
- 增刪改操作,可能會改變原來的資料,所以必定會重新整理快取。
- 查詢不同的mapper.xml檔案
- 手動清理快取 sqlsession.clearCache();
小結:一級快取是預設開啟的,只在一次Sqlsession中有效,也就是拿到連線到關閉連線的這個區間!
8.4二級快取
- 二級快取也叫全域性快取,一級快取作用域太低了,所以誕生了二級快取。
- 基於namaspace級別的快取,一個名稱空間,對應的一個二級快取。
- 工作機制:
- 一個會話查詢一條資料,這個資料就會被放在當前會話的一級快取中;
- 如果當前會話關閉了,這個會話對應的一級快取就沒了;但是我們想要的是,會話關閉了,一級快取中的資料就會被儲存到二級快取中;
- 新的會話查詢資訊,就可以從二級快取中獲取內容;
- 不同的mapper查出的資料會放在自己對應的快取中;
步驟:
1、開啟全域性快取(cacheEnabled預設的值為true)
<setting name="cacheEnabled" value="true"/>
2、在要使用二級快取中的Mapper中開啟
<cache/>
也可以自定義引數
<cache eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
問題:我們需要將實體類序列化,實體類需要實現Serializable介面
小結:
- 只要開啟了二級快取,在同一個mapper中就有效
- 所有的資料都會先放在一級快取中;
- 只有當會話提交,或者關閉的時候,才會提交到二級快取中。