mybaits 學習筆記(搭建)一

一、導入mybaits jar包java

  手動導入mybaits github 下載地址 https://github.com/mybatis/mybatis-3/releasesmysql

  maven導入依賴git

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>github

二、主配置文件sql

在github上下載源碼包,並在\src\test\java\org\apache\ibatis\submitted\complex_property下拷貝Configuration.xml做爲配置樣板。數據庫

配置數據庫連接apache

<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db_name"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>數組

三、獲取SqlSessionmybatis

SqlSession做用 一、向sql語句傳入參數。 二、執行sql語句。 三、獲取執行sql語句的結果。 四、事務的控制。app

如何獲得SqlSession:

  1. 經過主配置文件獲取數據庫鏈接相關信息。
  2. 經過配置信息構建SqlSessionFactory。
  3. 經過SqlSessionFactory打開數據庫會話。

四、構建數據庫訪問類

  public class DBAcess {

public SqlSession getSqlSession() throws IOException{
//經過配置文件讀取數據庫連接信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml"); //配置文件路徑爲相對於代碼根目錄的路徑
//經過配置文件構建一個SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//經過SqlSessionFactory打開一個數據庫會話
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}

}

 

五、mabaits操做xml配置

  在github上下載源碼包,並在\src\test\java\org\apache\ibatis\submitted\complex_property下拷貝User.xml做爲配置樣板。

  在mapper下配置相應的sql操做如: 

<select id="queryUserList"   resultMap="UserResult">
  SELECT id,username,password  FROM user WHERE 1 = 1
</select>

根據須要配置相應的resultMap返回數據 如:   

<resultMap type="bean.User" id="UserResult">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="password" jdbcType="VARCHAR" property="password.encrypted"/>
</resultMap> 

 

六、將mabaits操做xml配置添加到主配置文件中

<mappers>
<mapper resource="oconfig/User.xml"/>
</mappers>

DAO層調用  例:sqlSession.selectList("User.queryUserList");//User對應<mapper namespace="User"> 

 

七、傳遞參數到SQL   結合OGNL

 

Mybaits中的OGNL
取值範圍  標籤中的屬性
     取值寫法  String與基本數據類型 _parameter 
 自定義類型  屬性名(username)
   集合  數組:array
 List:list
 Map:_parameter
  操做符  java經常使用操做符  +,-,*,/,==,!=,||,&&等
 本身特有的操做符  and,or,mod,in,not in

 

Mybaits中的OGNL
從集合中取出一條數據 數組 array[索引](String[])
array[索引]。屬性名(User[])
List list[索引](List<String>)
list[索引].屬性名(List<User>)
Map _parameter.key(Map<String,String>)
key.屬性名(Map<String,User>)
利用foreach標籤從集合中取出數據 <foreach collection="array" index="i" item="item">
數組 i:索引(下標)

item

item.屬性名

List
Map i:key

 

    

    

<select id="queryUserList" parameterType="bean.User" resultMap="UserResult">
<!-- SELECT * FROM user WHERE id = #{id:INTEGER} -->
select id,username,password from user where 1=1
<if test="username!= null and !&quot;&quot;.equals(username.trim())">and username=#{username}</if>
<if test="password!= null and !&quot;&quot;.equals(password.trim())">and password =#{description}</if>
</select>

 

     例:

    User user = new User(); 

    user.setUsername("...");

    DAO層調用  例:sqlSession.selectList("User.queryUserList",user);

    

八、log4j配置

  maven資源:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

手動可到mybatis-3.4.5\lib下獲取

 

        在mybaits源碼包下mybatis-3-mybatis-3.4.5\src\test\java下拷貝log4j.properties至java源碼根目錄下

  修改日誌級別爲debug可查看mybatis調試日誌

  

log4j.rootLogger=DEBUG, stdout

#log4j.logger.org.apache.ibatis=ERROR

log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%nlog4j.logger.org.apache=INFO

相關文章
相關標籤/搜索