關於Mybatis的sql註釋問題

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註釋內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: