上一篇做了一個 MyBatis 的簡單的入門, 這一節我們深入一下, 主要是學習如何對數據表做 CRUD操作.
上一篇簡單的做了一個查詢,這一篇, 咱們按照 查, 增, 改, 刪來講.
一: 查
1. 查詢一個 Person 對象.
這個簡單的查詢一個對象在上一篇中已經說了, 這裏就不說了.
2. 查詢一個 屬性值.
爲了獲取 SqlSession 對象方便, 我把獲取 SqlSession 的操作抽象成一個工具類.
public class MyBatisUtil { private MyBatisUtil(){} public static SqlSession getSqlSession() { InputStream is = MyBatisUtil.class.getClassLoader() .getResourceAsStream("conf.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder() .build(is); SqlSession session = sessionFactory.openSession(); return session; } }
SQL 語句主要都寫在 PersonMapper.xml 中, 然後調用對應的 id 屬性值.
PersonMapper.xml 中增加一個查詢
<select id="getValue" parameterType="int" resultType="String"> SELECT name FROM person WHERE id=#{id} </select>
因爲我們查詢的姓名是一個 String 類型, 所以我們的 resultType值爲String.
調用代碼爲:
@Test public void testGetValue(){ SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.getValue"; String str = session.selectOne(statement, 1); System.out.println(str); }
一定要記住 com.mybatis.entities.PersonMapper.getValue 加粗的文字, 爲<select>標籤的 id 屬性值.
是不是 So Easy!
3. 查詢一組 Person 對象.
這個就是返回一個集合, 直接上代碼, 也很簡單
PersonMapper.xml 中增加一個查詢
<select id="getPersons" parameterType="int" resultType="com.mybatis.entities.Person"> SELECT id, name, age FROM person </select>
返回值類型就是一個 Person 對象, 而查詢是查詢所有的.
調用代碼:
@Test public void testGetPersons(){ SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.getPersons"; List<Person> persons = session.selectList(statement); persons.forEach(person -> System.out.println(person)); }
List<Person> persons = session.selectList(statement);
重點是這一個, 不是 selectOne, 而是 selectList, 查詢一組對象. 沒有給它傳參數.
4. 按條件查詢或者模糊查詢一組對象.
先構造一個查詢條件類 PersonCondition 如下:
package com.mybatis.util; public class PersonCondition { private String name; private Integer minAge; private Integer maxAge; public PersonCondition() { super(); } public PersonCondition(String name, Integer minAge, Integer maxAge) { super(); this.name = name; this.minAge = minAge; this.maxAge = maxAge; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getMinAge() { return minAge; } public void setMinAge(Integer minAge) { this.minAge = minAge; } public Integer getMaxAge() { return maxAge; } public void setMaxAge(Integer maxAge) { this.maxAge = maxAge; } @Override public String toString() { return "PersonCondition [name=" + name + ", minAge=" + minAge + ", maxAge=" + maxAge + "]"; } }
然後
PersonMapper.xml 內容如下:
<select id="getPersonsByCondition" parameterType="com.mybatis.util.PersonCondition" resultType="com.mybatis.entities.Person"> <!-- 因爲裏邊有大於號小於號, 所用用 <![CDATA[]]> 括起來--> <![CDATA[ SELECT id, name, age FROM person WHERE name like #{name} AND age > #{minAge} AND age < #{maxAge} ]]> </select>
這裏直接用 #{name} 來獲取 PersonCondition 類中的name屬性值.
調用代碼如下:
@Test public void testGetPersonsByCondition(){ // 構造一個查詢條件類, name 要用 %% 括起來, mysql的語法. PersonCondition condition = new PersonCondition("%k%", 10, 20); SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.getPersonsByCondition"; // 把查詢條件傳進去 List<Person> persons = session.selectList(statement, condition); persons.forEach(person -> System.out.println(person)); }
二: 增
利用 <insert> 標籤來插入數據.
PersonMapper.xml 內容如下:
<insert id="insertPerson" parameterType="com.mybatis.entities.Person"> INSERT INTO person(name, age) VALUES(#{name}, #{age}) </insert>
調用代碼如下:
@Test public void testInsertPerson(){ SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.insertPerson"; session.insert(statement, new Person(null, "AAA", 20)); session.commit(); }
調用 session.insert(); 來完成插入.
注意: MyBatis 的 session 默認是不自動提交的.
三: 改
利用<update>標籤和 session.update()方法完成.
PersonMapper.xml 內容如下:
<update id="updatePerson" parameterType="com.mybatis.entities.Person"> UPDATE person SET name=#{name}, age=#{age} WHERE id=#{id} </update>
調用代碼如下:
@Test public void testUpdatePerson(){ SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.updatePerson"; // 我們把 id 爲 5 的人的姓名改爲 BBB, 年齡改爲 20 session.update(statement, new Person(5, "BBB", 20)); session.commit(); }
四: 刪
利用 <delete> 標籤, 和 session.delete() 方法完成操作.
PersonMapper.xml
<delete id="deletePerson" parameterType="int"> <!-- 當傳入的參數爲 int 類型的時候, 用#{id}獲取或者用#{abc} 獲取都沒問題, 當時當傳入一個對象的時候必須用 #{對象屬性}來獲取. --> DELETE FROM person WHERE id=#{abc} </delete>
調用代碼如下:
@Test public void testDeletePerson(){ SqlSession session = MyBatisUtil.getSqlSession(); String statement = "com.mybatis.entities.PersonMapper.deletePerson"; // 我們把 id 爲 5 的人從數據庫中刪除 session.delete(statement, 5); session.commit(); }
本篇博文代碼點擊 http://pan.baidu.com/s/1o7p1CyI 下載.