zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    MyEclipse------文件字符输入,输出流读写信息
    MyEclipse------快速写入内容到指定目录下的文件(字节输出流)
    MyEclipse------快速读取特定目录下的文件的内容(字节输入流)
    MyEclipse------在特定目录创建文件和书写内容
    MyEclipse------遍历某个路径下的(所有或特定)文件和目录
    MyEclipse------File类的各种方法
    MyEclipse------如何在特定目录下创建文件夹
    MyEclipse------PreparedStatement使用方法
    使php支持mbstring库
    web页面性能测试
  • 原文地址:https://www.cnblogs.com/xxxxxiaochuan/p/13873628.html
Copyright © 2011-2022 走看看