MyBatis(3)-映射文件

2018-09-29 04:03:41来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

本次博文有疑问,请先看MyBatis(1)-简单入门 和 MyBatis(2)-全局配置文件!

如在有疑问,请留言或者咨询博主,博主每天都在!谢谢!

映射文件:

主要是在xxxmapper.xml文件里的配置

映射文件指导这MyBatis如何进行数据库增删该查,有着很重要的意义。

下面开始进入正题,最后会发一个完整版的代码

因为实现的主要内容是增删改查,所以我们现在接口类中定义好相关的方法,后面不在进行单一的定义!

工程的目录如下:

 

EmployeeMapper.java

 

package com.MrChengs.dao;

import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.MrChengs.bean.Employee;

public interface EmployeeMapper {

  //我们直接传入map public Employee getByMap(Map<String,Object> map); //查询满足两个条件的数据 public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name); //查询 public Employee getEmployee(int id); //增加 public void addEmp(Employee employee); //更新 public void updateEmp(Employee employee); //删除 public void deleteById(int id); }

 

 

 

1)insert-映射插入语句

1.1)EmployeeMapper.xml

 

  <insert id="addEmp" >

             insert into test(name,gender,email) values(#{name},#{gender},#{email})

     </insert>
 
1.2)在测试类中MyBatisTest.java

 

 

   @Test
     public void test2() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手动添加数据:
                Employee em = new Employee( "zhangsan", "boy", "zhangsan@qq.com");
                mapper.addEmp(em);
                //手动提交数据
                sqlSession.commit();
           }finally{
                sqlSession.close(); }}

 

 

 

1.3)测试成功

 

2)update映射更新语句

2.1)EmployeeMapper.xml

 

 <update id="updateEmp">
           update test set name=#{name},gender=#{gender} where id=#{id}
     </update>

 

 

 

2.2)测试类中

 

   @Test
     public void test3() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手动添加数据:
                Employee em = new Employee( 6,"Jerry", "gril", null);
                mapper.updateEmp(em);
                //手动提交数据
                sqlSession.commit();
           }finally{
                sqlSession.close();}}

 

 

 

2.3)测试成功!

 

3)delete-映射删除操作

3.1)EmployeeMapper.xml

     <delete id="deleteById">
           delete from test where id=#{id}
     </delete>

 

3.2)测试类中

 @Test
     public void test4() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手动添加数据:
                mapper.deleteById(6);
                //手动提交数据
                sqlSession.commit();
           }finally{
                sqlSession.close();}}

3.3)测试成功!

 

4)关于主键的值问题

有时候需要用到主键值的问题,所以我们需要去学习这个知识点!

4.1)EmployeeMapper.xml

    <!-- useGeneratedKeys="true"使用自增主键获取主键的值 -->
    <!-- keyProperty=""接受对应的主键值-->
    <insert id="addEmp" useGeneratedKeys="true"  keyProperty="id">
        insert into test(name,gender,email) values(#{name},#{gender},#{email})
    </insert>

 

4.2)测试类

  @Test
    public void test2() throws IOException{
        //1.得到SqlSeesionFactory
        SqlSessionFactory sessionFactory = getSqlSessionFactory();
        //2.得到SqlSession
        SqlSession sqlSession = sessionFactory.openSession();
        try{
            EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
            //手动添加数据:
            Employee em = new Employee( "zhangsan", "boy", "zhangsan@qq.com");
            mapper.addEmp(em);
            //获取主键值的属性值进行打印
            System.out.println(em.getId());
            //手动提交数据
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
    }

 

4.3)测试成功

 

5)参数处理

5.1)单个参数:MyBatis不会做任何处理

    #{参数名} : 取出参数值

5.1.1)xml配置文件中

<select id="getId" resultType="com.MrChengs.bean.Employee">
        select id,name,gender,email from test where id = #{idabc}
    </select>

 

5.1.2)测试类

@Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);        
        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) new 
                SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        try{
            Employee employee =  session.selectOne("getId",5);
            System.out.println(employee);
        }finally{
            session.close();
        }
    }

 

5.1.3)测试是成功的!

 

5.2)多个参数时

MyBatis会做特殊的处理

多个参数时封装成map

key param1 ,param2......

value 传入的值

5.2.1)接口类中的查询代码设计如上所展示的

public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);
此时去掉@param:
public Employee getByIdAndNames(int id,String name);

 

5.2.2) xml文件

<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{id} and name=#{name}
 </select>

 

5.2.3)测试代码

  @Test
    public void test5() throws IOException{
                SqlSessionFactory sessionFactory = getSqlSessionFactory();
                SqlSession sqlSession = sessionFactory.openSession();
                try{
                    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
                    Employee employee = mapper.getByIdAndNames(5, "MrChengs");
                    System.out.println(employee);
                }finally{
                    sqlSession.close();
                }
    }

 

5.2.4)此时会报错

5.2.5)处理方案:是在EmployeeMapper.xml文件中修改的

<!-- 同时使用两个参数进行数据库的查询 -->
    <!-- public Employee getByIdAndNames(int id,String name); -->
    <select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{param1} 
      and name=#{param2}
</select>

 

 此时得到正确的答案!

 

5.3)多个查询字段时推荐使用的命名字段

明确指定封装参数时的map和key @param("id")
key:使用@param指定的值
5.3.1)接口类中
public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);

 5.3.2)xml文件中

<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{id} and name=#{name}
</select>

 

测试类是5.2.3中的测试类,此时可以得到正确的结果!
 
 
5.4)
pojo:
如果参数过多,正好是业务逻辑的数据模型,我们直接传入pojo刚刚好
#{属性名} : 取出传入的pojo的属性值
如果多个参数不是业务逻辑数据,没有对应pojo,为了方法我们也可以直接传入map
#{key}就是取出对应的值
 
5.4.1)接口类中的
  public Employee getByMap(Map<String,Object> map);

 5.4.2)xml文件中添加

<select id="getByMap" resultType="com.MrChengs.bean.Employee" >
           select id,name,gender,email from test where id = #{id} and name=#{name}
</select>

 

5.4.3)测试类

  @Test
    public void test6() throws IOException{
                SqlSessionFactory sessionFactory = getSqlSessionFactory();
                SqlSession sqlSession = sessionFactory.openSession();
                try{
                    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", 5);
                    map.put("name", "MrChengs"); 
                    Employee employee = mapper.getByMap(map);
                    System.out.println(employee);
                }finally{
                    sqlSession.close();
                }
    }

 

运行时,可以正确输出结果!

 

5.5)思考问题注意

 

 未完,待更新....

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:线程

下一篇:【Java】Class文件编译的版本号与JDK版本号的对应关系