程序笔记   发布时间:2022-07-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了mybatis入门笔记大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

原始JDBC方式

package org.example;

import org.example.domain.User;

import java.sql.*;
import java.util.ArrayList;

public class App {
    static Connection conn = null;
    static PreparedStatement stmt = null;
    static ResultSet rs = null;
    public static void main( String[] args ) {
        ArrayList<User> users = new ArrayList<User>();
        try {
            //注册mysql驱动
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mybatis";
            String username = "root";
            String password = "123456";
            //创建连接对象
            conn = DriveRMANager.getConnection(url, username, password);
            String sql = "SELEct * from user";
            //创建prepareStatement(线程安全),Statement(线程不安全)
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            //遍历结果集
            while(rs.next()){
                User user = new User();
                user.setNo(rs.geTint("uno"));
                user.setName(rs.getString("uname"));
                user.setEmail(rs.getString("email"));
                users.add(user);
            }
            users.forEach(u -> System.out.println(u));
        } catch (ClassnotFoundException | SQLException E) {
            e.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }
}

@H_230_0@mybatis入门

1. 导入jar包

mybatis入门笔记

2. 在pom.xml中加入maven插件

mybatis入门笔记

3. 创建实体类 User 4. 创建dao 接口UserDao

mybatis入门笔记

5. 编写mapper.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="org.example.dao.UserDao">
    <SELEct id="SELEctUsers" resultType="org.example.domain.User">
        SELEct * from user;
    </SELEct>
    <insert id="insertUser">
        insert into user values(#{uno},#{unamE},#{email})
    </insert>
</mapper>

mybatis入门笔记

tips: 一般将mapper.xml文件放在与UserDao同一目录中,并且名称与dao接口相同.

6. 编写核心配置文件

<?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>
    <properties @R_772_5550@e="jdbc.properties"></properties>
    <setTings>
        <setTing name="logImpl" value="STDOUT_LOGGING"/>
    </setTings>
    <environments default="operate">
        <environment id="operate">
            <!--
            transactionManager:Mybatis提交事务,回滚事物的方式。
                type:事物的类型
                    1) JDBC: 表示mybatis底层是调用JDBC中的Connection对象的,commit,rollBACk
                    2) MANAGED: 把mybatis事务处理委托给其他的容器(一个服务器软件,一个框架(Spring))
-->
            <transactionManager type="JDBC"></transactionManager>
            <!--
            datasource:
                type: 指定数据源的类型。
                    1): 使用连接池,mybatis会创建PooledDatasource类
                    2): 不适用连接池,在每次执行sql语句,先创建连接,执行sql,在关闭连接
                        mybatis会创建一个UnPooledDatasource,管理Connection对象的使用
                    3): java命名和目录服务(windows注册表)
-->
            <datasource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${usernamE}"/>
                <property name="password" value="${passworD}"/>
            </datasource>
        </environment>
    </environments>
    <mappers>
        <!--
         使用package加载mapper.xml:这个包的所有xml文件一次都加载给mybatis
         要求:
                1.mapper文件名需要和接口名称一样,区分大小写
                2.mapper文件和dao接口需要在同一目录
                <package name="org.example.dao"/>
         -->
        <mapper @R_772_5550@e="org/example/dao/UserDao.xml"></mapper>
    </mappers>
</configuration>

7. 创建测试类

mybatis入门笔记

大佬总结

以上是大佬教程为你收集整理的mybatis入门笔记全部内容,希望文章能够帮你解决mybatis入门笔记所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。