Giter VIP home page Giter VIP logo

my_orm's Introduction

自定义持久层框架

此项目是Mybatis的学习项目,只是用来了解加深对Mybatis的了解的,仅简单实现了Mybatis。具体内容见我的学习笔记

使用方法如下:

resources下编写SqlMapConfig.xml配置文件

<configuration>
<!--    配置数据源-->
    <dataSource>
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </dataSource>
<!--    存放mapper.xml的全路径-->
    <mapper resource="UserMapper.xml"></mapper>

</configuration>

resources下编写UserMapper.xml配置文件

<mapper namespace="user">
<!--    sql的唯一标识:namespace和id组成  :statementId -->
    <select id="selectList" resultType="com.iorm.pojo.User" >
        select * from user
    </select>

<!--    User user = new User()
        user.setId(1);
-->
    <select id ="selectOne" resultType="com.iorm.pojo.User" paramType="com.iorm.pojo.User">
        select * from user where id = #{id} and username = #{username}
    </select>
</mapper>

测试方法的编写

/**
 * 测试类 测试orm框架
 */
public class Test1 {

    @Test
    public void test() throws PropertyVetoException, DocumentException, SQLException, IntrospectionException, NoSuchFieldException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
        //测试第一版orm
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSqlSession();

        User user = new User();
        user.setId(1);
        user.setUsername("张老师");
//        测试单个
//        User userSelect = sqlSession.selectOne("user.selectOne",user);
//        System.out.println(userSelect.toString());
//        测试所有
//        List<Object> selectList = sqlSession.selectList("user.selectList");
//        selectList.stream().forEach(System.out::println);
//        测试代理
        //测试第二版
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> all = userDao.findAll();
        if (all == null){
            System.out.println("all为null");
        }else {
            System.out.println(all.size());
        }
        all.stream().forEach(System.out::println);
        User byCondition = userDao.findByCondition(user);
        System.out.println(byCondition);
    }
}

my_orm's People

Contributors

loserfromlazy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.