MyBatis学习笔记

2016-12-04  本文已影响72人  im宇

写在前面:
这是一篇菜鸟读 MyBatis 官方文档的学习笔记。

临近毕业了,开始准备毕业设计。之前就在想,毕业设计做一个好玩点装逼点的东西:好玩点的是做个智能小车,装逼点的是做个用遗传算法解决实际问题。但是后来呢,还是跟现实妥协了:做个跟以后工作息息相关的事情吧!因此我选择了做一个被选烂了的老生常谈的《XXX系统》系列。为了完成这个“高实用性”的《XXX系统》,只好恶补一下相关知识。毕业设计知识准备第一个知识点先来学学数据库相关框架吧——MyBatis

MyBatis 浅显流程图

MyBatis浅显流程.png

我想以后只要看着上面的流程图,大概就会使用 MyBatis 了吧。
先来简单说说 初次使用 MyBatis 的流程吧:

  1. 导入mybatis包,这个不用说了

  2. 创建配置文件“mybatis-config.xml”,并做相关配置。最基础的配置就是<environment>和<mappers>两个标签。

  3. 创建一个MyBatisUtil类,封装一个返回SqlSession函数。

  4. 编写Mapper.xml、IMapper.java(这里使用接口形式)

  5. 将Mapper.xml注册到“mybatis-config.xml”中。即<mappers>里面写一个<mapper resource="xxx/xxx/Mapper.xml"/>,这样才能告诉MyBatis上哪里找sql语句

Mybatis 配置文件

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

一些对象的 Scope 和生命周期

SqlSession session = sqlSesstionFactory.openSession();
try{
  // do work
}finally{
  session.close();
}
SqlSession session = sqlSessionFactory.openSession();
try{
  XxxMapper mapper = session.getMapper(XxxMapper.class);
  // do word
}finally{
  session.close();
}

附上一些文件的写法,方便以后复制粘贴

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.imframework</groupId>
    <artifactId>imframework</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>imframework Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- mybatis包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- jdbc driver for mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <!-- mybatis启动要加载log4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--<resource>-->
                <!--<directory>src/main/resources</directory>-->
                <!--<includes>-->
                    <!--<include>**/*.xml</include>-->
                <!--</includes>-->
            <!--</resource>-->
        </resources>
        <finalName>imframework</finalName>
    </build>
</project>
<?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>
        <typeAlias alias="User" type="me.imframework.model.User"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/hbatis?useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="me/imframework/model/UserMapper.xml"/>
    </mappers>
</configuration>
<?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="me.imframework.dao.IUser">
    <!-- 没有 parameterType声明,typeHandlers自动推算类型 -->
    <select id="selectById" resultType="User">
        select * from user where id = #{id}
    </select>
    <!-- 一个多条数据插入的例子 -->
    <insert id="insertAll" parameterType="java.util.List">
        insert into user(name,age,address) values
        <foreach item="item" collection="list" separator=",">
            (#{item.name},#{item.age},#{item.address})
        </foreach>
    </insert>
</mapper>
上一篇下一篇

猜你喜欢

热点阅读