• MyBatis 学习总结(二)


    1.MyBatis基础环境的搭建
    1.1 核心配置文件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> 
        <typeAliases> 
            <typeAlias alias="User" type="com.mipo.entity.User"/> 
        </typeAliases> 
        <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/test?characterEncoding=utf-8"/> 
                    <property name="username" value="root"/> 
                    <property name="password" value="root"/> 
                </dataSource> 
            </environment> 
        </environments> 
        <mappers> 
            <mapper resource="com/mipo/mapping/UserMapper.xml"/> 
        </mappers>
    </configuration>                    

    1.2 配置映射文件UserMapper.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.mipo.mapping.UserMapper"> 
        <select id="selectUser" parameterType="Integer" resultType="User"> 
            SELECT * FROM T_USER WHERE ID = #{ID} 
        </select> 
    </mapper>
        

    1.3 编写junit单元测试

    @Test
    public void myBatisTest(){
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new qlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            User user = (User)sqlSession.selectOne("com.mipo.mapping.UserMapper.selectUser", Integer.valueOf(1));
            System.out.println(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 相关阅读:
    火狐常用的插件
    sourceinsight技巧
    为sourceinsight添加makefile、kconfig、*.S文件支持
    如何在shell中打印出带颜色的字符?
    Linux shell tee指令学习
    【转载】dirs、pushd、popd指令
    【转载】SHELL字符串处理技巧(${}、##、%%)
    【转载】利用shell脚本获取一个文件的绝对路径readlink
    如何查看智能手机的IP地址
    SDK Manager中勾选项
  • 原文地址:https://www.cnblogs.com/liqingdong/p/MyBatis.html
Copyright © 2020-2023  润新知