1.MyBatis执行SQL的两种方式:SqlSession和Mapper接口
方式一:用 Mapper 接口发送 SQL
PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
personMapper.insertPerson(person);
通过 SqlSession 的 getMapper 方法来获取一个 Mapper 接口,就可以调用它的方法了。因为 SQL映射 文件或者接口注解定义的 SQL 都可以通过“类的全限定名+方法名”查找,所以 MyBatis 会启用对应的 SQL 进行运行,并返回结果。
package com.wangxing.mybatis.test;
import com.wangxing.mybatis.bean.Person;
import com.wangxing.mybatis.mapper.PersonMapper;
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.util.List;
public class TestMain {
public static void testInsertPerson(){
//定义SqlSession对象
SqlSession sqlSession=null;
try {
//通过SqlSessionFactoryBuilder类创建出SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
//从SqlSessionFactory中取得一个SqlSession对象
sqlSession=sqlSessionFactory.openSession();
//通过得到数据访问接口对象调用insertPerson方法
PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
Person person=new Person();
person.setPername("zhangsan");
person.setPerage(23);
person.setPeraddress("西安");
personMapper.insertPerson(person);
//提交sqlsession
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
public static void testUpdatePerson(){
//定义SqlSession对象
SqlSession sqlSession=null;
try {
//通过SqlSessionFactoryBuilder类创建出SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
//从SqlSessionFactory中取得一个SqlSession对象
sqlSession=sqlSessionFactory.openSession();
//通过得到数据访问接口对象调用insertPerson方法
PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
Person person=new Person();
person.setPerid(1);
person.setPername("lisi");
person.setPerage(24);
person.setPeraddress("beijing");
personMapper.updatePerson(person);
//提交sqlsession
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
public static void testSelectPersonById(){
//定义SqlSession对象
SqlSession sqlSession=null;
try {
//通过SqlSessionFactoryBuilder类创建出SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
//从SqlSessionFactory中取得一个SqlSession对象
sqlSession=sqlSessionFactory.openSession();
//通过得到数据访问接口对象调用insertPerson方法
PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
Person person=personMapper.selectPersonById(1);
//提交sqlsession
sqlSession.commit();
System.out.println(person.getPerid()+"\t"+person.getPername());
}catch (Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
public static void testSelectPerson(){
//定义SqlSession对象
SqlSession sqlSession=null;
try {
//通过SqlSessionFactoryBuilder类创建出SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
//从SqlSessionFactory中取得一个SqlSession对象
sqlSession=sqlSessionFactory.openSession();
//通过得到数据访问接口对象调用insertPerson方法
PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
List<Person> personlist=personMapper.selectPerson();
//提交sqlsession
sqlSession.commit();
System.out.println("personlist.size----"+personlist.size());
}catch (Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
public static void testDeletePerson(){
/