学习内容mybatis

文章目录

  • 1.引入依赖到pom.xml

    2.配置核心配置文件:mybatis-config.xml (resources目录下)

    3.mysql映射文件:CollegeMapper.xml(resources目录下)

    4.测试


    MyBatis 是一款优秀的持久层框架,用于简化 JDBC 开发。

    框架:
     框架就是⼀个半成品软件,是⼀套可重⽤的、通⽤的、软件基础代码模型

    搭建Mybatis框架
    1.引入依赖到pom.xml
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.5</version>
    </dependency>
    2.配置核心配置文件:mybatis-config.xml (resources目录下)
            1)通用配置

            2)DataSource数据源,连接数据库

            3)加载sql映射文件

    <?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>
            <package name="com.ucloud.mybatis02.pojo"/>
        </typeAliases>
        
        <!--environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的environment-->
        <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:///mybatis?useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
     
        </environments>
     
        <mappers>
            <!--加载sql映射文件-->
           <mapper resource="com/ucloud/mybatis02/mapper/CollegeMapper.xml"></mapper>
        </mappers>
    </configuration>

    编写 SQL 映射⽂件 --> 统⼀管理sql语句,解决硬编码问题
    在模块的 resources ⽬录下创建映射配置⽂件 UserMapper.xml ,内容如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybat
    is.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="test">
    <select id="selectAll" resultType="user">
    select * from tb_user;
    </select>
    </mapper>

    3.mysql映射文件:CollegeMapper.xml(resources目录下)
            1)namespace:命名空间

            2)标签形式封装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="test">
        <select id="selectCollegeById" parameterType="String" resultType="College">
            select * from sims_college where ID=#{ID}
        </select>
     
        <insert id="insertCollege" parameterType="College">
            insert into sims_college values(#{ID},#{NAME},#{STUDENT_NUMBER},#{PRESIDENT})
        </insert>
    </mapper>
    4.测试
    public class DeleteCollegeDemo {
     
        public static void main(String[] args) throws IOException {
            //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
     
            //2. 获取SqlSession对象,用它来执行sql
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //3. 执行sql
            College college = new College();
            college.setID("2");
            college.setNAME("美术");
            college.setSTUDENT_NUMBER(100);
            college.setPRESIDENT("点击");
     
            //要和映射文件中的id名一致
            int row= sqlSession.insert("test.insertCollege",college);
           
           .事物提交
            sqlSession.commit();
     
         . 释放资源
            sqlSession.close();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sjhlll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值