mybatis

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>
    <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"/>
                <property name="username" value="root"/>
                <property name="password" value="yto123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/User.xml"/>
        <mapper resource="mapper/Us.xml"/>
    </mappers>

</configuration>

2.User.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="user">
    <select id="select" parameterType="string" resultType="string">
    select age
    from user
    where name = #{name,jdbcType=VARCHAR}
  </select>
</mapper>

3.生成sqlSession

String resource = "mybatis-config.xml";
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(resource);     //获取配置文件
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(inputStream).openSession();  //通过SqlSessionFactoryBuilder得到SqlSessionFactry在得到SqlSession
String s = sqlSession.selectOne("user1.select","1");
System.out.println(s);

 

posted @ 2019-07-05 14:22  霍宇飞  阅读(271)  评论(0编辑  收藏  举报