首頁 > 軟體

關於Mybatis的sql註釋問題

2022-06-12 14:01:11

Mybatis的sql註釋

//mapper下的sql註釋
package com.msb.mapper;
import com.msb.pojo.Dept;
import com.msb.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.awt.*;
public interface EmpTestMapper {
    @Select("select * from dept where deptno =#{deptno}")
    Dept findByDeptno(int deptno);
    @Update("update dept set dname =#{dname}, loc =#{loc} where deptno =#{deptno}")
    int updateDept(Dept dept);
    @Insert("insert into dept values(DEFAULT,#{dname},#{loc})")
    int addDept(Dept dept);
    @Delete("delete from dept where deptno =#{deptno}")
    int removeDept(int deptno);
}
<?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.msb.mapper.EmpTestMapper">
    <!--resultMap是一個對映表,對應的欄位會對應資料表的欄位-->
    <!-- List<Emp> findEmp();-->
</mapper>
import com.msb.mapper.DeptMapper;
import com.msb.mapper.EmpMapper;
import com.msb.mapper.EmpTestMapper;
import com.msb.pojo.Dept;
import com.msb.pojo.Emp;
import com.msb.pojo.Projectrecord;
import com.msb.pojo.Projects;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class Test1 {
    SqlSession sqlSession = null;
    EmpMapper mapper =null;
   @Before
   public void test1(){
       //首先做一個物件SqlSessionFactoryBuilder建立一個繪話
       SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
       //有一個文字輸入的io流進行讀取操作
       InputStream stream = null;
       try {
           //這裡的路徑直接會定位到組態檔classes下面;所以這個檔案在次目錄下--編譯和
           //-圖紙;對資料庫檔案進行讀取,獲取一個io流,由於組態檔在classes下面,直接寫檔名即可
           stream = Resources.getResourceAsStream("sqlMapConfig.xml");
       } catch (IOException e) {
           e.printStackTrace();
       }
       //build需要指向一個檔案進行讀取出來--工廠
       SqlSessionFactory factory = ssfb.build(stream);
        //需要用sqlSession去呼叫增刪改查--工人去獲取資料,開啟這個繪話
       sqlSession = factory.openSession(true);
   }

    @Test
    public void test4(){
        EmpTestMapper mapper = sqlSession.getMapper(EmpTestMapper.class);
        Dept emp = mapper.findByDeptno(20);
        Dept dept = new Dept();
        dept.setDeptno(null);
        dept.setLoc("大格");
        dept.setDname("水軍");
        int addDept = mapper.addDept(dept);
        System.out.println(addDept);
        System.out.println(emp);
    }

    @After
    public void test3(){
       if (sqlSession!=null){
           sqlSession.close();
       }
    }
}

到此這篇關於關於Mybatis的sql註釋問題的文章就介紹到這了,更多相關Mybatis sql註釋內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com