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(); } }
执行结果:
时间: 2024-10-12 15:28:48