导图社区 MyBatis
MyBatis包括导入坐标、核心文件 SqlMapConfig.xml、MyBatis的Dao层实现、映射文件 UserMapper.xml等。
编辑于2021-12-06 17:24:53MyBatis
持久层框架, mybatis 通过xml 或注解的方式将要执行的各种,statemen 配置起来,并通过java对象,和statement中sql的动态参数进行映射生成最值执行的sql语句 最后 mybatis 框架执行sql并将结果映射为java对象并返回。采用ORM 思想解决了实体和数据库映射的问题。对jdbc 进行了封住,屏蔽了jdbc api 底层访问袭击,我们不需要jdbc api 打交道,就可以完成对数据库的持久化操作 原本jdbc开发存在的问题 数据库连接的创建,造成资源的浪费 sql 语句在代码中编写,耦合死,不便于维护 查询操作时,需要手动将结果集封装到实体中,插入操作时,需要手动将实体数据设置到占位符位置 框架解决方法 使用数据库连接池 初始化连接资源 将sql语句抽取到xml 配置文件中 使用反射,内省等底层技术,自动将实体与表进行字段自动映射
MyBatis的Dao层实现
传统开发方式 编写 UserDao接口 编写UserDaoImpl 实现
代理开发方式
主要编写 Mapper 接口 由Mybatis 框架根据接口定义创建动态代理对象,代理对象的方法同上边Dao接口实现类方法 Mapper.xml 文件中 namespace 与 mapper 接口的全限定名相同 Mapper 接口方法名和 Mapper.xml 中定义的每个 statemen 的 id 相同 Mapper 接口方法输入参数类型和 mapper.xml 中定义的每个 sql 的 praameterType 的类型相同 Mapper 接口方法输出参数类型和 mapper.xml 中定义的每个 sql 的 resulTpe 类相同
实现过程UserMapper mapper = sqlSession.getMapper(xx接口.class);
UserMapper.xml <!--查询操作--> <select id="findAll" resultType="user"> select * from user </select> <!--根据id进行查询--> <select id="findById" parameterType="int" resultType="user"> select * from user where id=#{id} </select> service 层 不需要实现 UserMapper 接口 public class ServiceDemo { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> all = mapper.findAll(); System.out.println(all); User user = mapper.findById(1); System.out.println(user); } }
MyBatis 映射文件深入 UserMapper.xml
动态 sql 语句 【SQL 片段抽取】
if 判断
<!--sql语句抽取--> <sql id="selectUser">select * from user</sql> <select id="findByCondition" parameterType="user" resultType="user"> <include refid="selectUser"></include> <where> <if test="id!=0"> and id=#{id} </if> <if test="username!=null"> and username=#{username} </if> <if test="password!=null"> and password=#{password} </if> </where> </select>
foreach 循环 collection 接收容器 open 开始内容 item 结束内容 item 中间值变量 separator 分隔符
<!--sql语句抽取--> <sql id="selectUser">select * from user</sql> <select id="findByIds" parameterType="list" resultType="user"> <include refid="selectUser"></include> <where> <foreach collection="list" open="id in(" close=")" item="id" separator=","> #{id} </foreach> </where> </select> 案列2 select id,name,photo from tb_user where status =1 and id IN <foreach collection="list" item="one" separator="," open="(" close=")"> #{one} </foreach>
item : 表示集合中每一个元素进行迭代时的别名
index : 指定一个名字,用于迭代过程中,每次迭代的位子
open : 表示语句什么开始
separator : 表示每次进行迭代之间以什么符号作为分隔符
close : 表示什么结束
collection : 必须属性 参数是单个 且 是一个list 属性值 list 参数是单个且 是一个array 属性值 array 参数是多个,需要封装成map
MyBatis 核心配置文件深入 SqlMapConfig.xml
typeHandlers 标签 重写或创建 类型转换器 步骤 实现org.apache.ibatis.type.TypeHandler 接口 或继承 org.apache.ibatis.type.BaseTypeHandler 然后可以选择性地将它映射到一个JDBC类型。 例如需求:一个Java中的Date数据类型,我想将之存到数据库的时候存成一个1970年至今的毫秒数,取出来时转换成java的Date,即java的Date与数据库的varchar毫秒值之间转换。
实现 BaseTypeHandler<Date> java代码转换成 date
将java类型转换成数据需要的类型 public void setNonNullParameter()
//将java类型 转换成 数据库需要的类型 public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { long time = date.getTime(); preparedStatement.setLong(i,time); }
帮助执行 preparedStatement 可以设置参数 int i 就是当前参数位置
将数据库中的类型 转换成java类型 public Date getNullableResult()
//将数据库中类型 转换成java类型 //String参数 要转换的字段名称 //ResultSet 查询出的结果集 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { //获得结果集中需要的数据(long) 转换成Date类型 返回 long aLong = resultSet.getLong(s); Date date = new Date(aLong); return date; } //将数据库中类型 转换成java类型 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { long aLong = resultSet.getLong(i); Date date = new Date(aLong); return date; } //将数据库中类型 转换成java类型 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { long aLong = callableStatement.getLong(i); Date date = new Date(aLong); return date; }
String s 是要转换的字段名 Result 查询出来的结果集
plugins 标签 使用第三方插件对功能进行扩展 例如:分页助手PageHelper 是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据
导入 PagHelper 坐标
<groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> //解析器 <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version>
在mybatis核心配置文件中配置PagHelper插件
<!--配置分页助手插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 数据语言--> <property name="dialect" value="mysql"></property> </plugin> </plugins>
在调用前设置 //设置分页相关参数 当前页+每页显示的条数 PageHelper.startPage(3,3);
public void test3() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //设置分页相关参数 当前页+每页显示的条数 PageHelper.startPage(3,3); List<User> userList = mapper.findAll(); for (User user : userList) { System.out.println(user); } //获得与分页相关参数 PageInfo<User> pageInfo = new PageInfo<User>(userList); System.out.println("当前页:"+pageInfo.getPageNum()); System.out.println("每页显示条数:"+pageInfo.getPageSize()); System.out.println("总条数:"+pageInfo.getTotal()); System.out.println("总页数:"+pageInfo.getPages()); System.out.println("上一页:"+pageInfo.getPrePage()); System.out.println("下一页:"+pageInfo.getNextPage()); System.out.println("是否是第一个:"+pageInfo.isIsFirstPage()); System.out.println("是否是最后一个:"+pageInfo.isIsLastPage()); sqlSession.close(); }
getOageNum() 当前页
getPageSize() 每页显示条数
getTotal() 总条数
getPages() 总页数
getPrePage() 上一页
getNextPage() 下一页
isIsFirstPage() 是否是第一个
isIsLastPage() 是否是最后一个
pageHelper.startPage(页面码,数据条数)
//分页查询 public PageResult pageQuery(QueryPageBean queryPageBean) { Integer currentPage = queryPageBean.getCurrentPage();//页码 Integer pageSize = queryPageBean.getPageSize();//几条数据 String queryString = queryPageBean.getQueryString();//过滤条件 //分页查询基于mybatis框架的分页助手插件完成 PageHelper.startPage(currentPage,pageSize);
1、properties标签:该标签可以加载外部的properties文件 2、typeAliases标签:设置类型别名 3、environments标签:数据源环境配置标签 4、typeHandlers标签:配置自定义类型处理器 5、plugins标签:配置MyBatis的插件
MyBatis 多表操作
黑马 MyBatis05day
一对一 查询
对应的sql语句: select * from orders o,user u where o.uid=u.id;
一对多 查询
对应的sql语句:select *,o.id oid from user u left join orders o on u.id=o.uid;
多对多 查询
对应的sql语句: SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id select u.*,r.*,r.id rid from user u left join user_role ur on u.id=ur.user_id inner join role r on ur.role_id=r.id;
映射配置标签 <mapper namespace="com.itheima.mapper.OrderMapper"> 属性 colunm 数据表的字段名称 property 实体属性名称 assicuatuib 标签属性 propert 当前实体(order)中的属性名称 javaType 当前实体中属性 类型(自定义类) collection 标签 配置集合 property 集合名称 ofType 当前集合中的数据类型
<mapper namespace="com.itheima.mapper.OrderMapper"> <resultMap id="orderMap" type="order"> <!--手动指定字段与实体属性的映射关系 column: 数据表的字段名称 property:实体的属性名称 --> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result> <!--<result column="uid" property="user.id"></result> <result column="username" property="user.username"></result> <result column="password" property="user.password"></result> <result column="birthday" property="user.birthday"></result>--> <!-- property: 当前实体(order)中的属性名称(private User user) user数据实现类 javaType: 当前实体(order)中的属性的类型(User) --> <association property="user" javaType="user"> <id column="uid" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> <result column="birthday" property="birthday"></result> </association> </resultMap> <select id="findAll" resultMap="orderMap"> SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id </select> </mapper> <!--配置集合信息 property:集合名称 ofType:当前集合中的数据类型 --> <collection property="orderList" ofType="order"> <!--封装order的数据--> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result>
MyBatis 注解开发
核心配置文件中 导入数据源 加载映射关系
<!--通过properties标签加载外部properties文件--> <properties resource="jdbc.properties"></properties> <!--自定义别名--> <typeAliases> <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias> </typeAliases> <!--数据源环境--> <environments default="developement"> <environment id="developement"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <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> <!--指定接口所在的包--> <package name="com.itheima.mapper"></package> </mappers>
public interface UserMapper { @Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save(User user); @Update("update user set username=#{username},password=#{password} where id=#{id}") public void update(User user); @Delete("delete from user where id=#{id}") public void delete(int id); @Select("select * from user where id=#{id}") public User findById(int id); @Select("select * from user") public List<User> findAll(); @Select("select * from user") @Results({ @Result(id=true ,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "orderList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid") ) }) public List<User> findUserAndOrderAll(); @Select("SELECT * FROM USER") @Results({ @Result(id = true,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "roleList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid") ) }) public List<User> findUserAndRoleAll(); }
public interface OrderMapper { @Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid); @Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result( property = "user", //要封装的属性名称 可是集合 column = "uid", //根据那个字段去查询user表的数据 可以是查询结果id javaType = User.class, //要封装的实体类型 //select属性 代表查询那个接口的方法获得数据 one = @One(select = "com.itheima.mapper.UserMapper.findById") ) }) public List<Order> findAll(); @Select("select *,o.id oid from orders o,user u where o.uid=u.id") @Results({ @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result(column = "uid",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) public List<Order> findAll();//方式二
public interface RoleMapper { @Select("SELECT * FROM sys_user_role ur,sys_role r WHERE ur.roleId=r.id AND ur.userId=#{uid}") public List<Role> findByUid(int uid); }
MyBatis 相关API
SqlSession 工厂构建器 SqlSessionFactoryBuilder 常用API:SqlSessionFactory build(lnputStream inputStream) 通过加载mybatis的核心文件的输入流的形式构建一个SqlSessionFactory对象 其中Resources工具类,在org.apache.ibatis.io 中 Resources类帮助你从类路径下,文件系统或一个web URL 中加载资源文件 SqlSessionFactoryBuilder.builder(resourceAsStream) 构建
String resource="org/mybatis/builder/mybatis-config.xml" InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream);
SqlSessionFactory 有多个方法创建 SqlSession 实例。常用的如下 openSession() 默认开启一个事务,但事务不会自动提交,需要手动提交改事务,更新操作数据才会持久化到数据 中 openSession(boolean autoCommit) 参数是否自动提交,如果设置为true,就不需要手动提交
SqlSession 实例在 MyBatis 中是非常强大的一个类。在这里你会看到所有执行语句、提交或回滚事务和获取映射器实例的方法,执行语句的主要方法有:
<T> selectone(String statement,Objcet parameter) <E> List<E> selectList(String statement,Objcet patameter) int insert(String statement,Objcet patameter) int updata(String statement,Objcet patameter) int delete(String statement,Objcet patameter)
操作事务主要方法 void commit() void rollback()
Resources 核心配置文件 是用 org.apache.ibatis.io 的
入门查询操作
//查询操作 public void test1() throws IOException { //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session回话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行操作 参数:namespace+id List<User> userList = sqlSession.selectList("userMapper.findAll"); //打印数据 System.out.println(userList); //释放资源 sqlSession.close(); }
插入
//插入操作 public void test2() throws IOException { //模拟user对象 User user = new User(); user.setUsername("xxx"); user.setPassword("abc"); //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session回话对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); //执行操作 参数:namespace+id sqlSession.insert("userMapper.save",user); //mybatis执行更新操作 提交事务 //sqlSession.commit(); //释放资源 sqlSession.close(); }
修改
//修改操作 public void test3() throws IOException { //模拟user对象 User user = new User(); user.setId(7); user.setUsername("lucy"); user.setPassword("123"); //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session回话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行操作 参数:namespace+id sqlSession.update("userMapper.update",user); //mybatis执行更新操作 提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); }
删除
//删除操作 public void test4() throws IOException { //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session回话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行操作 参数:namespace+id sqlSession.delete("userMapper.delete",8); //mybatis执行更新操作 提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); }
核心文件 工厂对象 会话对象 都是固定不变的
映射文件 UserMapper.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="userMapper">//java包开始的地址 对应表的实现类 <!--删除操作--> <delete id="delete" parameterType="int"> delete from user where id=#{abc} </delete> <!--修改操作--> <update id="update" parameterType="com.itheima.domain.User"> update user set username=#{username},password=#{password} where id=#{id} </update> <!--插入操作--> <insert id="save" parameterType="com.itheima.domain.User"> insert into user values(#{id},#{username},#{password}) </insert> <!--查询操作--> <select id="findAll" resultType="user"> select * from user </select> <!--根据id进行查询--> <select id="findById" resultType="user" parameterType="int"> select * from user where id=#{id} </select> </mapper>
命名空:<mapper namespace="userMapper"> 别名 <select id="findAll" 使用时 userMapper.findAll 结果类型:parameterType="com.itheima.domain.User" \int 等 查询结果 :select * from user 封装到结果实现类
#{属性名}
反回结果, 输入参数 resultType="user" parameterType="int"
<resultMap id="orderMap" type="order">//order是 数据库实现类 <select id="findAll" resultMap="orderMap"> SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id </select>
resultMap 返回结果
标签 select 查询 insert 插入 updata 修改 delete 删除 where where条件 if 判断 foreach 循环 sql 片段抽取
核心文件 SqlMapConfig.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> <!--通过properties标签加载外部properties文件--> <properties resource="jdbc.properties"></properties> <!--自定义别名--> <typeAliases> <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias> </typeAliases> <!--数据源环境--> <environments default="developement"> <environment id="developement"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <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="com/itheima/mapper/UserMapper.xml"></mapper> </mappers> </configuration>
configuration 配置 properties 属性 settings 设置 typeAliases 类型别名 typeHandlers 类型处理器 objectFactory 对象工厂 plugins 插件 environments 环境| * environment 环境变量 **transactionManager 事务管理器 **dataSource 数据源 databaseldProvider 数据库厂商标识 mappers 映射器
导入坐标
<groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> 日志 非必须 <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version>
public class Order { private int id; private Date ordertime; private double total; //当前订单属于哪一个用户 private User user; public class Role { private int id; private String roleName; private String roleDesc; private int id; private String username; private String password; private Date birthday; //当前用户具备哪些角色 private List<Role> roleList;
@Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与@Result 一起使用,封装多个结果集 @One:实现一对一结果集封装 @Many:实现一对多结果集封装
@Before 抽取
@Before //抽取 public void before() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(true); mapper = sqlSession.getMapper(UserMapper.class); } @Test public void testSave(){ User user = new User(); user.setUsername("tom"); user.setPassword("abc"); mapper.save(user); }
@Results 注解 @Result @One @Many 组合完成复杂查询
public interface OrderMapper { @Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid); @Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result( property = "user", //要封装的属性名称 column = "uid", //根据那个字段去查询user表的数据 javaType = User.class, //要封装的实体类型 //select属性 代表查询那个接口的方法获得数据 one = @One(select = "com.itheima.mapper.UserMapper.findById") ) }) public List<Order> findAll(); @Select("select *,o.id oid from orders o,user u where o.uid=u.id") @Results({ @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result(column = "uid",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) public List<Order> findAll();//方式二
@Results 相当于<resultMap> 使用 格式@Results({@Result(),@Result()})或@Results(@Result())
@Resut 属性 column 数据库的列名 property 需要装配的属性名 one 需要使用 @One 注解(@Result(one=@One)())) many 需要时用@Many 注解 (@Result(many=@many)()))
@One 注解中指定子查询返回单一对象 属性 select 指定用来多表查询的 sqlmapper 使用 @Result(column=" ",property="",one=@One(select=""))
@Many 注解中用来指定子查询返回对象集合 使用格式@Result(property="",column="",many=@Many(select=""))
environments 标签 事务管理器 transactionManager 类有两中 JDBC MANAGED 数据源 dataSource 类偶三种 UNPOOLED POOLED JNDI
JDBC:这个配置就是直接使用了JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务作用域。 MANAGED:这个配置几乎没做什么。它从来不提交或回滚一个连接,而是让容器来管理事务的整个生命周期(比如 JEE 应用服务器的上下文)。 默认情况下它会关闭连接,然而一些容器并不希望这样,因此需要将 closeConnection 属性设置为 false 来阻止它默认的关闭行为。 UNPOOLED:这个数据源的实现只是每次被请求时打开和关闭连接。 POOLED:这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来。 JNDI:这个数据源的实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的引用。
mapper 标签 加载映射
使用相对于类路径的资源引用,例如:<mapper resource="org/mybatis/builder/AuthorMapper.xml"/> 使用完全限定资源定位符(URL),例如:<mapper url="file:///var/mappers/AuthorMapper.xml"/> 使用映射器接口实现类的完全限定类名,例如:<mapper class="org.mybatis.builder.AuthorMapper"/> 将包内的映射器接口实现全部注册为映射器,例如:<package name="org.mybatis.builder"/>
properties 标签 加载额外的配置文件
<!--通过properties标签加载外部properties文件--> <properties resource="jdbc.properties"></properties> jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=root
typeAliases 类别名
框架中的基本类 都已经设置了别名 别名 类型 string String int Intger 等等 <!--自定义别名--> <typeAliases> <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias> </typeAliases>