• Mybatis搭建


    java环境准备:

    开发环境搭建

    Mybatis搭建步骤:

    1、导入jar包

    2、创建mybatis-config.xml配置数据源

    3、创建工具类,将mybatis-config.xml配置文件写入SqlSessionFactory,打开数据连接,生成SqlSession

    4、创建pojo类,创建dao接口,创建mapping文件

    5、编写测试类实现

    1、创建pojo类User

    public class User {
        private int id;  //id
        private String name;   //姓名
        private String pwd;   //密码
    
    //构造,有参,无参
        //set/get
        //toString()
    }

    2、创建UserDao接口

    public interface UserDao {
        List<User> selectUser();
    }

    3、创建mapping.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="com.alan.dao.UserDao">
        <select id="selectUser" resultType="com.alan.pojo.User">
            select * from user
        </select>
    </mapper>

    4、创建mybatis-config.xml配置文件,配置数据源,注册mapping

    <?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.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="1qazxsw2"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="com/alan/dao/mapping.xml"/>
        </mappers>
    </configuration>

    5、创建工具类MybatisUtils

    public class MybatisUtils {
    
        private static SqlSessionFactory sqlSessionFactory;
    
        static {
            try {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //获取SqlSession连接
        public static SqlSession getSession(){
            return sqlSessionFactory.openSession();
        }
    
    }

    6、创建测试类

    public class MyTest {
        @Test
        public void selectUser() {
            SqlSession session = MybatisUtils.getSession();
            //方法一:
            //List<User> users = session.selectList("com.alan.mapper.UserDao.selectUser");
            //方法二:
            UserDao mapper = session.getMapper(UserDao.class);
            List<User> users = mapper.selectUser();
    
            for (User user: users){
                System.out.println(user);
            }
            session.close();
        }
    }
  • 相关阅读:
    移动Web开发调研
    如何将一个Excel文件中的sheet移动到另外一个Excel?
    Web自动化测试工具调研
    DOM中文本节点索引方法
    词法、语法与语义相关知识
    http 登录Digest认证相关知识
    javascript sandbox
    MVVM与Backbone demo
    Sass与Web组件化相关的功能
    Lua参数绑定函数实现方法
  • 原文地址:https://www.cnblogs.com/alanchenjh/p/12290037.html
Copyright © 2020-2023  润新知