升級版大數(shù)據(jù)教程配套03三大框架my教案day_第1頁
升級版大數(shù)據(jù)教程配套03三大框架my教案day_第2頁
升級版大數(shù)據(jù)教程配套03三大框架my教案day_第3頁
升級版大數(shù)據(jù)教程配套03三大框架my教案day_第4頁
升級版大數(shù)據(jù)教程配套03三大框架my教案day_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Mybatis 第一天框架課程1 課程計劃JAVAEE 開發(fā)的三劍客:三大框架Mybatis:封裝 jdbc代碼的一個框架(hibernate)ORMSpring MVC:用來封裝 servlet 編程的一個框架(struts2)Spring:體系整合框架,其他框架的粘合劑;什么是框架:框架(Framework)是整個或部分系統(tǒng)的可重用設計,表現(xiàn)為一組抽象構件及構件實例間交互的方法; 另一種定義認為,框架是可被應用開發(fā)者定制的應用骨架。是用框架可以提高程序復用性和系統(tǒng)的可擴充性,以縮短大型應用軟件系統(tǒng)的開發(fā)周期,提高開發(fā)質量??蚣艿膬?yōu)點(總結)1.靈活可配置:將程序中寫死的代碼(硬編碼)可以寫

2、到配置文件中2.代碼復用性高:將程序中反復要寫的代碼(套路代碼)進行抽取封裝,提高代碼的復用性3.簡化開發(fā):框架將底層復雜的細節(jié)進行了封裝,并提供了方便調用的 API,使得開發(fā)將工作的重點轉移到實際業(yè)務中,大大提高了項目的開發(fā)效率第一天:1、mybatis 的介紹2、Mybatis 的入門a)使用 jdbc 操作數(shù)據(jù)庫存在的問題b)Mybatis 的架構c)Mybatis 的入門程序3、Dao 的開發(fā)方法a)原始 dao 的開發(fā)方法b)動態(tài)方式4、SqlMapConfig.xml 文件說明第二天:1、輸入和輸出a)輸入?yún)?shù)b)返回值2、動態(tài) sql3、關聯(lián)a)一對一關聯(lián)b)一對多關聯(lián)2 MyB

3、atis 介紹MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache softwarefoundation 遷移到了code,并且改名為MyBatis 。2013年11月遷移到。MyBatis是一個優(yōu)秀的持久層框架,它對jdbc的操作數(shù)據(jù)庫的過程進行封裝,使開發(fā)者只需要關注 SQL 本身,而不需要花費精力去處理例如驅動、創(chuàng)建connection、創(chuàng)建statement、手動設置參數(shù)、結果集檢索等jdbc繁雜的過程代碼。Mybatis通過xml或注解的方式將要執(zhí)行的各種statement(statement、preparedStatemnt、Callabl

4、eStatement)配置起來,并通過java對象和statement中的sql進行生成最終執(zhí)行的sql語句,最后由mybatis框架執(zhí)行sql并將結果返回。成java對象并3 使用 jdbc 編程問題總結3.1 創(chuàng)建 mysql 數(shù)據(jù)庫先導入創(chuàng)建數(shù)據(jù)庫的 sql導入到數(shù)據(jù)庫中。3.2 創(chuàng)建工程開發(fā)環(huán)境:eclipse marsJdk:1.71、創(chuàng)建一個 java 工程。2、導入 jar 包。此時需要 mysql 的數(shù)據(jù)庫驅動。3.3 jdbc 編程步驟:1、 加載數(shù)據(jù)庫驅動2、 創(chuàng)建并獲取數(shù)據(jù)庫3、 創(chuàng)建 jdbc statement 對象4、 設置 sql 語句5、 設置 sql 語句中的

5、參數(shù)(使用 preparedStatement)6、 通過 statement 執(zhí)行 sql 并獲取結果7、 對 sql 執(zhí)行結果進行處理8、(resultSet、preparedstatement、connection)3.4 jdbc 程序public static void main(String args) Connection connection = null;PreparedStatement preparedStatement = null; ResultSet resultSet = null;try /加載數(shù)據(jù)庫驅動Class.forName("com.mysql

6、.jdbc.Driver");/通過驅動管理類獲取數(shù)據(jù)庫connection =DriverManager.getConnection("jdbc:mysql:/localhost:3306/mybatis?charac terEncoding=utf-8", "root", "root");/定義sql語句 ?表示占位符String sql = "select * from user where username = ?"/獲取預處理statementpreparedStatement = connec

7、tion.prepareStatement(sql);/設置參數(shù),第一個參數(shù)為sql語句中參數(shù)的序號(從1開始),第二個參數(shù)為設置的參數(shù)值preparedStatement.setString(1, "王五");/向數(shù)據(jù)庫發(fā)出sql執(zhí)行,出結果集resultSet = preparedStatement.executeQuery();/遍歷結果集while(resultSet.next()System.out.println(resultSet.getString("id")+""+resultSet.getString("

8、username"); catch (Exception e) e.printStackTrace();finally/if(resultSet!=null)try resultSet.close(); catch (SQLException e) / TODO Auto-generated catchblocke.printStackTrace();if(preparedStatement!=null)try preparedStatement.close(); catch (SQLException e) / TODO Auto-generated catchblocke.pri

9、ntStackTrace();if(connection!=null)try connection.close(); catch (SQLException e) / TODO Auto-generated catchblock上邊使用 jdbc 的原始方法(封裝)實現(xiàn)了數(shù)據(jù)庫表的操作。3.5 jdbc 問題總結如下:1、 數(shù)據(jù)庫創(chuàng)建、頻繁造成系統(tǒng)浪費從而影響系統(tǒng)性能,如果使用數(shù)據(jù)庫池可解決此問題。2、 Sql 語句在代碼中硬編碼,造成代碼不易維護,實際應用 sql 變化的可能較大,sql 變動需要改變 java 代碼。3、 使用 preparedStatement 向占有位符號傳參數(shù)存在硬編

10、碼,因為 sql 語句的 where 條件不一定,可能多也可能少,修改 sql 還要修改代碼,系統(tǒng)不易維護。4、 對結果集存在硬編碼(列名),sql 變化導致代碼變化,系統(tǒng)不易維護,如果能將數(shù)據(jù)庫封裝成 pojo 對象比較方便。e.printStackTrace();4 Mybatis 架構MyBatis配置文件數(shù)據(jù)庫1、 mybatis 配置SqlMapConfig.xml,此文件作為 mybatis 的全局配置文件,配置了 mybatis 的運行環(huán)境等信息。mapper.xml 文件即 sqlSqlMapConfig.xml 中加載。文件,文件中配置了操作數(shù)據(jù)庫的 sql 語句。此文件需要

11、在2、 通過 mybatis 環(huán)境等配置信息構造 SqlSessionFactory 即會話工廠(單例)3、 由會話工廠創(chuàng)建 sqlSession 即會話,操作數(shù)據(jù)庫需要通過 sqlSession 進行。4、 mybatis 底層自定義了 Executor 執(zhí)行器接口操作數(shù)據(jù)庫,Executor 接口有兩個實現(xiàn),一個是基本執(zhí)行器、一個是緩存執(zhí)行器。5、 Mapped Statement 也是 mybatis 一個底層封裝對象,它包裝了 mybatis 配置信息及 sql信息等。mapper.xml 文件中一個 sql 對應一個 Mapped Statement 對象,sql 的 id 即是 M

12、apped statement 的 id。6、 Mapped Statement 對sql 執(zhí)行輸入?yún)?shù)進行定義,包括HashMap、基本類型、pojo,Executor通過 Mapped Statement 在執(zhí)行 sql 前將輸入的 java 對象是 jdbc 編程中對 preparedStatement 設置參數(shù)。至 sql 中,輸入?yún)?shù)就MappedStatement輸出ListPojoString、Integer等基礎數(shù)據(jù)類型MapExecutor輸入PojoString、Integer等基礎數(shù)據(jù)類型MapSqlSessionSqlSessionFactoryMappern.xmlM

13、apper2.xmlMapper1.xmlSqlMapConfig xml7、 Mapped Statement 對sql 執(zhí)行輸出結果進行定義,包括HashMap、基本類型、pojo,Executor通過 Mapped Statement 在執(zhí)行 sql 后將輸出結果至 java 對象中,輸出結果過程相當于 jdbc 編程中對結果的處理過程。5 Mybatis 入門程序5.1 mybatismybaits 的代碼由.com 管理,地址:mybatis-3.2.7.jarmybatis 的包libmybatis 的依賴包mybatis-3.2.7.pdfmybatis 使用手冊5.2 需求實現(xiàn)

14、以下功能:根據(jù)用戶 id一個用戶信息根據(jù)用戶名稱模糊用戶信息列表添加用戶更新用戶刪除用戶5.3 工程搭建5.3.1 第一步:創(chuàng)建 java 工程使用 eclipse 創(chuàng)建 java 工程,jdk 使用 1.7.0_72。5.3.2 第二步:加入 jar 包加入 mybatis包、依賴包、數(shù)據(jù)驅動包。5.3.3 第三步:perties<可選>加 log4j 的配置文件是為了調試觀察方便在工程的 classpath 下創(chuàng)建 perties 如下:# Global logging configurationlog4j.rootLogger=DEBUG,

15、 stdout # Console output.mybatis 默認使用 log4j 作為輸出日志信息。5.3.4 第四步:SqlMapConfig.xml在 classpath 下創(chuàng)建 SqlMapConfig.xml,如下:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//DTD Config 3.0/EN"""><configuration><!-

16、和spring整合后 environments配置將-><environments default="development"><environment id="development"><!- 使用jdbc事務管理-><transactionManager type="JDBC" /><!- 數(shù)據(jù)庫連接池-><dataSource type="POOLED"><property name="driver" va

17、lue="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql:/localhost:3306/mybatis?characterEncoding=u tf-8" /><property name="username" value="root" /><property name="password" value="root" />log4j.

18、appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p %t - %m%nSqlMapConfig.xml 是 mybatis配置文件,上邊文件的配置內容為數(shù)據(jù)源、事務管理。補充: DTD 約束的本地文件加載配置1、在 eclipse 中點擊2、3、添加 dtd 約束文件</dataSource></environme

19、nt></environments></configuration>5.3.5 第五步:po 類Po 類作為 mybatis 進行 sql使用,po 類通常與數(shù)據(jù)庫表對應,User.java 如下:Public class User private int id;private String username;/ 用戶private String sex;/5.3.6 第六步:sql文件在 classpath 下的 sqlmap 目錄下創(chuàng)建 sql文件 User.xml:namespace :命名空間,用于sql 語句,后面會講另一層非常重要的作用。5.3.7 第

20、七步:在配置文件中加載 sql文件mybatis 框架需要加載文件,將 User.xml 添加在 SqlMapConfig.xml,如下:<mappers><mapper resource="sqlmap/User.xml"/></mappers><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//DTD Mapper 3.0/EN"""&g

21、t;<mapper namespace="test"></mapper>private Date birthday;/ 生日private String address;/ 地址get/set5.4 根據(jù) id用戶信息文件:5.4.1在 user.xml 中添加:parameterType:定義輸入到 sql 中的置占位符號并將輸入變量 id 傳到 sql。類型,#id表示使用 preparedstatement 設resultType:定義結果類型。5.4.2 測試程序:public class TestMybatis <!- 根據(jù)id獲取用

22、戶信息 -><select id="findUserById" parameterType="int" resultType="360.mybatis.po.User">select * from user where id = #id</select>/會話工廠private SqlSessionFactory sqlSessionFactory;Beforepublic void createSqlSessionFactory() throws IOException / 配置文件Stri

23、ng resource = "SqlMapConfig.xml"InputStream inputStream = Resources.getResourceAsStream(resource);/ 使用SqlSessionFactoryBuilder從xml配置文件中創(chuàng)建SqlSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);/ 根據(jù) id用戶信息Testpublic void testFindUserById() / 數(shù)據(jù)庫會話實例SqlSession

24、sqlSession = null;try / 創(chuàng)建數(shù)據(jù)庫會話實例sqlSessionsqlSession = sqlSessionFactory.openSession();單個,根據(jù)用戶id用戶信息/User user = sqlSession.selectOne("test.findUserById",10);/ 輸出用戶信息5.5 根據(jù)用戶名用戶信息文件:5.5.1在 user.xml 中添加:parameterType:定義輸入到 sql 中的做字符串的拼接。類型,$value表示使用參數(shù)將$value替換,注意:如果是取簡單數(shù)量類型的參數(shù),括號中的值必須為 va

25、lueresultType:定義結果類型。<!- 自定義條件用戶列表 -><select id="findUserByUsername" parameterType="java.lang.String" resultType="360.mybatis.po.User">select * from user where username like '%$value%'</select>System.out.println(user); catch (Exception e)

26、 e.printStackTrace(); finally if (sqlSession != null) sqlSession.close();5.5.2 測試程序:5.6 小結5.6.1 #和$#表示一個占位符號,通過#可以實現(xiàn) preparedStatement 向占位符中設置值,自動進行 java/ 根據(jù)用戶名稱模糊用戶信息Testpublic void testFindUserByUsername() / 數(shù)據(jù)庫會話實例SqlSession sqlSession = null; try / 創(chuàng)建數(shù)據(jù)庫會話實例sqlSessionsqlSession = sqlSessionFacto

27、ry.openSession();/單個,根據(jù)用戶id用戶信息List<User> list = sqlSession.selectList("test.findUserByUsername", "張");System.out.println(list.size(); catch (Exception e) e.printStackTrace(); finally if (sqlSession != null) sqlSession.close();類型和 jdbc 類型轉換,#可以有效防止 sql 注入。 #可以接收簡單類型值或 pojo

28、屬性值。如果 parameterType 傳輸單個簡單類型值,#括號中可以是 value 或其它名稱。$表示拼接 sql 串,通過$可以將 parameterType 傳入的內容拼接在 sql 中且不進行 jdbc 類型轉換, $可以接收簡單類型值或 pojo 屬性值,如果 parameterType 傳輸單個簡單類型值,$括號中只能是 value。5.6.2 parameterType 和 resultTypeparameterType:指定輸入?yún)?shù)類型,mybatis 通過 ognl 從輸入對象中獲取參數(shù)值拼接在 sql中。resultType:指定輸出結果類型,mybatis 將 sql

29、型的對象。結果的一行為 resultType 指定類5.6.3 selectOne 和 selectListselectOne一條,如果使用 selectOne多條則拋出異常:org.apache.ibatis.exceptions.TooManyResultsException: Expected one result(or null) to be returned by selectOne(), but found: 3atorg.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultS qlSession.ja

30、va:70)selectList 可以一條或多條。5.7 添加用戶文件:5.7.1在 SqlMapConfig.xml 中添加:5.7.2 測試程序:/ 添加用戶信息Testpublic void testInsert() / 數(shù)據(jù)庫會話實例SqlSession sqlSession = null; try / 創(chuàng)建數(shù)據(jù)庫會話實例sqlSessionsqlSession = sqlSessionFactory.openSession();/ 添加用戶信息User user = new User(); user.setUsername("張小明"); user.setAddr

31、ess("河南鄭州"); user.setSex("1"); user.setPrice(1999.9f);sqlSession.insert("test.insertUser", user);<!- 添加用戶 -><insert id="insertUser" parameterType="360.mybatis.po.User"> insert into user(username,birthday,sex,address) values(#usernam

32、e,#birthday,#sex,#address)</insert>5.7.3 mysql 自增主鍵返回通過修改 sql文件,可以將 mysql 自增主鍵返回:添加 selectKey 實現(xiàn)將主鍵返回keyProperty:返回的主鍵在 pojo 中的哪個屬性order:selectKey 的執(zhí)行順序,是相對與 insert 語句來說,由于 mysql 的自增原理執(zhí)行完 insert 語句之后才將主鍵生成,所以這里 selectKey 的執(zhí)行順序為 afterresultType:返回的主鍵是什么類型LAST_INSERT_ID():是 mysql 的函數(shù),返回 auto_inc

33、rement 自增列新id 值。<insert id="insertUser" parameterType="360.mybatis.po.User"><!- selectKey將主鍵返回,需要再返回 -><selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID()</selectKey>insert i

34、nto user(username,birthday,sex,address) values(#username,#birthday,#sex,#address);</insert>/提交事務sqlSmit(); catch (Exception e) e.printStackTrace(); finally if (sqlSession != null) sqlSession.close();5.7.4 Mysql 使用 uuid 實現(xiàn)主鍵需要增加通過 select uuid()得到 uuid 值<insertid="insertUser" parame

35、terType="360.mybatis.po.User"><selectKey resultType="java.lang.String" order="BEFORE"keyProperty="id">select uuid()</selectKey>insert into user(id,username,birthday,sex,address)values(#id,#username,#birthday,#sex,#address)</insert>注意

36、這里使用的 order 是“BEFORE”5.8 刪除用戶文件:5.8.1<!- 刪除用戶 -><delete id="deleteUserById" parameterType="int"> delete from user where id=#id</delete>5.8.2 測試程序:5.9 修改用戶文件5.9.1/ 根據(jù)id刪除用戶Testpublic void testDelete() / 數(shù)據(jù)庫會話實例SqlSession sqlSession = null; try / 創(chuàng)建數(shù)據(jù)庫會話實例sqlSessi

37、onsqlSession = sqlSessionFactory.openSession();/ 刪除用戶sqlSession.delete("test.deleteUserById",18);/ 提交事務sqlSmit(); catch (Exception e) e.printStackTrace(); finally if (sqlSession != null) sqlSession.close(); </update>5.9.2 測試程序/ 更新用戶信息Testpublic void testUpdate() / 數(shù)據(jù)庫會話實例SqlSession s

38、qlSession = null; try / 創(chuàng)建數(shù)據(jù)庫會話實例sqlSessionsqlSession = sqlSessionFactory.openSession();/ 添加用戶信息User user = new User(); user.setId(16); user.setUsername("張小明"); user.setAddress("河南鄭州"); user.setSex("1");sqlSession.update("test.updateUser", user);/ 提交事務sqlSmit(

39、);<!- 更新用戶 -><update id="updateUser" parameterType="360.mybatis.po.User"> update user setusername=#username,birthday=#birthday,sex=#sex,address=#addresswhere id=#id5.10 Mybatis 解決 jdbc 編程的問題1、 數(shù)據(jù)庫創(chuàng)建、頻繁造成系統(tǒng)浪費從而影響系統(tǒng)性能,如果使用數(shù)據(jù)庫池可解決此問題。解決:在 SqlMapConfig.xml 中配置數(shù)據(jù)池,使用

40、連接池管理數(shù)據(jù)庫。2、 Sql 語句寫在代碼中造成代碼不易維護,實際應用 sql 變化的可能較大,sql 變動需要改變 java 代碼。解決:將 Sql 語句配置在Xmapper.xml 文件中與 java 代碼分離。3、 向 sql 語句傳參數(shù)麻煩,因為 sql 語句的 where 條件不一定,可能多也可能少,占位符需要和參數(shù)一一對應。解決:Mybatis 自動將 java 對象至 sql 語句,通過 statement 中的 parameterType 定義輸入?yún)?shù)的類型。4、 對結果集麻煩,sql 變化導致代碼變化,且前需要遍歷,如果能將數(shù)據(jù)庫封裝成 pojo 對象比較方便。解決:Myb

41、atis 自動將 sql 執(zhí)行結果義輸出結果的類型。至 java 對象,通過 statement 中的 resultType 定 catch (Exception e) e.printStackTrace(); finally if (sqlSession != null) sqlSession.close();6 Dao 開發(fā)方法使用 Mybatis 開發(fā) Dao,通常有兩個方法,即原始 Dao 開發(fā)方法和 Mapper 接口開發(fā)方法。6.1 需求將下邊的功能實現(xiàn) Dao:根據(jù)用戶 id一個用戶信息根據(jù)用戶名稱模糊用戶信息列表添加用戶信息6.2 SqlSession 的使用范圍SqlSess

42、ion 中封裝了對數(shù)據(jù)庫的操作,如:、更新、刪除等。通過 SqlSessionFactory 創(chuàng)建 SqlSession,而 SqlSessionFactory 是通過 SqlSessionFactoryBuilder進行創(chuàng)建。6.2.1 SqlSessionFactoryBuilderSqlSessionFactoryBuilder 用于創(chuàng)建 SqlSessionFacoty,SqlSessionFacoty 一旦創(chuàng)建完成就不需要 SqlSessionFactoryBuilder 了,因為 SqlSession 是通過 SqlSessionFactory 生產(chǎn),所以可以將 SqlSessio

43、nFactoryBuilder 當成一個工具類使用,最佳使用范圍是方法范圍即方法體內局部變量。6.2.2 SqlSessionFactorySqlSessionFactory 是一個接口,接口中定義了 openSession 的不同重載方法,SqlSessionFactory 的最佳使用范圍是整個應用運行期間,一旦創(chuàng)建后可以重復使用,通常以單例模式管理 SqlSessionFactory。6.2.3 SqlSessionSqlSession 是一個面向用戶的接口, sqlSession 中定義了數(shù)據(jù)庫操作方法。每個線程都應該有它自己的 SqlSession 實例。SqlSession 的實例不

44、能共享使用,它也是線程不安全的。因此最佳的范圍是請求或方法范圍。絕對不能將 SqlSession 實例的一個類的靜態(tài)字段或實例字段中。放在打開一個 SqlSession;使用完畢就要關閉它。通常把這個關閉操作放到確保每次都能執(zhí)行關閉。如下:塊中以finally6.3 原始 Dao 開發(fā)方式原始 Dao 開發(fā)方法需要程序員編寫 Dao 接口和 Dao 實現(xiàn)類。文件6.3.1<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//DT

45、D Mapper 3.0/EN"""><mapper namespace="test"><!- 根據(jù)id獲取用戶信息 -><select id="findUserById" parameterType="int"SqlSession session = sqlSessionFactory.openSession(); try / do work finally session.close();6.3.2 Dao 接口Public class UserDaoImpl im

46、plements UserDao private SqlSessionFactory sqlSessionFactory;/注入SqlSessionFactorypublic UserDaoImpl(SqlSessionFactory sqlSessionFactory)this.setSqlSessionFactory(sqlSessionFactory);Public interface UserDao public User getUserById(int id) throws Exception;public void insertUser(User user) throws Exce

47、ption;resultType="360.mybatis.po.User"> select * from user where id = #id</select><!- 添加用戶 -><insert id="insertUser" parameterType="360.mybatis.po.User"><selectKey keyProperty="id" order="AFTER" resultType="ja

48、va.lang.Integer">select LAST_INSERT_ID()</selectKey>insert into user(username,birthday,sex,address) values(#username,#birthday,#sex,#address)</insert></mapper>Overridepublic User getUserById(int id) throws Exception SqlSession session = sqlSessionFactory.openSession();User

49、user = null;try /通過sqlsession調用selectOne方法獲取一條結果集/參數(shù)1:指定定義的statement的id,參數(shù)2:指定向statement中傳遞的參數(shù)user = session.selectOne("test.findUserById", 1);System.out.println(user); finallysession.close();return user;OverridePublic void insertUser(User user) throws Exception SqlSession sqlSession = sql

50、SessionFactory.openSession();try sqlSession.insert("insertUser", user);sqlSmit(); finallysession.close();6.3.3 Dao 測試創(chuàng)建一個 JUnit 的測試類,對 UserDao 進試。6.3.4 問題原始 Dao 開發(fā)中存在以下問題:u Dao 方法體存在重復代碼:通過 SqlSessionFactory 創(chuàng)建 SqlSession,調用 SqlSession 的數(shù)private SqlSessionFactory sqlSessionFactory;Beforepu

51、blic void init() throws Exception SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder(); InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = sessionFactoryBuilder.build(inputStream);Testpublic void testGetUserById() Use

52、rDao userDao = new UserDaoImpl(sqlSessionFactory); User user = userDao.getUserById(22); System.out.println(user);據(jù)庫操作方法u 調用 sqlSession 的數(shù)據(jù)庫操作方法需要指定 statement 的 id,這里存在硬編碼,不得于開發(fā)維護。6.4 Mapper 動態(tài)方式6.4.1 開發(fā)規(guī)范Mapper 接口開發(fā)方法只需要程序員編寫 Mapper 接口(相當于 Dao 接口),由 Mybatis框架根據(jù)接口定義創(chuàng)建接口的動態(tài)對象,法。對象的方法體同上邊 Dao 接口實現(xiàn)類方Map

53、per 接口開發(fā)需要遵循以下規(guī)范:1、 Mapper.xml 文件中的 namespace 與 mapper 接口的類路徑相同。2、 Mapper 接口方法名和 Mapper.xml 中定義的每個 sql 的 id 相同3、 Mapper 接口方法的輸入?yún)?shù)類型和 mapper.xml 中定義的每個 sql 的 parameterType 的類型相同4、 Mapper 接口方法的輸出參數(shù)類型和mapper.xml 中定義的每個 sql 的resultType 的類型相同文件)6.4.2 Mapper.xml(定義 mapper文件 UserMapper.xml(內容同 Users.xml),需

54、要修改 namespace 的值為 UserMapper 接口路徑。將 UserMapper.xml 放在 classpath 下 mapper 目錄 下。<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//DTD Mapper 3.0/EN"""><mapper namespace="360.mybatis.mapper.UserMapper">

55、<!- 根據(jù)id獲取用戶信息 -><select id="findUserById" parameterType="int" resultType="360.mybatis.po.User">6.4.3 Mapper.java(接口文件)/* 用戶管理mapper*/Public interface UserMapper /根據(jù)用戶id用戶信息public User findUserById(int id) throws Exception;select * from user where id =

56、 #id</select><!- 自定義條件用戶列表 -><select id="findUserByUsername" parameterType="java.lang.String" resultType="360.mybatis.po.User">select * from user where username like '%$value%'</select><!- 添加用戶 -><insert id="insertUser" parameterType="360.mybatis.po.User"><selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">select LAST_INSERT_ID()</selectKey>insert into user(username,birthday,sex,address) values(#username,#birthday

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論