您的位置:首页技术文章

关于Mybatis的sql注释问题

【字号: 日期:2023-02-22 14:52:19浏览:10作者:猪猪

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注释内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

标签: MsSQL