Mybatis快速入门

1、Mybatis是什么

1、mybatis是一个ROM(对象关系映射)持久层框架。
2、Mybatis是对JDBC代码的封装,低层还是JDBC代码。

2、Mybatis的优势

1、sql语句与代码分离,存放于xml文件中,方便维护。
2、封装了JDBC的代码,减少代码量。
3、不用手动对结果集进行处理(LIst集合等)

3、Mybatis快速入门

3.1、添加依赖(pom.xml)

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <!-- Mybaits依赖-->
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
  </dependency>
  <dependency>
  <!-- Mysql依赖-->
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
  </dependency>
</dependencies>

<resource>
  <directory>src/main/java</directory>
  <includes>
    <include>**/*.properties</include>
    <include>**/*.xml</include>
    <include>**/*.yml</include>
  </includes>
  <filtering>false</filtering>
</resource>

3.2、文件目录:

在这里插入图片描述

3.2、基于XML文件开发

3.2.1、创建主配置文件:(resource文件下的xxx.xml))

<configuration>
	<!--引入Properties文件(使用"${}")-->
    <properties  resource="db.properties"/>
    <!--
    批量定义别名,之后程序会为包下的所有类都自动加上别名,
    其定义别名的规范就是对应包装类的类名首字母变为小写,
    也可以载JavaBean使用@Alias("别名")注解
    -->
    <typeAliases>
        <package name="org.example.domain"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <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="org\example\dao\UserMapper.xml" />
    </mappers>
</configuration>

3.2.2、配置映射文件(和接口类名一致):

<?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="org.example.dao.UserMapper">
    <!--添加select、update、insert、delete-->
   <select id="SelectByCondition" resultType="User">
        select * from Student
        <where>
            <if test="id!=null and id!=''">
                and id=#{id}
            </if>
            <if test="email!=null and email!=''">
                and email=#{email}
            </if>
            <if test="name!=null and name!=''">
                and name=#{name }
            </if>
            <if test="age!=null and age!=''">
                and age =#{age}
            </if>
            <if test="score!=null and score!=''">
                <choose>
                    <when test="score=='A'">
                        and score >= 80
                    </when>
                    <when test="score=='B'">
                        and score >= 60 and  80 > score
                    </when>
                    <when test="score=='C'">
                        and  60 > score
                    </when>
                </choose>
            </if>
        </where>
    </select>
    <!--
    模糊查询(like)
	concat():mysql字符串拼接。
	trim():去除空格
	-->
    <select id="select" resultType="User">
        select * from student
        <where>
            email like concat("%",trim(#{email}),"%")
        </where>
    </select>
</mapper>

3.2.3、测试

InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
//openSession(false):false是不自动提交事务
SqlSession sqlSession = build.openSession(false);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.SelectByCondition(new User(null, null, null, null, 'A')));
sqlSession.commit();

3.3、基于注解文件开发

3.3.1、创建主配置文件:(resource文件下的xxx.xml))

<configuration>
	<!--引入Properties文件(使用"${}")-->
    <properties  resource="db.properties"/>
    <!--
    批量定义别名,之后程序会为包下的所有类都自动加上别名,
    其定义别名的规范就是对应包装类的类名首字母变为小写,
    也可以载JavaBean使用@Alias("别名")注解
    -->
    <typeAliases>
        <package name="org.example.domain"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <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 class="org.example.dao.UserMapper" />
	</mappers>
</configuration>

3.3.2、注解

		@Insert : 插入sql , 和xml insert sql语法完全一样@Select : 查询sql, 和xml select sql语法完全一样
		@Update : 更新sql, 和xml update sql语法完全一样
		@Delete : 删除sql, 和xml delete sql语法完全一样
		@Param : 入参
		@Results : 设置结果集合@Result : 结果
		@ResultMap : 引用结果集合
		@SelectKey : 获取最新插入id

3.2.3、测试

InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
//openSession(false):false是不自动提交事务
SqlSession sqlSession = build.openSession(false);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.SelectByCondition(new User(null, null, null, null, 'A')));
sqlSession.commit();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值