使用eclipse时,编写对象的返回值非常麻烦,而使用返回值快捷键可以简化这一过程。
- 第一种 Alt+shift+L
- 将光标放在有返回值的代码句的分号后面;
Resources.getResourceAsStream("mybatis.xml"); new SqlSessionFactoryBuilder().build(resourceAsStream); factory.openSession();
- 按住Alt+Shift+L键,会弹出对话框,输入变量的名字,点击OK即可;
- 将光标放在有返回值的代码句的分号后面;
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession session = factory.openSession();
- 第二种 Ctrl+1
- 将光标放置在有返回值代码句那一行上(分号前后都行);
Resources.getResourceAsStream("mybatis.xml"); new SqlSessionFactoryBuilder().build(resourceAsStream); factory.openSession();
- 选择Assign statement to new local variable : 生成局部变量;
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession session = factory.openSession();
- 选择Assign statement to new field : 生成全局变量;
import java.io.IOException; import java.io.InputStream; import java.util.List; 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 com.bjm.pojo.People; public class Test { private static SqlSessionFactory factory; private static InputStream resourceAsStream; private static SqlSession openSession; public static void main(String[] args) throws IOException{ resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); factory = new SqlSessionFactoryBuilder().build(resourceAsStream); openSession = factory.openSession(); List<People> selectList = openSession.selectList("com.bjm.mapper.selAll"); for (People people : selectList) { System.out.println(people.toString()); }openSession.close(); } }
- 将光标放置在有返回值代码句那一行上(分号前后都行);