MyBatis_3

1.MyBatis相关的配置文件

     1.1.MyBatis的核心配置文件

          名称:我们可以自己定义,推荐大家使用【mybatis-config.xml】

          位置:IntelliJ IDEA中通过Maven创建项目,一般都保存在src/main/resources目录下

          文件中的配置元素及其属性:例-->

<?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 resource="mydata.properties"></properties>
    <!-- 配置mybatis默认的连接数据库的环境 -->
    <environments default="development">
        <environment id="development">
            <!-- 配置事务管理器 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源 -->
            <dataSource type="POOLED">
                <property name="driver" value="${mydriver}"/>
                <property name="url" value="${myurl}"/>
                <property name="username" value="${myusername}"/>
                <property name="password" value="${mypassword}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--注册数据访问接口-->
        <mapper resource="PersonMapper.xml"></mapper>
    </mappers>
</configuration>

     解析:

          <?xml version="1.0" encoding="UTF-8"?>---表示xml文件的文件头,说明MyBatis的核心配置文件是一个xml文件。

          <!DOCTYPE configuration

                  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

                "http://mybatis.org/dtd/mybatis-3-config.dtd">---引入MyBatis配置文件的格式。原本在xml文件中出现的标记,我们可以自己定义,但是引入这个MyBatis配置文件的格式之后,我们就不

能在xml文件中自己定义标记了,只能使用MyBatis配置文件的格式提供默认的一组标记。

          <configuration></configuration>---MyBatis配置文件的格式提供的MyBatis核心配置文件的根元素。

          <properties resource="mydata.properties"></properties>MyBatis核心配置文件中configuration元素中的子元素,引入“xxxx.properties”资源文件到当前核心配置文件。通过resources属性

引入,被引入的“xxxx.properties”资源文件应该放在src/main/resources目录下。如果被引入的“xxxx.properties”资源文件放在src/main/resources目录下的子文件夹中,我们应该“子文件夹名称

\xxxx.properties”.例如:<properties resource="mypro\mydata.properties"></properties>,被引入进来的资源文件可以在当前核心配置文件中通过"${}"得到资源文件中的数据值.

           <environments  default="development">---MyBatis核心配置文件中configuration元素中的子元素,定义配置MyBatis的数据库连接环境。default属性:默认值是development。

           <environment id="development"></environment>---是environments子元素,指定具体的数据库连接环境的配置值。

           <transactionManager type="JDBC"></transactionManager>--是environment的子元素指明数据库连接环境的事务管理器。它有一个type属性,这个属性的取值就决定了事务管理由谁去操

作。

                JDBC – 这个配置就是直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务作用域。

                MANAGED – 这个配置几乎没做什么。它从来不提交或回滚一个连接,而是让容器来管理事务的整个生命周期(比如 JEE 应用服务器的上下文)。 默认情况下它会关闭连接,然而一

些容器并不希望这样,因此需要将 closeConnection 属性设置为 false 来阻止它默认的关闭行为。

                   <transactionManager type="MANAGED">

                        <property name="closeConnection" value="false"/>

                   </transactionManager>

                NOTE--如果你正在使用 Spring + MyBatis,则没有必要配置事务管理器, 因为 Spring 模块会使用自带的管理器来覆盖前面的配置。

       <dataSource type="POOLED">

            <property name="driver" value="${mydriver}"/>

            <property name="url" value="${myurl}"/>

            <property name="username" value="${myusername}"/>

            <property name="password" value="${mypassword}"/>

       </dataSource>

         <dataSource  type="POOLED"></dataSource>---指明数据库连接环境的数据源配置【数据库驱动、URL、用户名、密码】

         dataSource元素有一个type数据,配置指定是否使用数据库连接池技术。

          UNPOOLED– 这个数据源的实现只是每次被请求时打开和关闭连接。虽然一点慢,它对在及时可用连接方面没有性能要求的简单应用程序是一个很好的选择。 不同的数据库在这方面表现

也是不一样的,所以对某些数据库来说使用连接池并不重要,这个配置也是理想的。

          POOLED– 这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来,避免了创建新的连接实例时所必需的初始化和认证时间。 这是一种使得并发 Web 应用快速响应请求的流行处理

方式。

         JNDI– 这个数据源的实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的引用。

         <mappers> </mappers>---MyBatis核心配置文件中configuration元素中的子元素,是配置数据访问接口对应的sql映射文件路径/地址的。

           具体的配置:

           <!-- 第一种配置-->

                <!-- src/main/resources/PersonMapper.xml -->

               <!-- src/main/resources/com/wangxing/mybatis/mapper/PersonMapper.xml -->

                    <mappers>
                        <mapper resource="com/wangxing/mybatis/mapper/PersonMapper.xml"/>
                    </mappers>

            <!-- 第二种配置-->:数据访问接口与sql映射文件在同一个包[数据访问接口所在包]中

                   <mappers>
                       <package name="com.wangxing.mybatis.mapper"/>
                   </mappers>

                      可以将整个包中的SQL映射文件全部加载。

                   <mappers>
                       <mapper class="com.wangxing.mybatis.mapper.PersonMapper"></mapper>

                   </mappers>

             <!-- 第三种配置-->:SQL映射文件不在工程下,而是本机的指定目录下

                  <mappers>
                       <mapper url="file:///F:/20200728/MyBatis/PersonMapper.xml"></mapper>

                  </mappers>

     1.2.Sql映射文件【Mapper文件】

          名称:与数据访问接口名称一样,后缀名”.xml”

          位置:

                   1.与核心配置文件同在src/main/resources下

                      核心配置文件:<mapper resource="PersonMapper.xml"/>

                   2.在src/main/resources下的子文件夹中

                      核心配置文件:<mapper resource="mapper/PersonMapper.xml"/>

                   3.在src/main/resources下通过修改工程结构创建的包中【重启】

                        核心配置文件:

                          <mapper resource="com/wangxing/mybatis/mapper/PersonMapper.xml"/>

                          <package name="com.wangxing.mybatis.mapper"/>

                          <mapper class="com.wangxing.mybatis.mapper.PersonMapper"></mapper>

                    4.不在工程中,而是在指定的目录下

                           核心配置文件:<mapper url="file:///F:/20200728/MyBatis/PersonMapper.xml"></mapper>

          文件中的配置元素及其属性:例-->

<?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="com.wangxing.mybatis.mapper.PersonMapper">
    <insert id="insertPerson" parameterType="com.wangxing.mybatis.bean.Person">
        insert into t_person values (null,#{pername},#{perage},#{peraddress});
    </insert>
    <update id="updatePerson" parameterType="com.wangxing.mybatis.bean.Person">
        update t_person set per_name=#{pername},per_age=#{perage},per_address=#{peraddress} where per_id=#{perid};
    </update>
    <resultMap id="personMap" type="com.wangxing.mybatis.bean.Person">
        <id column="per_id" property="perid"></id>
        <result column="per_name" property="pername"></result>
        <result column="per_age" property="perage"></result>
        <result column="per_address" property="peraddress"></result>
    </resultMap>
    <select id="selectPersonById" parameterType="int" resultMap="personMap">
        select * from  t_person where per_id=#{perid};
    </select>
    <select id="selectPerson" resultMap="personMap">
        select * from  t_person;
    </select>
    <delete id="deletePersonById" parameterType="java.lang.Integer">
        delete from t_person where per_id=#{perid};
    </delete>
</mapper>

     解析:

        <?xml version="1.0" encoding="UTF-8"?>---表示xml文件的文件头,说明MyBatis的核心配置文件是一个xml文件。

        <!DOCTYPE mapper
                 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
---引入SQL映射文件的格式。原本在xml文件中出现的标记,我们可以自己定义,但是引入这个SQL映射文件的格式之后,我们就不能在

xml文件中自己定义标记了,只能使用SQL映射文件的格式提供默认的一组标记。

        <mapper namespace="com.wangxing.mybatis.mapper.PersonMapper">--sql映射文件的根元素,它有一个namespace属性,这个属性是用来设置数据访问接口的包名+接口名。

         <insert id="insertPerson" parameterType="com.wangxing.mybatis.bean.Person">                

                  insert into t_person values (null,#{pername},#{perage},#{peraddress});        

        </insert>----配置插入数据用的sql语句

           id---数据访问接口中添加数据方法的方法名称

           parameterType----数据访问接口中添加数据方法的参数类型

                   1.PoJo类型【对象型】  com.wangxing.mybatis.bean.Person

                   2.基本类型【String,基本类型,封装类】

                         String---java.lang.String

                         基本类型---int,boolean.......

                         封装类-----java.lang.Ingeter

                    3.集合类型【list,hashMap】

         <delete id="deletePersonById" parameterType="java.lang.Integer">                 

                 delete from t_person where per_id=#{perid};

          </delete>---配置删除数据用的sql语句 

           id---数据访问接口中添加数据方法的方法名称

           parameterType----数据访问接口中添加数据方法的参数类型

                   1.PoJo类型【对象型】  com.wangxing.mybatis.bean.Person

                   2.基本类型【String,基本类型,封装类】

                         String---java.lang.String

                         基本类型---int,boolean.......

                         封装类-----java.lang.Ingeter

                    3.集合类型【list,hashMap】

         <select id="selectPersonById" parameterType="int" resultMap="personMap">               

            select * from  t_person where per_id=#{perid};        

         </select>----配置查询数据用的sql语句    

           id---数据访问接口中添加数据方法的方法名称

           parameterType----数据访问接口中添加数据方法的参数类型

                   1.PoJo类型【对象型】  com.wangxing.mybatis.bean.Person

                   2.基本类型【String,基本类型,封装类】

                         String---java.lang.String

                         基本类型---int,boolean.......

                         封装类-----java.lang.Ingeter

                    3.集合类型【list,hashMap】

            resultMap---配置查询的结果类型的元素【数据库表中的列名称与实体类中的成员变量的名称不同】

            resultType--配置查询的结果类型。【数据库表中的列名称与实体类中的成员变量的名称相同】

        <resultMap id="personMap" type="com.wangxing.mybatis.bean.Person">
                <id column="per_id" property="perid"></id>
                <result column="per_name" property="pername"></result>
                <result column="per_age" property="perage"></result>
                <result column="per_address" property="peraddress"></result>
          </resultMap>

             id属性---查询的结果类型的名称,实际上就是select元素中resultMap的属性值

             type属性---查询的结果的具体类型【java实体类的类名】

             resultMap元素中目前有2类子元素

             <id>---配置主键列映射

             <result>---配置非主键列映射

             column属性----数据库表的列名

             property属性---java实体类成员变量的名称。

2.MyBatis的核心配置文件中的typeAliases元素有什么作用,如何配置,如何使用?

      typeAliases元素---出现在MyBatis的核心配置文件中,给SQL映射文件的数据类型设置别名用的。SQL映射文件的数据类型,insert 元素的参数类型,resultMap元素的types属性等这些地方

都需要数据类型。如果我们不设置typeAliases元素,那么SQL映射文件的数据类型就得是包名+类名。

     1.一个类一个别名【默认别名】

          MyBatis的核心配置文件中properties标记与environments标记之间设置:

                 <typeAliases>
                       <!--默认的别名 [类名,不区分大小写]-->
                       <typeAlias type="com.wangxing.mybatis.bean.Person"></typeAlias>
                </typeAliases>

          Sql映射文件中使用:

               <insert id="insertPerson" parameterType="Person">
                    insert into t_person values (null,#{pername},#{perage},#{peraddress});
               </insert>
               <update id="updatePerson" parameterType="person">
                   update t_person set per_name=#{pername},per_age=#{perage},per_address=#{peraddress} where per_id=#{perid};
               </update>

     2.一个类一个别名【指定别名】

         MyBatis的核心配置文件中properties标记与environments标记之间设置:

            <typeAliases>
                   <typeAlias alias="personBean" type="com.wangxing.mybatis.bean.Person"></typeAlias>
            </typeAliases>

         Sql映射文件中使用:

            <resultMap id="personMap" type="personBean">
                 <id column="per_id" property="perid"></id>
                 <result column="per_name" property="pername"></result>
                 <result column="per_age" property="perage"></result>
                 <result column="per_address" property="peraddress"></result>
            </resultMap>
            <select id="selectPersonById" parameterType="int" resultMap="personMap">
                select * from  t_person where per_id=#{perid};
            </select>

     3.指定包下的所有类自动生成别名【类名,不区分大小写】

          MyBatis的核心配置文件中properties标记与environments标记之间设置:

              <typeAliases>
                  <!--指定包下的所有类自动生成别名【类名,不区分大小写】-->
                  <package name="com.wangxing.mybatis.bean"/>
              </typeAliases>

          Sql映射文件中使用:

              <insert id="insertPerson" parameterType="Person">
                     insert into t_person values (null,#{pername},#{perage},#{peraddress});
              </insert>
              <update id="updatePerson" parameterType="person">
                      update t_person set per_name=#{pername},per_age=#{perage},per_address=#{peraddress} where per_id=#{perid};
              </update>

3.Sql映射文件中的select元素resultType resultMap属性的区别?【输出数据就是返回值】

     resultType/resultMap都是查询语句执行之后的返回结果的类型表示属性,会在sql映射文件中select元素中使用。

       1.  resultType表示的执行完数据库操作之后,返回的结果数据类型,这个结果可以用三种类型数据来处理:

             1.简单类型。例如:string、long、integer、double等

             2.pojo类型。例如:Person,User等

             3.HashMap类型。

             数据库表中的列名称与实体类中的成员变量的名称相同时,一般设置resultType指定返回的结果数据类型。

              数据库表:

create  table t_person(
per_id int primary key auto_increment,
per_name varchar(20),
per_age int,
per_address varchar(30)
);

            bean类:

package com.wangxing.mybatis.bean;

public class Person {
    private int per_id;
    private String per_name;
    private int per_age;
    private String per_address;
........
getXXX()、setXXX()
}

              数据访问接口:

import com.wangxing.mybatis.bean.Person;
import java.util.List;
public interface PersonMapper {
    /**
     * 查询所有数据
     * @return
     */
    List<Person> selectPerson();
}

               SQL映射文件:

<?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="com.wangxing.mybatis.mapper.PersonMapper">
    <select id="selectPerson" resultType="com.wangxing.mybatis.bean.Person">
        select * from  t_person;
    </select>
</mapper>

          2. resultMap表示的执行完数据库操作之后,返回的结果数据类型。resultMap是mybatis中最重要最强大的元素,使用resultmap可以解决2个问题:

               1.数据库表中的列名称与实体类中的成员变量的名称不相同时,设置resultMap指定返回的结果数据类型。

create  table t_person(
per_id int primary key auto_increment,
per_name varchar(20),
per_age int,
per_address varchar(30)
);
package com.wangxing.mybatis.bean;

public class Person {
    private int perid;
    private String pername;
    private int perage;
    private String peraddress;
........
getXXX()、setXXX()
}
import com.wangxing.mybatis.bean.Person;
import java.util.List;
public interface PersonMapper {
    /**
     * 查询所有数据
     * @return
     */
    List<Person> selectPerson();
}
<?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="com.wangxing.mybatis.mapper.PersonMapper">
    <resultMap id="personMap" type="com.wangxing.mybatis.bean.Person">
        <id column="per_id" property="perid"></id>
        <result column="per_name" property="pername"></result>
        <result column="per_age" property="perage"></result>
        <result column="per_address" property="peraddress"></result>
    </resultMap>
    <select id="selectPerson" resultMap="personMap">
        select * from  t_person;
    </select>
</mapper>

              2.完成高级查询。例如:一对一、一对多、多对多。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值