MyBatis框架連線資料庫的基本使用第四篇
阿新 • • 發佈:2021-01-27
MyBatis框架連線資料庫的基本使用
- 匯入jar包
- 建立並配置核心配置檔案
- 建立並配置對映檔案
- 保證SQL語句中的id和方法名一致
- 保證對映檔案中的namespace和介面的全限定名一致
/**
* MyBatis 最基本的演示
* MyBatis使用步驟
* 匯入jar包
* 建立並配置核心檔案
* 建立並配置對映檔案
* 對映檔案中SQL語句的ID要和介面方法名一致
* 對映檔案中的nameSpace要和介面的全限定名一致
* 建立測試
* 獲取核心配置檔案的輸入流
* 獲取sqlSessionFactory
* 獲取sqlSession
* 獲取介面的動態代理子類
* 介面的動態代理子類呼叫方法
*
*/
下面是程式碼可以幫助你更好的理解
匯入jar包
mybatis的jar包以及資料庫連線的jar包
建立並配置核心配置檔案
<?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>
< !-- <properties>
<property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties> -->
<!--
<properties>:設定或引入資原始檔
resource:在類路徑下訪問資原始檔
url:在網路路徑或磁碟路徑下訪問資原始檔
-->
<properties resource="jdbc.properties"></properties>
<settings>
<!-- 將下劃線對映成駝峰,user_name對映為userName -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 開啟延遲載入 預設是關閉的 兩個屬性要同時設定 -->
<!-- 開啟延遲載入 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 是否查詢所有資料 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<!--
為型別設定類型別名
type:Java 型別,若只設置type,預設的別名就是型別,且不區分大小寫
就是給類設定別名,例如下面的u,就可以在 對映檔案SQL語句執行返回值中寫u
-->
<!-- <typeAlias type="com.atguigu.bean.User" alias="u"/> -->
<package name="bean"/>
</typeAliases>
<!--
<environments>:設定連線資料庫的環境
default:設定預設使用的資料庫環境
-->
<environments default="mysql">
<!--
<environment>:設定某個具體的資料庫的環境
id:資料庫環境的唯一標示
-->
<environment id="mysql">
<!-- type="JDBC|MANAGED" -->
<transactionManager type="JDBC" />
<!-- type="POOLED|UNPOOLED|JNDI" -->
<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>
<environment id="oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssm" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 引入對映檔案 -->
<!-- 引入對映檔案可以設定多個mapper
<mapper resource="" />
<mapper resource="" />
也可以使用 <package/>標籤
此種寫法要求mapper介面和mapper對映檔案必須在同一個包下
這樣方法可以在sourceFolder中建立一個和介面包名全限定名一致的包路徑,以實現介面和對映檔案上的物理隔離
<package name="com.atguigu.mapper"/>
這兩個方法不可以混用
-->
<mappers>
<!-- <mapper resource="userMapper.xml" /> -->
<package name="mapperInterface"/>
</mappers>
</configuration>
建立並配置對映檔案
<?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="mapperInterface.SqlMapper">
<!-- User selectUser1(Integer id); -->
<select id="selectUser1" resultType="User">
select id,userName,age,sex
from testuser where
id=#{id}
</select>
<!-- User selectUser2(Integer id); -->
<select id="selectUser2" resultType="User">
select id,userName,age,sex
from testuser where
id=${value}
</select>
<!-- User selectUserByMore1(Integer id,String userName); -->
<select id="selectUserByMore1" resultType="User">
select
id,userName,age,sex from testuser where id=#{0} and
userName=#{1}
</select>
<!-- User selectUserByMore2(Integer id,String userName); -->
<select id="selectUserByMore2" resultType="User">
select
id,userName,age,sex from testuser where id=${param1} and
userName='${param2}'
</select>
<!-- User selectUserByMap(Map<String,Object> map); -->
<select id="selectUserByMap" resultType="User">
select
id,userName,age,sex from testuser where id=${id} and
userName='${userName}'
</select>
<!-- User selectUserByPram(@Param("id")Integer id,@Param("UserName")String
userName); -->
<select id="selectUserByPram" resultType="User">
select
id,userName,age,sex from testuser where id=${id} and
userName='${userName}'
</select>
<!-- User selectUserByList(List list); -->
<select id="selectUserByList" resultType="User">
</select>
<!-- User selectUserByArray(Integer[] id); -->
<select id="selectUserByArray">
</select>
<!-- 自定義對映 -->
<!-- 自定義對映 <id column="id" property="id" />
<id>:設定主鍵的對映關係,column設定欄位名,property設定屬性名
<result column="sex" property="sex" />
<result>:設定非主鍵的對映關係,column設定欄位名,property設定屬性名
<select>語句中的id還是需要和介面中的方法名一致 resultMap需要和<resultMap>中的id保持一致
<resultMap>中的type是查詢返回結果的型別
column是指查詢到的欄位名 property是指查詢到的欄位名對映給JavaBean中的哪個屬性
-->
<resultMap type="User" id="UserDepList">
<id column="id" property="id" />
<result column="userName" property="userName" />
<result column="age" property="age" />
<result column="sex" property="sex" />
<!-- <result column="depId" property="dep.depId" />
<result column="depName" property="dep.depName" /> -->
<!-- 自定義對映中可以使用<association>來定義JavaBean中的JavaBean -->
<association property="dep" javaType="Dep">
<id column="depId" property="depId" />
<result column="depName" property="depName" />
</association>
</resultMap>
<!-- ArrayList<User> selectUserDep(); -->
<select id="selectUserDep" resultMap="UserDepList">
select
id,userName,age,sex,testdep.depId,depName from testUser left join
testdep on
testuser.id=testdep.depId
</select>
<resultMap type="User" id="moreStep">
<id column="id" property="id"/>
<result column="userName" property="userName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<!--
select:分步查詢的SQL的id,即介面的全限定名.方法名或namespace.SQL的id
column:分步查詢的條件,注意:此條件必須是從資料庫查詢過得
-->
<association property="dep" select="selectDepByDepId" column="depId"></association>
</resultMap>
<!-- User selectUserDepByMoreStep(Integer id); -->
<select id="selectUserDepByMoreStep" resultMap="moreStep">
select id,userName,age,sex,depId from testuser where id=#{id}
</select>
<!-- Dep selectDepByDepId(Integer depId); -->
<select id="selectDepByDepId" resultType="Dep">
select depId,depName from testdep where depId=#{depId}
</select>
<resultMap type="Dep" id="DepUserList">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
<collection property="list" ofType="User">
<id column="id" property="id"/>
<result column="userName" property="userName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
<!-- Dep selectDepUser(Integer depId); -->
<select id="selectDepUser" resultMap="DepUserList">
select testdep.depId,depName,id,userName,age,sex from
testdep left join
testuser on testdep.depId=testuser.depId
where
testdep.depId=#{depId}
</select>
<resultMap type="Dep" id="DepUserByMoreStep">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
<!-- collection:處理一對多和多對多的關係 -->
<!-- fetchType 是否開啟延遲載入 lazy開啟 eager不開啟 -->
<collection property="list" select="selectUserBydepId" column="depId" fetchType="eager"></collection>
</resultMap>
<!-- Dep selectDepUserByMoreStep(Integer id); -->
<select id="selectDepUserByMoreStep" resultMap="DepUserByMoreStep">
select depId,depName from testdep where depId=#{depId}
</select>
<!-- ArrayList<User> selectUserBydepId(Integer id); -->
<select id="selectUserBydepId" resultType="User">
select id,userName,age,sex from testuser where depId=#{depId}
</select>
</mapper>