MyBatis的基本操作(02)-----Seeeion.commit引起事務的提交,多條件查詢,智能標簽的應用,ResultMap結果映射
一,為什麽session.commit()可以引起事務的提交?
進入commit()方法查看源碼
進入SqlSession.class文件之後,Ctrl+H彈出右邊的框,選擇DeFaultSqlSession
找到COMMIt()方法的重載,ctrl+左鍵進入源碼進行下一步的剖析
在最底層的commit()方法裏邊進行了事務的提交,所以SqlSession.commit()方法可以引起事務的提交
二,ResultMap的結果映射
註意:ResultMap不可以和ResultType共存
為什麽使用ResultMap?
主要解決數據表中的列名和數據庫實體類的屬性名不一致的問題
ResultMap的使用步驟:
在映射文件中,創建一個節點
這裏的property對應的是實體類的屬性名,column對應的是數據表中的列名
在select節點中,將ResultType改為ResultMap並將值改為創建好的節點的id屬性值
三,多條件查詢
1.普通的多條件查詢
//多條件查詢 public List<book> selectMultMap(Map<String,Object> map);
<!--多條件查詢--> <select id="selectMultMap" resultMap="myBook"> select* from book where bookName like ‘%‘ #{bookName} ‘%‘ and bookprice>#{bookprice} </select>
@Test public void test4(){ SqlSession sqlSession= MyBatisUtil.getSession(); IBookDao mapper = sqlSession.getMapper(IBookDao.class); Map<String,Object> map=new HashMap<String,Object>(); map.put("bookName","大"); map.put("bookprice",100); List<book> books = mapper.selectMultMap(map); for(book item:books){ System.out.println(item.getBookName()); } sqlSession.close(); }
2.多條件索引查詢
//多條件索引查詢 public List<book> selectMultIndex(String bookname,Integer bookprice);
<!--多條件索引查詢--> <select id="selectMultIndex" resultMap="myBook"> select * from book where bookName like ‘%‘ #{0} ‘%‘ and bookprice>#{1} </select>
@Test public void test5(){ SqlSession sqlSession= MyBatisUtil.getSession(); IBookDao mapper = sqlSession.getMapper(IBookDao.class); List<book> list = mapper.selectMultIndex("大", 300); for(book item:list){ System.out.println(item.getBookName()); } sqlSession.close(); }e[‘
四,添加後返回自增列的值
當我們需要拿到新添加的列的主鍵Id的時候:
在<insert>節點中,在書寫一個<selectKey>節點
!!!需要註意的是:由於主鍵Id是自增的,在Sql語句中我們是不用添加的,所以這裏聲明的keyProperty的作用就是將其賦給Id
五,工具類、
將一些重復性很大的代碼放在一個類中,調用時直接通過類的方法來調用,減少代碼的書寫量
例如:將這幾行代封裝在一個類,以後需要用到時就不需要在進行書寫,直接調用
String path="mybatis-config.xml"; InputStream is= Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession =factory.openSession();
public class MyBatisUtil { static String path="mybatis-config.xml"; static InputStream is; static SqlSessionFactory factory; static { try { is= Resources.getResourceAsStream(path); factory=new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ return factory.openSession(true); } }
六,智能標簽
智能標簽分為:
if where foreaachArray[數組類型] foreachList<包裝類型Integer> foreachList<自定義類型>
//智能標簽if查詢 public List<book> selectIf(book bok); //foreach array public List<book> foreachArray(int[] ids); //foreach List public List<book> foreachArrayList(List<Integer> list); //foreach 自定義類型 public List<book> foreachArrayBook(List<book> list);
if where
<select id="selectIf" resultMap="myBook"> SELECT * from book <where> <if test="bookName!=null"> and bookName like ‘%‘ #{bookName} ‘%‘ </if> <if test="bookprice!=null"> and bookprice>#{bookprice} </if> </where> </select>
foreaachArray[數組類型] foreachList<包裝類型Integer> foreachList<自定義類型>
<!--數組--> <select id="foreachArray" resultMap="myBook"> SELECT * from book <where> bookId IN <foreach collection="array" open="(" close=")" separator="," item="myid"> #{myid} </foreach> </where> </select> <!--List集合--> <select id="foreachArrayList" resultMap="myBook"> SELECT * from book <where> bookId IN <foreach collection="list" open="(" close=")" separator="," item="myid"> #{myid} </foreach> </where> </select> <!--自定義類型的List集合--> <select id="foreachArrayBook" resultMap="myBook"> SELECT * from book <where> bookId IN <foreach collection="list" open="(" close=")" separator="," item="books"> #{books.bookId} </foreach> </where> </select>
MyBatis的基本操作(02)-----Seeeion.commit引起事務的提交,多條件查詢,智能標簽的應用,ResultMap結果映射