发表于: 2017-11-20 20:21:03

1 701


今天完成的事情:

    今天主要是在昨天所做的Mybatis上的代码重构以及使用mapper代理DAO接口,不用分离Impl

先附上一下结构列表

这是进行DAO接口并且使用实现类实现的方法,感觉整体上的格式和JDBC没有多大出入,仅仅是把SQL语句提到了XML文件中,这样就不能提现出Mybatis的优势在哪里,


Mybatis本身提供了一种方法,使用mapper.xml文件对DAO接口代理的方法:

定义好了mapper.xml映射文件后,接下来就要编写mapper接口了,编写mapper接口要遵循以下四个开发规范:

在mapper.xml中,使namespace等于mapper接口的地址(完全限定名)

②mapper.java接口中的方法名和mapper.xml中statement的id一致

③mapper.java接口中方法的输入参数类型和mapper.xml中statement的parameterType指定的类型一致

④mapper.java接口中方法返回值类型和mapper.xml中④statement的resultType指定的类型一致

package com.mybatis.mapper;

import com.mybatis.bean.Student;

import java.util.List;

/**
* @author Arike
* Create_at  2017/11/20 14:57
*/
public interface StudentMapper {

   Student getStudentById(long id);
    
   List<Student> getStudentByName(String name);
   
   void updateStudent(Student student);
   
   void insertStudent(Student student);
   
   void deleteStudent(long id);
   
}

增删改查所有方法的名称和返回值类型都要和mapper.xml中一样.

其中比较特殊的是使用名称模糊查询getStudentName.虽然在mapper中设置的返回值类型是Student,但我在接口中所使用的返回值是集合,这是因为模糊查询返回值很可能不止1个,Mybatis强大又体现出来了,他会根据你返回值的数量个数而改变返回的类型为一个集合还是一个对象


测试方法


import java.io.Reader;

import static org.junit.Assert.*;

/**
* @author Arike
* Create_at  2017/11/20 16:51
*/
public class StudentMapperTest {
private SqlSession sqlSession;
   private StudentMapper sm;
   
   @Before
   public void setup() throws Exception {
Reader reader = Resources.getResourceAsReader("config.xml");
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       sqlSession = sqlSessionFactory.openSession(true);//设置为true表示自动提交事务.默认为false
       sm = sqlSession.getMapper(StudentMapper.class);
   }

@Test
   public void getStudentById() throws Exception {

System.out.println(sm.getStudentById(41L));
   }

@Test
   public void getStudentByName() throws Exception {
System.out.println(sm.getStudentByName("%"));
   }

@Test
   public void updateStudent() throws Exception {
sm.updateStudent(new Student(37L,"孙悟空",20));
       
   }

@Test
   public void insertStudent() throws Exception {
Student s =new Student("悟饭",18);
       sm.insertStudent(s);
       System.out.println(s.getId());
   }

@Test
   public void deleteStudent() throws Exception {
sm.deleteStudent(48L);
   }

}

因为我没有在mapper中定义实现类, 所以需要在测试类中生成SqlsessionFactoty对象用以操作DAO接口.

具体步骤:

①通过基本配置文件config.xml获取到SqlSessionFactory对象

②通过SqlSessionFactory对象获取到SqlSession对象

③使用SqlSession提供的getMapper()方法获取到DAO接口的实例对象.(使用接口的.class文件获取)

④之后就可以通过Mybatis生成的这个实例对象操作接口中的方法.


从这里就可以看出,Mybatis把映射做的是多强大.


明天计划的事情

早上继续多线程的学习,中午开始Mybatis动态SQL的学习.


遇到的问题:

首先在对前一天的代码重构的过程中又遇到了找不到配置文件的异常, 这个和第一天学习mybatis时如出一辙,

后来经过很多调试,修改都没用,发现自己又不报异常了...估计是IDEA本身出问题了把.

为了一劳永逸的解决这个问题,我发现了如下在maven项目中使用Mybatis避免出现此类IOException的方法:

在maven配置pom.xml中加上一段设置语句:

<build>
 <resources>
   <resource>
     <directory>src/main/java</directory>
     <includes>
       <include>**/*.xml</include>
     </includes>
   </resource>
 </resources>
</build>

加上之后idea就可以通过maven识别到maven独特路径下的.xml配置文件了.因为.xml文件会有很多,全部放到resources文件夹中不方便命名,也不方便查找管理,最好的是放到mapper.xml代理的接口的包中.

这样统一维护的时候比较直观.

另外Mybaits基本配置文件上就可以直接使用com/../../这种想对包名来定位了:

<mappers>
   <mapper resource="com/mybatis/DAO/daomapper.xml"/>
   <mapper resource="com/mybatis/mapper/mapper.xml"/>
</mappers>


另外在测试的时候发现查询语句可以很正确的执行,但是增删改执行之后发现事务并没有提交,本来想手动使用commit,但是使用mapper代理之后操作的是DAO接口的实例,实例是没有commit方法的,尝试过在接口中添加commit(),但是发现mapper并不会给DAO接口中的方法定义,所以通过查询:

public void setup() throws Exception {
Reader reader = Resources.getResourceAsReader("config.xml");
   SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
   sqlSession = sqlSessionFactory.openSession(true);//设置为true表示自动提交事务.默认为false
   sm = sqlSession.getMapper(StudentMapper.class);
}

SqlSessionFactory的openSession方法有默认值为boolean类型的false,当用默认情况生成SqlSession对象.对象调用SQL语句方法不会默认提交事务,如果加上初始值true,就会自动commit增删改SQL了. 这个同样适用自己实现DAO接口的方式.实现之后就不用手动commit了.


收获:

①如何在Maven中使用自定义路径来存放.xml文件并且能够读取

②如何使用mapper代理的方式实现DAO接口(这也就是任务17步里所提的疑问,为什么不需要分离Impl)


禅道::http://task.ptteng.com/zentao/project-burn-414.html


返回列表 返回列表
评论

    分享到