mybatis學習記錄------2
一 單條記錄查詢
上篇部落格的例子便是單條記錄查詢
二 多條記錄查詢
1 在對映檔案中加入如下sql查詢,${}:表示拼接sql串,將接收到的引數不加任何修飾拼接在sql中,${value}:接收輸入引數的內容,如果傳入型別是簡單型別,${}中只能使用value。
<!--根據名稱查詢記錄,可能返回多條記錄 resultType:指定就是單條記錄所對映的java物件型別 ${}:表示拼接sql串,將接收到的引數不加任何修飾拼接在sql中。 使用${}拼接sql,容易引起sql注入 ${value}:接收輸入引數的內容,如果傳入型別是簡單型別,${}中只能使用value --> <select id="findUserByName" parameterType="String" resultType="pers.czs.mybatis.po.User"> select * from user where username like '%${value}%' </select>
2 在測試程式碼中加入如下執行程式碼,由於查詢可能出現多條,因此應該使用list集合存放selectList查詢出來的多條記錄
@Test public void findUserByNameTest() throws IOException { //Mybatis配置檔案 String resource = "SqlMapConfig.xml"; //得到配置檔案流 InputStream inputStream = Resources.getResourceAsStream(resource); //建立會話工廠,傳入配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通過工廠得到SqlSession會話 SqlSession sqlSession = sqlSessionFactory.openSession(); List<User> list = sqlSession.selectList("test.findUserByName", "小明"); System.out.println(list); sqlSession.close(); }
3 執行結果
三 新增使用者(insert)
1 在對映檔案中加入相應的程式碼:其中parameterType:指定輸入引數型別是pojo,無返回值,若主鍵是自增的,則可以不寫主鍵,mysql會自動分配,我這裡主鍵是ID,是自增的,故沒寫出來
<!--新增使用者 parameterType:指定輸入引數型別是pojo #{}中指定pojo屬性名,接收到pojo物件的屬性值,mybatis通過OGNL獲取物件屬性值 若主鍵是自增的,則可以不寫主鍵,mysql會自動分配,我這裡主鍵是ID,是自增的,故沒寫出來 --> <insert id="insertUser" parameterType="pers.czs.mybatis.po.User"> insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) </insert>
2 測試程式碼 此處記得使用sqlSession進行commit
@Test public void insertUserTest() throws IOException { //Mybatis配置檔案 String resource = "SqlMapConfig.xml"; //得到配置檔案流 InputStream inputStream = Resources.getResourceAsStream(resource); //建立會話工廠,傳入配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通過工廠得到SqlSession會話 SqlSession sqlSession = sqlSessionFactory.openSession(); User user = new User(); user.setAddress("福建福州"); user.setBirthday(new Date()); user.setSex("男"); user.setUsername("零零七"); sqlSession.insert("test.insertUser", user); //提交 sqlSession.commit(); sqlSession.close(); }
3 測試結果
四 新增使用者--主鍵返回
1 自增主鍵返回
修改上述插入使用者的對映檔案程式碼,last_insert_id(),得到剛剛insert進去記錄的主鍵值,只適用自增主鍵,keyProperty:將查詢到的主鍵設定到parameterType指定的物件的特定屬性中,resultType:指定select last_insert_id()的結果型別
<insert id="insertUser" parameterType="pers.czs.mybatis.po.User"> <!--將插入資料的主鍵返回,返回到user物件中 last_insert_id():得到剛剛insert進去記錄的主鍵值,只適用自增主鍵 keyProperty:將查詢到的主鍵設定到parameterType指定的物件的特定屬性中 resultType:指定select last_insert_id()的結果型別 --> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select last_insert_id() </selectKey> insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) </insert>
2 在測試程式碼中加入輸出id的語句
@Test public void insertUserTest() throws IOException { //Mybatis配置檔案 String resource = "SqlMapConfig.xml"; //得到配置檔案流 InputStream inputStream = Resources.getResourceAsStream(resource); //建立會話工廠,傳入配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通過工廠得到SqlSession會話 SqlSession sqlSession = sqlSessionFactory.openSession(); User user = new User(); user.setAddress("福建福州"); user.setBirthday(new Date()); user.setSex("男"); user.setUsername("零零七"); sqlSession.insert("test.insertUser", user); System.out.println(user.getId()); //提交 sqlSession.commit(); sqlSession.close(); }
3 輸出結果
五 非自增主鍵返回
1 使用mysql的uuid()函式生成主鍵,需要修改表中id欄位型別為string,長度設定為35位
思路:
先通過uuid()查詢到主鍵,將主鍵輸入到sql語句中。
執行uuid()語句順序相對於insert語句之前執行
將上面的對映檔案程式碼改成如下:
<!-- 使用mysql的uuid()生成主鍵 執行過程: 首先通過uuid()得到主鍵,將主鍵設定到user物件的id屬性中 其次在執行insert時,從user物件取出id屬性值 --> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() </selectKey> insert into user (id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})