mybatis(3)

mybatis配置文件的增删改查


 


 

 

1.编写接口方法Mapper接口

public interface BrandMapper {
    public List<Brand> selectAll();
}

2.编写sql语句、sql映射文件

<mapper namespace="com.itheima.mapper.BrandMapper">

    <select id="selectAll" resultType="brand">
        select *
        from tb_brand;
    </select>
</mapper>

3.执行方法,测试

package com.ithei.test;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.Test;

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

public class MybatisTest {
    @Test
    public void testSelectAll() throws IOException {
        //1.获取sqlSessionFactory对象
        String resources ="mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resources);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);


        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取mapper接口的处理对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行方法
        List<Brand> brands = mapper.selectAll();
        System.out.println(brands);

        //5.释放资源
        sqlSession.close();



    }
}

 

但是呢由于我们的pojo实体类和数据库字段名称不一致,所有大致有的数据封装失败。
 
{id=1, brandName='null', companyName='null', ordered=5, description='好吃不上火', status=0}, Brand{id=2, brandName='null', companyName='null', ordered=100, description='华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', status=1}, Brand{id=3, brandName='null', companyName='null', ordered=50, description='are you ok', status=1}
 
那么怎么解决呢?

方法一:给字段起别名

<mapper namespace="com.itheima.mapper.BrandMapper">
    <select id="selectAll" resultType="brand">
        select id, brand_name as brandName, company_name as companyName, ordered, description, status
        from tb_brand;
    </select>

起别名:对不一样的列名起别名,让别名和实体类的属性名一样(缺点:每次查询都要定义一次别名)
解决方法:sql片段,把起别名的语句放到sql片段里.

 <sql id="brand_column">
        id, brand_name as brandName, company_name as companyName, ordered, description, status
    </sql>

 那么怎么引用呢?
用include标签里面的引用id属性

 <select id="selectAll" resultType="brand">
        select
        <include refid="brand_column" />
        from tb_brand;
    </select>

但是呢这个片段的方法不灵活,如果只查询两个字段或几个字段,是要定义一堆sql片段的。
所以下面讲一下第二种方法:

方法二:ResultMap

  1. 定义<resultMap标签>
     
  <resultMap id="brandResultMap" type="brand">
        <!--
        id:完整主键字段的映射
        column:表的列名
        property:实体类的属性名
        result:完成一般字段的映射
        column:表的列名
        property:实体类的属性名
        -->
        <result column="brand_name" property="brandName" />
        <result column="company_name" property="companyName"/>
    </resultMap>

  1. 在select标签中,使用resultMap属性替换resultType属性
    这里的resultMap参数是resultMap的id名
   <select id="selectAll" resultMap="brandResultMap">
        select
        *
        from tb_brand;
    </select>
posted @ 2022-03-30 00:16  长情c  阅读(60)  评论(0)    收藏  举报