MyBatis_3

MyBatis_3

一、MyBatis相关的配置文件

1.1.MyBatis的核心配置文件

1.1.1.名称–我们可以自己定义,推荐大家使用【mybatis-config.xml】
1.1.2.位置
IntelliJ IDEA中通过Maven创建项目,一般都保存在src/main/resources目录下。
1.1.3.文件中的配置元素及其属性
例如:

<?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文件。 ---引入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>指明数据库连接环境的事务管理器。它有一个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”>—指明数据库连接环境的数据源配置【数据库驱动、URL、用户名、密码】

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

UNPOOLED– 这个数据源的实现只是每次被请求时打开和关闭连接。虽然一点慢,它对在及时可用连接方面没有性能要求的简单应用程序是一个很好的选择。 不同的数据库在这方面表现也是不一样的,所以对某些数据库来说使用连接池并不重要,这个配置也是理想的。

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

JNDI– 这个数据源的实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的引用。
< mappers> </ mappers>—配置数据访问接口对应的sql映射文件路径/地址的。
具体的配置:
1.修改工程结构在src/main/resources下创建保存SQL映射文件的包

2.添加SQL映射文件

3.核心配置文件中添加SQL映射路径

<!-- 第一种配置-->
<mappers>
        <mapper resource="com/wangxing/mybatis/mapper/PersonMapper.xml"/>
</mappers>

4.关闭项目,重新打开

<!-- 第二种配置-->
<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文件】

1.2.1.名称【与数据访问接口名称一样,后缀名”.xml”】
1.2.2.位置
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>

1.2.3.文件中的配置元素及其属性
例如:

<?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文件。 ---引入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】

< 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>----配置修改数据用的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实体类成员变量的名称。

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

typeAliases元素—出现在MyBatis的核心配置文件中,给SQL映射文件的数据类型设置别名用的。SQL映射文件的数据类型,insert 元素的参数类型,resultMap元素的types属性等这些地方都需要数据类型。如果我们不设置typeAliases元素,那么SQL映射文件的数据类型就得是包名+类名。
1.一个类一个别名【默认别名】
MyBatis的核心配置文件

<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.一个类一个别名【指定别名】

<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.指定包下的所有类自动生成别名【类名,不区分大小写】

<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>

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

resultType/resultMap都是查询语句执行之后的返回结果的类型表示属性
会在sql映射文件中select元素中使用
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)
);
package com.wangxing.mybatis.bean;

public class Person {
    private int per_id;
    private String per_name;
    private int per_age;
    private String per_address;

    public int getPer_id() {
        return per_id;
    }

    public void setPer_id(int per_id) {
        this.per_id = per_id;
    }

    public String getPer_name() {
        return per_name;
    }

    public void setPer_name(String per_name) {
        this.per_name = per_name;
    }

    public int getPer_age() {
        return per_age;
    }

    public void setPer_age(int per_age) {
        this.per_age = per_age;
    }

    public String getPer_address() {
        return per_address;
    }

    public void setPer_address(String per_address) {
        this.per_address = per_address;
    }
}

数据访问接口

package com.wangxing.mybatis.mapper;

import com.wangxing.mybatis.bean.Person;

import java.util.List;

public interface PersonMapper {
    /*
    添加数据
     */
    boolean insertPerson(Person person);
    /*
    修改数据
     */
    boolean updatePerson(Person person);
    /*
    根据id删除数据
     */
    boolean deletePersonById(int per_id);
    /*
    根据id查询数据
     */
    Person selectPersonById(int per_id);
    /*
    查询全部数据
     */
    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">
    <insert id="insertPerson" parameterType="person">
        insert into t_person values (null,#{per_name},#{per_age},#{per_address});
    </insert>
    <delete id="deletePersonById" parameterType="int">
        delete from t_person where per_id=#{per_id};
    </delete>
    <update id="updatePerson" parameterType="person">
    update t_person set per_name=#{per_name},per_age=#{per_age},per_address=#{per_address} where per_id=#{per_id};
    </update>
    <select id="selectPersonById" parameterType="int" resultType="com.wangxing.mybatis.bean.Person">
     select * from t_person where per_id=#{per_id};
    </select>
    <select id="selectPerson" resultType="com.wangxing.mybatis.bean.Person">
    select * from t_person
    </select>
</mapper>

resultMap表示的执行完数据库操作之后,返回的结果数据类型。
resultMap是mybatis中最重要最强大的元素,使用resultmap可以解决2个问题:
1、JavaBean的属性名和表字段名不一致的问题。
数据库表

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();
}

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">
    <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、付费专栏及课程。

余额充值