黑马SSM教程MyBatis框架学习笔记


1. 原始JDBC操作的缺点及MyBatis的解决方案

JDBC直接操作数据库存在的问题


2. MyBatis快速入门

public class Test1 {
    @Test
    public void test1() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得SQLSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace + id
        List<Student> stuList = sqlSession.selectList("studentMapper.findAll");
        //测试数据
        System.out.println(stuList);
        //释放资源
        sqlSession.commit();
        sqlSession.close();
    }
}

sqlMapConfig.xml

<?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>
    <!--数据源环境-->
    <environments default="server-MySql">
        <environment id="server-MySql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://120.26.1.6/demo"/>
                <property name="username" value="root"/>
                <property name="password" value="$$$$$$$$$$$"/>
            </dataSource>
        </environment>
        <environment id="localServer">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost/demo"/>
                <property name="username" value="root"/>
                <property name="password" value="$$$$$$$"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <mapper resource="com/itheima/mapper/StudentMapper.xml"/>
    </mappers>
</configuration>

StudentMapper.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">
<mapper namespace="studentMapper">
    <select id="findAll" resultType="com.itheima.domain.Student">
        select * from stu
    </select>
</mapper>

3. MyBatis增删改查

插入:
#{实体属性名}

<insert id="insert" parameterType="com.itheima.domain.Student">
        insert into stu values (#{id}, #{name})
</insert>
sqlSession.insert("studentMapper.insert", student);

其他数据库操作:
在这里插入图片描述

4.MyBatis核心配置文件

  • <envoroments><envoroments\>标签
  • <mappers><mappers\>
    在这里插入图片描述
  • <properties><properties\>
    加载properties文件,可以使用EL表达式读取&{}
<properties resources="jdbc.properties"></properties>
  • <typeAliases><typeAliases\>
    自定义别名
    在这里插入图片描述

除了可以设置自定义的别名,mybatis已经为我们设置好了很多常见的别名,如int,long,double,string…

  • typeHandlers标签

5. MyBatis的Dao层实现(重要)

代理开发方式规则
在这里插入图片描述

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> all = mapper.findAll();
        System.out.println(all);
    }
}

6.动态SQL

Mybatis的自动映射和驼峰映射

当我们数据库中字段名为roleName且对应的pojo中的属性名称也为roleName时,可以完成自动映射。
是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。默认关闭。

  • if
    xml使用示例
<select id="selectByCondition" resultType="com.itheima.domian.User">
        select * from user
        <where>
            <if test="id!=0">
                id_ = #{id}
            </if>
            <if test="username!=null">
                and username_ = #{username}
            </if>
            <if test="password!=null">
                and password_ = #{password}
            </if>
        </where>
    </select>
  • choose(when, otherwise)

List<User> selectByIds(List<Integer> ids);
根据ids中的值进行查询
list = [1, 2, 3] 对应的SQL语句为:select * from user where id in (1,2,3)
怎么进行动态查询呢?

<select id="selectByIds" parameterType="list" resultType="com.itheima.domian.User">
        select * from user
        <where>
            <foreach collection="list" item="id" open="id_ in(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

分析:

SQL语句应该为select * from user where id_ in (1,2,3)
其中select * from user为固定的,直接书写。
根据集合中是否有元素来决定是否有where子句,所以使用<where>标签
where子句中 open 和 close 分别为变化部分的左右不变部分
最后用foreach取出集合中的值,#{itemValue}

  • trim(where, set)
  • foreach
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值