美文网首页
Mybait入门

Mybait入门

作者: ManrayHsu | 来源:发表于2021-01-12 19:02 被阅读0次

    Main

    public static void main(String[] args) throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                StudentMapper mapper = session.getMapper(StudentMapper.class);
                Student student = mapper.selectStudent(1);
                System.out.println(student.toString());
            }
        }
    

    mybatis-config.xml

    <configuration>
        <!--引用外部配置文件-->
        <properties resource="mybatis.properties"/>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper class="com.frank.mybatis.xml.StudentMapper"/>
        </mappers>
    </configuration>
    

    mybatis.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/test
    jdbc.username=root
    jdbc.password=123456
    

    相关文章

      网友评论

          本文标题:Mybait入门

          本文链接:https://www.haomeiwen.com/subject/tcmeaktx.html