mybatis使用注解的方式执行sql语句

mybatis在不使用注解时 SQL语句都是写在.xml配置文件中

mybatis使用注解的方式执行sql语句_第1张图片

<?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.gx.mapper.EmployeeMapper">
    //简单的查询语句
    <select id="findAll" resultType="emp">
      /*标准sql语句*/
      select * from emp 
    </select>
  
</mapper>

使用注解模式时SQL语句直接在接口中的方法上的注解即可
.xml配置文件注意namspace即可,当然也可以不使用改配置文件,但是那就就在每个注解方法中都要加上一句话稍微费事点,个人建议还是使用namespace属性比较方便

mybatis使用注解的方式执行sql语句_第2张图片

接口文件:

mybatis使用注解的方式执行sql语句_第3张图片
mybatis使用注解的方式执行sql语句_第4张图片

package com.gx.mapper;

import com.gx.vo.Dept;
import com.gx.vo.Employee;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface EmployeeMapper {
    //Dao中定义的抽象方法要映射文件中的id值匹配起来,映射中的的参数就是此处方法的参数
    @Select("select * from emp")
    public List<Employee> findAll();

    @Select("select * from emp where deptno = #{dno}")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column = "ename",property = "ename"),
            @Result(column = "esex",property = "esex"),
            @Result(column = "esalary",property = "esalary"),
            @Result(column = "dno",property = "dept",javaType = Dept.class,
            one = @One(select = "com.gx.mapper.DeptMapper.selectDeptByDeptno",fetchType = FetchType.LAZY)),
    })

    public List<Employee> findAllByDeptno(int dno);

    public List<Employee>  findAllByDept(Employee emp);
}

测试文件:

为了以后方便直接把测试文件的算是头部封装在了MySqlSessionFactory类中了使用时直接调用即可
MySqlSessionFactory 文件:

package com.gx.util;

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 java.io.IOException;
import java.io.InputStream;

public class MySqlSessionFactory {
    public static SqlSession getSqlSession() {
        InputStream input = null;
        SqlSessionFactory sqlSessionFactory = null;
        SqlSession sqlSession = null;
        try {
            //1.定位MyBatis的主配置文件
            input = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //2.根据配置文件创建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
        //3.使用SqlSessionFactory生产SqlSession
        sqlSession = sqlSessionFactory.openSession();

        return sqlSession;
    }
}

测试文件EmployeeTest

package com.gx.test;

import com.gx.mapper.EmployeeMapper;
import com.gx.util.MySqlSessionFactory;
import com.gx.vo.Employee;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class EmployeeTest {
    public static void main(String[] args) {
        SqlSession sqlSession = MySqlSessionFactory.getSqlSession();
        try {

            findAll(sqlSession);
            //测试添加
            //insert(sqlSession);
            //findAllByDeptno(sqlSession);
//            findAllByDept(sqlSession);
            //提交
            sqlSession.commit();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            //如果出现了异常则回滚
            if(sqlSession!=null) {
                sqlSession.rollback();//回滚
            }

        }finally {
            //关闭sqlSession
            if(sqlSession!=null) {
                sqlSession.close();
            }
        }
    }

    private static void findAll(SqlSession sqlSession) {
        EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);//通过反射得到对象
        List<Employee> list = empMapper.findAll();
        for(Employee yg : list) {
            System.out.println(yg.getId()+"  "+yg.getEname()+"  "+yg.getEsex()+" "+ yg.getEsalary()
                    +"  "+yg.getDno());
           /* if(yg.getDept()!=null) {
                System.out.println("        对应的部门信息:"+yg.getDept().getDno()+"  "+yg.getDept().getDname()+"  "+yg.getDept().getDcity());
            }*/
        }
    }

    public static void insert(SqlSession sqlSession) {
        int reusult=0;

        Employee emp = new Employee();
        emp.setEname("赵彬");
        emp.setEsex("男");
        emp.setEsalary(1000.1);
        emp.setDno(1001);
        reusult = sqlSession.insert("com.gx.mapper.EmpMapper.insertEmp",emp);
        System.out.println("添加的结果:"+reusult);
    }

    public static void findAllByDept(SqlSession sqlSession) {
        //通过反射得到EmpMapper接口的对象
        EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee e = new Employee();
        //e.setEname("I");
        //e.setDeptno(20);
        List<Integer> l = new ArrayList<Integer>();
        l.add(1002);
        //l.add(20);
        e.setDeptnos(l);
        List<Employee> list = empMapper.findAllByDeptno(10);
        for(Employee emp : list) {
            System.out.println(emp.getId()+" "+emp.getEname()+" "+emp.getEsex()+" "+emp.getEsalary()+" "+emp.getDno());
        }
    }
}

这种注解方式就是把原本写在.xml配置文件中的sql语句放在了接口方法中的注解上去执行sql,实际上完成的都是相同的方法

你可能感兴趣的:(mybatis)