国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

mybatis筆記

2019-11-08 03:25:41
字體:
供稿:網(wǎng)友

JDBC連接:

Dao包:執(zhí)行sql語句,獲取操作結(jié)果封裝的對象,返回操作結(jié)果。

Mybatis:(向dao提供Sqlsession

(1)能與數(shù)據(jù)庫交互(2)能執(zhí)行sql語句

SqlSession作用:

(1)向SQL傳入?yún)?shù)

(2)執(zhí)行SQL語句

(3)獲取執(zhí)行SQL語句結(jié)果

(4)事物的控制

如何獲得SqlSession:

(1)通過配置文件獲取相關(guān)數(shù)據(jù)庫連接的信息

(2)通過配置信息構(gòu)建SqlSessionFactory

(3)通過SqlSessionFactory打開數(shù)據(jù)庫會話

mybatis下載網(wǎng)址:https://github.com/mybatis/mybatis-3/releases

mybatis驅(qū)動(dòng)路徑:D:/javaWeb/mybatis/源碼包/mybatis-3-mybatis-3.4.2/src/test/java/org/apache/ibatis/submitted/complex_PRoperty

配置步驟:

(1)把mybatis包導(dǎo)入工程,放到lib下邊。并創(chuàng)建對應(yīng)的jsp頁面。

(2)打開mybatis的驅(qū)動(dòng)路徑,復(fù)制下邊的xml驅(qū)動(dòng)文件,mybatis驅(qū)動(dòng)路徑:D:/JavaWeb/mybatis/源碼包/mybatis-3-mybatis-3.4.2/src/test/java/org/apache/ibatis/submitted/complex_property。

(3)更改圖上的代碼為MySQL,還有你的數(shù)據(jù)庫,用戶名和密碼。

   <dataSource type="UNPOOLED">        <property name="driver" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://127.0.0.1:3306/mirco"/>        <property name="root" value="root"/>

      <property name="root" value="root"/><!-- 補(bǔ)上用戶密碼-->      </dataSource>

(4)測試時(shí)候可以先注釋一些代碼。

<?xml version="1.0" encoding="UTF-8" ?><!--       Copyright 2009-2016 the original author or authors.       Licensed under the Apache License, Version 2.0 (the "License");       you may not use this file except in compliance with the License.       You may obtain a copy of the License at          http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software       distributed under the License is distributed on an "AS IS" BASIS,       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.       See the License for the specific language governing permissions and       limitations under the License.--><!DOCTYPE configuration    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--   <settings>    <setting name="useGeneratedKeys" value="false"/>    <setting name="useColumnLabel" value="true"/>  </settings>  <typeAliases>    <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/>  </typeAliases>-->  <environments default="development">    <environment id="development">      <transactionManager type="JDBC">        <property name="" value=""/>      </transactionManager>       <dataSource type="UNPOOLED">        <property name="driver" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://127.0.0.1:3306/mirco"/>        <property name="username" value="root"/>         <property name="passWord" value="root"/>      </dataSource>    </environment>  </environments>  <mappers>    <mapper resource="util/sqlxml/Message.xml"/>  </mappers></configuration>

(5)創(chuàng)建Message.xml

<?xml version="1.0" encoding="UTF-8"?><!--       Copyright 2009-2016 the original author or authors.       Licensed under the Apache License, Version 2.0 (the "License");       you may not use this file except in compliance with the License.       You may obtain a copy of the License at          http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software       distributed under the License is distributed on an "AS IS" BASIS,       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.       See the License for the specific language governing permissions and       limitations under the License.--><!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 通過namespace區(qū)分,不同的namespace可以有同樣的id --><mapper namespace="Message">  <resultMap type="bean.Message" id="MessageResult">  <!-- 主鍵用id,非主鍵用result -->    <id column="ID" jdbcType="INTEGER" property="id"/>    <result column="COMMAND" jdbcType="VARCHAR" property="command"/>    <result column="DESCRipTION" jdbcType="VARCHAR" property="description"/>    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>  </resultMap>  <select id="queryMessage"  resultMap="MessageResult">   select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1;  </select></mapper>

(6)創(chuàng)建真正和數(shù)據(jù)庫操作的層,db,創(chuàng)建類DBaccess

package db;import java.io.IOException;import java.io.Reader;import javax.annotation.Resource;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;/* * 專門用來訪問數(shù)據(jù)庫類 * */public class DBAccess {//dao層處理異常public SqlSession getSqlSession() throws IOException{//(1)通過配置文件獲取相關(guān)數(shù)據(jù)庫連接的信息。不建議放到src下邊,每個(gè)文件有分類去放Reader reader=Resources.getResourceAsReader("util/Configuration.xml");//(2)通過配置信息構(gòu)建SqlSessionFactorySqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);//(3)通過SqlSessionFactory打開數(shù)據(jù)庫會話SqlSession sqlSession=sqlSessionFactory.openSession();return sqlSession;//數(shù)據(jù)庫訪問層完成}}

(7)創(chuàng)建MessagqDao,通過簡單的調(diào)式得出沒有報(bào)錯(cuò)。

package dao;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.ibatis.session.SqlSession;import db.DBAccess;import bean.Message;/* * 根據(jù)查詢條件查詢 * */public class MessagqDao {public  List<Message> queryMessage(String command,String description){DBAccess dBAccess=new DBAccess();List<Message> messageList=new ArrayList<Message>();SqlSession sqlSession=null;//后邊要關(guān)掉try {sqlSession=dBAccess.getSqlSession();//sqlSession執(zhí)行SQL語句messageList =sqlSession.selectList("Message.queryMessage");//唯一的 } catch (IOException e) {e.printStackTrace();}finally{if(sqlSession!=null) sqlSession.close();}return messageList;}public static void main(String[] args ){MessagqDao messagqDao=new MessagqDao();messagqDao.queryMessage("", "");}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜阳市| 师宗县| 磐安县| 合江县| 道孚县| 论坛| 哈巴河县| 丹棱县| 门头沟区| 色达县| 本溪市| 达拉特旗| 凌海市| 米林县| 黄石市| 富顺县| 阿拉善盟| 哈尔滨市| 峨边| 崇阳县| 大港区| 舞钢市| 城市| 万年县| 美姑县| 旺苍县| 桃源县| 望谟县| 石门县| 泉州市| 南丰县| 四川省| 铁力市| 汤原县| 隆子县| 济南市| 资溪县| 滁州市| 德清县| 大余县| 淄博市|