MyBatis基本开发步骤

<?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="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <!--连接数据库三要素信息-->
                <property name="url" value="jdbc:mysql://localhost:3306/javaweb"/>
                <property name="username" value="1210449106"/>
                <property name="password" value="qqmmyang1"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>
    1. xxxMapper.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="">
    <insert id="insertStu">
        INSERT INTO t_student(id,sname,sage,sex,addr_id) VALUES (null,'张十三',18,1,7)
    </insert>
</mapper>
      1. sql语句最后结尾可以不写“;”
      2. CarMapper.xml文件的名字不是固定的。可以使用其它名字。
      3. CarMapper.xml文件的位置也是随意的。这里选择放在resources根下,相当于放到了类的根路径下。
      4. 将CarMapper.xml文件路径配置到mybatis-config.xml:

  1. 编写测试类
    1. 定位当前工程中mybatis核心配置文件存储位置
    2. SqlSessionFacoryBuilder读取配置文件并交给SqlSessionFactory
    3. SqlSessionFactory为开发人员提供SqlSession对象
    4. SqlSession对象完成SQL语句推送
public class AppTest {
    @Test
    public void test1(){

        InputStream is = null;
        try {
            //1.定位当前工程中的mybatis-config-xml配置文件
            is = Resources.getResourceAsStream("mybatis-config.xml");

            //2.SqlSessionFactoryBuilder读取配置文件信息并交给SqlSessionFactory
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

            //3.SqlSessionFactory为开发人员提供SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);

            //4.SqoSession对象帮助开发人员完成SQL语句推送
            int count = sqlSession.insert("com.wry.pojo.Student.insertStu");
            System.out.println(count);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //资源关闭
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void test02(){
        InputStream is = null;
        try {
            //生成SqlSessionFactoryBuilder
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //生成SqlSessionFactory对象
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is = Resources.getResourceAsStream("mybatis-config.xml"));
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //执行sql语句
            int count = sqlSession.insert("com.wry.pojo.Student.insertStu");
            System.out.println(count);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值