• Mybitis配置文件中的别名以及mapper中的namespace


    1 基本知识

    MyBatis中如果每次配置类名都要写全称也太不友好了,我们可以通过在主配置文件中配置别名,就不再需要指定完整的包名了。

    别名的基本用法:

    <configuration>  
        <typeAliases>  
          <typeAlias type="com.domain.Student" alias="Student"/>  
      </typeAliases>  
      ......  
    </configuration>

     但是如果每一个实体类都这样配置还是有点麻烦这时我们可以直接指定package的名字, mybatis会自动扫描指定包下面的javabean,并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(student)

    <typeAliases>  
        <package name="com.domain"/>  
    </typeAliases>

    这样,在Mapper中我们就不用每次配置都写类的全名了,但是有一个例外,那就是namespace。

    namespace属性

    在MyBatis中,Mapper中的namespace用于绑定Dao接口的,即面向接口编程。

    它的好处在于当使用了namespace之后就可以不用写接口实现类,业务逻辑会直接通过这个绑定寻找到相对应的SQL语句进行对应的数据处理

    student = (Student) session.selectOne("com.domain.Student.selectById", new Integer(10));  
    <mapper namespace="com.domain.Student">    
      
        <select id="selectById" parameterType="int" resultType="student">    
           select * from student where id=#{id}    
        </select>  
           
    </mapper>  

    2 Mybitis配置

    这里新建一个spring boot程序

    2.1 目录结构

    2.2 xml文件配置

    mybatis-config.xml配置:

    <?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>
        <typeAliases>
            <package name="sample.mybatis.domain"/>
        </typeAliases>
        <mappers>
            <mapper resource="mapper/CityMapper.xml"/>
            <mapper resource="mapper/HotelMapper.xml"/>
        </mappers>
    </configuration>

    这里要注意别名namespace和mappers的配置。

    CityMapper.xml配置:

    <?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="sample.mybatis.mapper.CityMapper">
        <select id="selectCityById" resultType="City">
            select * from city where id = #{id}
        </select>
    </mapper>

    HotelMapper.xml配置:

    <?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="sample.mybatis.mapper.HotelMapper">
        <select id="selectByCityId" resultType="Hotel">
            select * from hotel where city = #{id}
        </select>
    </mapper>

    2.3  Mapper接口、dao实现

    HotelMapper.java:

    package sample.mybatis.mapper;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import sample.mybatis.domain.Hotel;
    
    /**
     * @author Eduardo Macarron
     */
    @Mapper
    public interface HotelMapper {
    
        Hotel selectByCityId(int city_id);
    
    }
    CityDao.java:
    package sample.mybatis.dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import sample.mybatis.domain.City;
    
    /**
     * @author Eddú Meléndez
     */
    @Component
    public class CityDao {
    
        @Autowired
        private SqlSession sqlSession;
    
        public City selectCityById(long id) {
            return this.sqlSession.selectOne("selectCityById", id);
        }
    
    }

    City.java:

    package sample.mybatis.domain;
    
    import java.io.Serializable;
    
    /**
     * @author Eddú Meléndez
     */
    public class City implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private Long id;
    
        private String name;
    
        private String state;
    
        private String country;
    
        public Long getId() {
            return this.id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getState() {
            return this.state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public String getCountry() {
            return this.country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        @Override
        public String toString() {
            return getId() + "," + getName() + "," + getState() + "," + getCountry();
        }
        
    }

    Hotel.java:

    package sample.mybatis.domain;
    
    import java.io.Serializable;
    
    public class Hotel implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private Long city;
    
        private String name;
    
        private String address;
    
        private String zip;
    
        public Long getCity() {
            return city;
        }
    
        public void setCity(Long city) {
            this.city = city;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getZip() {
            return zip;
        }
    
        public void setZip(String zip) {
            this.zip = zip;
        }
    
        @Override
        public String toString() {
            return getCity() + "," + getName() + "," + getAddress() + "," + getZip();
        }
        
    }

    执行结果:

  • 相关阅读:
    paip.提升效率僵尸代码的迷思
    paip.输入法编程词库多意义条目分割 python实现.
    paip.提升效率提升绑定层次form绑定取代field绑定
    paip.提升效率调试日志系统日志参数含义python
    paip.自定义java 泛型类与泛型方法的实现总结
    paip.提升效率request自动绑定domain object
    paip.提升效率filter map reduce 的java 函数式编程实现
    paip.php 5.0 5.3 5.4 5.5 6.0的新特性总结与比较
    paip.解决中文url路径的问题图片文件不能显示
    paip.判断字符是否中文与以及判读是否是汉字uapi python java php
  • 原文地址:https://www.cnblogs.com/lixuwu/p/5900951.html
Copyright © 2020-2023  润新知