11.11 Mybatis

目录

搭建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>

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);
       
        //4.事物提交
        sqlSession.commit();

        //5. 释放资源
        sqlSession.close();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值