• helloWorld程序


    总结:

     

    测试方法

        public static void main(String[] args) throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);//解析全局配置文件
            //根据全局配置文件生成sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            try{
                //传入接口EmployeeMapper.class,根据EmployeeMapper.xml的配置,可以得到接口的实现类对象,传统的dao需要我们自己写实现类
                //也就是说,mybatis会为接口创建一个代理对象,代理对象去执行具体的crud
                EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
                System.out.println(mapper.getClass());//Proxy
                employee e = mapper.getEmpById(1);
                System.out.println(e);
            }finally {
                sqlSession.close();
            }
        }

    EmployeeMapper.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="dao.EmployeeMapper"><!-- dao.EmployeeMapper是dao的接口类-->
        <select id="getEmpById" resultType="bean.employee"><!--返回值的类型,包装为bean对象-->
                <!--id为接口类中的抽象方法名-->
                <!--以下为该方法要执行的具体操作-->
            select * from tbl_employee where id = #{id}
            <!-- #{id}:从传递过来的参数id中取值-->
        </select>
    </mapper>

    mybatis-config.xml

    <?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/mybatis?serverTimezone=GMT%2B8"/><!--serverTimezone=GMT%2B8解决了时区不匹配问题-->
                    <property name="username" value="root"/>
                    <property name="password" value="xxx"/>
                </dataSource>
            </environment>
        </environments>
        <mappers><!-- sql映射文件,要注册到全局配置文件中-->
            <mapper resource="mappers/EmployeeMapper.xml"/>
        </mappers>
    </configuration>
  • 相关阅读:
    文件下载的几种方式
    获取文件的后缀名(转为数组) 字符串和变量的拼接 HTML中字符串和变量的拼接
    小程序之选择拍照或者本地相册
    实时显示时间
    uni-app事件冒泡 如何解决事件冒泡 推荐tap事件
    Codeforces Global Round 7 C. Permutation Partitions(组合数学)
    Codeforces Global Round 7 B. Maximums(逻辑)
    Codeforces Global Round 7 A. Bad Ugly Numbers(数学)
    Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)
    Codeforces Round #622 (Div. 2) B. Different Rules(数学)
  • 原文地址:https://www.cnblogs.com/xxxxxiaochuan/p/13873628.html
Copyright © 2020-2023  润新知