美文网首页
MyBatis第一天

MyBatis第一天

作者: 默辽 | 来源:发表于2019-06-14 17:04 被阅读0次

一、MyBatis简介

  1. Mybatis:开源免费框架,原名叫 iBatis,2010 在 google code,2013 年迁移到 github。
  2. 作用:数据访问层框架。
    2.1 底层是对JDBC的封装。
  3. MyBatis的优点之一
    3.1 使用 mybatis 时不需要编写实现类,只需要写需要执行的 sql 命令。

二、环境搭建

  1. 导包


    image.png
  2. 在src目录下新建全局配置文件(编写JDBC四个变量)
    2.1 没有名称和地址要求(建议直接新建在src目录下,文件取名为mybatis.xml)


    image.png

    2.2 在全局配置文件中引入 DTD 或 schema

<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

2.2.1 如果导入 dtd 后(alt+/)没有提示
Window--> preference --> XML --> XMl catalog --> add 按钮


image.png

2.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>
    <!-- default引用environment的id,当前所使用的环境 -->
    <environments default="default">
        <!-- 声明可以使用的环境 -->
        <environment id="default">
            <!-- 使用原生JDBC事务 -->
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/> 
                <property name="url" value="jdbc:mysql://localhost:3306/java505"/>
                <property name="username" value="root"/>    
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/uaichn/mapper/FlowerMapper.xml"/>
    </mappers>
</configuration>

3 新建以mapper结尾的包,在包下新建:实体类名+Mapper.xml


image.png

3.1 文件作用:编写需要执行的SQL语句
3.2 可以把xml文件理解为实现类
3.3 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">
<!-- namespace:理解成实现类的全路径(包名+类名) -->
<mapper namespace="cn.uaichn.mapper.FlowerMapper">
    <!--id:方法名
        parameterType:定义参数类型
        resultType:返回值类型. 
        
        注意:如果方法返回值是 list,在 resultType 中写 List 的泛型,
        因为 mybatis是对jdbc封装,一行一行读取数据.
        -->
    <select id="selAll" resultType="cn.uaichn.mapper.Flower">
        select * from flower
    </select>
</mapper>
  1. 测试结果
public class MybatisTest {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        //使用工厂设计模式
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //生产Sqlsession
        SqlSession session = factory.openSession();
        List<Flower> list = session.selectList("cn.uaichn.mapper.FlowerMapper.selAll");
        for (Flower flower : list) {
            System.out.println(flower.toString());
        }
        session.close();
    }
}

三、环境搭建详解

  1. 全局配置文件中内容
    1.1 <transactionManager /> type属性
    image.png
    1.2 <dataSource /> type属性
    1.2.1 POOLED 使用数据库连接池
    1.2.2 UNPOOLED 不使用数据库连接池,和直接使用JDBC一样
    1.2.3 JNDI java命名接口技术
    1.2.4 延伸阅读
    image.png
    1.2.5 其他属性
    image.png
    1.2.5.1 <settings>
    <!-- 配置日志 -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

1.2.5.2 <typeAliases> 类型别名是为 Java 类型命名一个短的名字。它只和 XML 配置有关, 只用来减少类完全限定名的多余部分。

    <typeAliases>
        <package name="cn.uaichn.mapper"/>
    </typeAliases>

通过以上配置,直接给某个包下所有类起别名,别名为类名,不区分大小写

<select id="selAll" resultType="cn.uaichn.mapper.Flower">等效于<select id="selAll" resultType="flower">

四、三种查询方式

  1. selectList() 返回值为List <resultType属性控制>
    1.1 适用于查询结果都需要遍历的需求
    <select id="selAll" resultType="cn.uaichn.mapper.Flower">
        select * from flower
    </select>
    List<Flower> list = session.selectList("cn.uaichn.mapper.FlowerMapper.selAll");
        for (Flower flower : list) {
            System.out.println(flower.toString());
    }
image.png

2.selectOne() 返回值object
2.1 适用于返回结果只是变量或一行数据时。

    <select id="selCount" resultType="int">
        select count(*) from flower
    </select>
    int count = session.selectOne("cn.uaichn.mapper.FlowerMapper.selCount");
    System.out.println(count);
    session.close();
image.png
  1. selectMap 返回值Map
    3.1 适用于需要在查询结果中通过某列的值取到这行数据的需求
    3.2 Map<key,resultType控制>
    <!-- resultType决定map的value -->
    <select id="selMap" resultType="cn.uaichn.mapper.Flower">
        select * from flower
    </select>
    //name:把数据库中哪个列的值当作map的key
    Map<Object, Object> map = session.selectMap("cn.uaichn.mapper.FlowerMapper.selMap", "name");
    System.out.println(map);
    session.close();
image.png

相关文章

网友评论

      本文标题:MyBatis第一天

      本文链接:https://www.haomeiwen.com/subject/xfrpfctx.html