步驟1:先運行,看到效果,再學習
步驟2:模仿和排錯
步驟3:在前一步的基礎上進行
步驟4:配置文件Category.xml
步驟5:增長
步驟6:刪除
步驟7:獲取
步驟8:修改
步驟9:查詢全部 html
步驟 1 : 先運行,看到效果,再學習java
老規矩,先下載下載區(點擊進入)的可運行項目,配置運行起來,確承認用以後,再學習作了哪些步驟以達到這樣的效果。sql
步驟 2 : 模仿和排錯apache
在確保可運行項目可以正確無誤地運行以後,再嚴格照着教程的步驟,對代碼模仿一遍。
模仿過程不免代碼有出入,致使沒法獲得指望的運行結果,此時此刻經過比較正確答案 ( 可運行項目 ) 和本身的代碼,來定位問題所在。
採用這種方式,學習有效果,排錯有效率,能夠較爲明顯地提高學習速度,跨過學習路上的各個檻。 session
推薦使用diffmerge軟件,進行文件夾比較。把你本身作的項目文件夾,和個人可運行項目文件夾進行比較。
這個軟件很牛逼的,能夠知道文件夾裏哪兩個文件不對,而且很明顯地標記出來
這裏提供了綠色安裝和使用教程:diffmerge 下載和使用教程mybatis
步驟 3 : 在前一步的基礎上進行app
這一個知識點講解經過Mybatis進行CRUD一套操做,創建在上一個知識點Mybatis 入門教程的基礎上進行。學習
步驟 4 : 配置文件Category.xmlui
首先一次性修改配置文件Category.xml,提供CRUD對應的sql語句。
每一個SQL如何使用在後續對應操做裏一一講解。spa
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.how2java.pojo"> <insert id="addCategory" parameterType="Category"> insert into category_ ( name ) values (#{name}) </insert> <delete id="deleteCategory" parameterType="Category"> delete from category_ where id= #{id} </delete> <select id="getCategory" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="updateCategory" parameterType="Category"> update category_ set name=#{name} where id=#{id} </update> <select id="listCategory" resultType="Category"> select * from category_ </select> </mapper>
步驟 5 : 增長
運行以後,如圖所示能夠看到 經過Mybatis查詢出來的數據裏有新增長的Category
經過session.insert調用addCategory對應的SQL語句
Category c = new Category(); c.setName("新增長的Category"); session.insert("addCategory", c);
addCategory對應的插入sql語句,#{name}會自動獲取c對象的name屬性值
<insert id="addCategory" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Category c = new Category(); c.setName("新增長的Category"); session.insert("addCategory", c); listAll(session); session.commit(); session.close(); } private static void listAll(SqlSession session) { List<Category> cs = session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); } } } <insert id="addCategory" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert>
步驟 6 : 刪除
刪除id=6的對象
Category c = new Category(); c.setId(6); session.delete("deleteCategory",c);
deleteCategory對應刪除的sql語句
<delete id="deleteCategory" parameterType="Category" > delete from category_ where id= #{id} </delete>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Category c = new Category(); c.setId(6); session.delete("deleteCategory", c); listAll(session); session.commit(); session.close(); } private static void listAll(SqlSession session) { List<Category> cs = session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); } } } <delete id="deleteCategory" parameterType="Category" > delete from category_ where id= #{id} </delete>
步驟 7 : 獲取
經過session.selectOne獲取id=3的記錄
Category c= session.selectOne("getCategory",3);
getCategory對應的sql語句:
<select id="getCategory" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Category c = session.selectOne("getCategory", 3); System.out.println(c.getName()); // listAll(session); session.commit(); session.close(); } private static void listAll(SqlSession session) { List<Category> cs = session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); } } } <select id="getCategory" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select>
步驟 8 : 修改
經過session.update進行修改
session.update("updateCategory",c);
updateCategory對應的sql語句:
<update id="updateCategory" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Category c = session.selectOne("getCategory", 3); c.setName("修改了的Category名稱"); session.update("updateCategory", c); listAll(session); session.commit(); session.close(); } private static void listAll(SqlSession session) { List<Category> cs = session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); } } } <update id="updateCategory" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update>
步驟 9 : 查詢全部
session.selectList執行查詢語句
List<Category> cs = session.selectList("listCategory");
listCategory對應的sql語句
<select id="listCategory" resultType="Category"> select * from category_ </select>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); listAll(session); session.commit(); session.close(); } private static void listAll(SqlSession session) { List<Category> cs = session.selectList("listCategory"); for (Category c : cs) { System.out.println(c.getName()); } } } <select id="listCategory" resultType="Category"> select * from category_ </select>
更多內容,點擊瞭解: https://how2j.cn/k/mybatis/mybatis-crud/1088.html