发表于: 2017-04-28 23:27:38

3 1332


Task1的第六天


今日完成

配置MyBatis

老规矩,添加依赖。

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.2</version>
</dependency>

然后添加数据的properties配置文件。

文件内容如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jnshu?characterEncoding=utf8
jdbc.username=root
jdbc.password=root

添加MyBatis的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <!--读取数据库的配置文件-->
   <properties resource="db.properties" />
   <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC"/>
           <!--使用MyBatis自带的池化的数据源-->
           <dataSource type="POOLED">
               <!--数据库连接的各项参数,由db.properties提供-->
               <property name="driver" value="${jdbc.driver}"/>
               <property name="url" value="${jdbc.url}"/>
               <property name="username" value="${jdbc.username}"/>
               <property name="password" value="${jdbc.password}"/>
           </dataSource>
       </environment>
   </environments>

   <!--映射文件-->
   <mappers>
       <mapper resource="mybatis/sqlmapper/StudentMapper.xml" />
   </mappers>
</configuration>

注意,<mappers>中的内容,在下文“建立Mapper映射文件”中。

建立Mapper接口

创建mapper包,建立Mapper接口。

package com.semonx.jnshu.mapper;

import com.semonx.jnshu.domain.Student;

import java.util.List;

public interface StudentMapper {
   // 查询所有的学生
   public List<Student> findAllStudent();

   // 通过qq号查询一个学生
   public Student findStudentByQq(String qq);

   // 通过线上学习id查询一个学生
   public Student findStudentByOnlineId(String onlineId);

   // 插入一个学生
   public void addStudent(Student student);

   // 通过qq号删除一个学生
   public void deleteStudentByQq(String qq);

   // 通过线上学习id删除一个学生
   public void deleteStudentByOnlineId(String onlineId);

   // 通过qq号修改一个学生
   public void updateStudentByQq(String qq, Student student);

   // 通过线上学习id修改一个学生
   public void updateStudentByOnlineId(String onlineId, Student student);

}

建立Mapper映射文件

代码如下:

<?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.semonx.jnshu.mapper.StudentMapper">

   <!--查询所有学生-->
   <select id="findAllStudent" resultType="com.semonx.jnshu.domain.Student">
       SELECT name,qq,profession,join_date,school,online_id,daily_url,
       declaration,introducer,referee,counselor,description,city,review
       FROM entry_form
   </select>

   <!--通过QQ查询学生-->
   <select id="findStudentByQq" parameterType="String" resultType="com.semonx.jnshu.domain.Student">
       SELECT name,qq,profession,join_date,school,online_id,daily_url,
       declaration,introducer,referee,counselor,description,city,review
       FROM entry_form WHERE qq=#{qq}
   </select>

   <!--通过线上ID查询学生-->
   <select id="findStudentByOnlineId" parameterType="String"
           resultType="com.semonx.jnshu.domain.Student">
       SELECT name,qq,profession,join_date,school,online_id,daily_url,
       declaration,introducer,referee,counselor,description,city,review
       FROM entry_form WHERE online_id=#{onlineId}
   </select>

</mapper>

目前只完成了三个查询,其他部分准备明天完成。

测试

package com.semonx.jnshu.mapper;

import com.semonx.jnshu.domain.Student;
import com.semonx.jnshu.mapper.StudentMapper;
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.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static org.junit.Assert.*;

public class StudentMapperTest {
//    private SqlSessionFactory sqlSessionFactory;
   private StudentMapper mapper;

   @Before
   public void setUp() throws IOException {
       // 配置文件路径
       String resource = "mybatis/SqlMapConfig.xml";
       // 获得配置文件输入流
       InputStream inputStream = Resources.getResourceAsStream(resource);
       // 构建SqlSessionFactory对象
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       // 打开SqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession();
       // 获取代理对象
       StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
   }

   @Test
   public void testFindAllStudent() {
       // 调用接口方法
       List<Student> studentList = mapper.findAllStudent();

       assertNotNull(studentList);
       assertEquals(11, studentList.size());

   }

   @Test
   public void testFindStudentByQq() {
       Student student = mapper.findStudentByQq("15044774771");

       assertNotNull(student);
       assertEquals("陈小六", student.getName());
   }
}

明日计划

  1. 1. 完成增删改部分。
  2. 2. 和spring整合。

问题总结

  1. 1. 最近在准备毕设,事情比较多,好累。写日报都不顺畅了。
  2. 2. 看了看MyBatis,感觉有点乱。
  3. 3.问一下师兄,MyBatis有Dao和Mapper两种使用方式,适用场景有什么区别吗?我现在感觉Mapper好像好一点,我比较喜欢。
  4. 4. 关于Spring的配置,还有MyBatis,都有xml和注解两种,xml和注解哪种用的比较多呢?



返回列表 返回列表
评论

    分享到