使用SqlSession手动查询

使用SqlSession手动查询

mybatis框架在操作数据的时候,离不开SqlSession接口实例类的作用。可以说SqlSession接口实例是开发过程中打交道最多的一个类。即是DefaultSqlSession类

public static void main(String[] args) throws FileNotFoundException {

    String strFilePath = Thread.currentThread().getContextClassLoader().getResource("mybatisconfig.xml").toString();
    strFilePath = strFilePath.replace("file:/", "");
    FileInputStream inputStream = new FileInputStream(strFilePath);
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession session = factory.openSession();

    //第一种方式
    //使用SqlSession直接操作,
    //参数是nameSpace+节点(select,update等)id
    //此时是使用Executor 来实现后续的处理
    User userDirectBySession = session.selectOne("gupaoIT.dao.UserMapper.getUserById", 1);



    //第二种方式,直接调用Mapper,
    // 使用mapper执行后续操作, 此处是使用动态代理实现的
    //最后调用MapperMethod.execute实现数据库交互
    UserMapper mapper = session.getMapper(UserMapper.class);
    User u = mapper.getUserById(1);

    //第三种,使用ProxyFactory 自己模拟调用ProxyMapper
    MapperProxyFactory factory1 = new MapperProxyFactory(UserMapper.class);
    UserMapper userMapperT = (UserMapper) factory1.newInstance(session);
    User user1 = userMapperT.getUserById(2);


}

辅助代码:

public interface UserMapper {
    User getUserById(int id);
}


public class User {
    private int id;
    private String name;
    private String age;
    private String sex;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}

Mapper.xml



<mapper namespace="gupaoIT.dao.UserMapper">
    <select id="getUserById" resultType="gupaoIT.dao.User">
        SELECT * from User where id= #{id}
    select>
mapper>

mybatisconfig.xml



<configuration>
    <properties>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://x.x.x.x:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            dataSource>
        environment>
    environments>

    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    mappers>
configuration>

名词解释:
coco: 约定大于声明
myBatis中xml 和注解方式可以同时使用,xml的优势是配置动态语句比较方便,可视性比较好, 但是没有语法检查。
注解:有语法检查,书写复杂语句时可视性差
xml和注解同时使用时时相互补充的, 不存在优先级,但必须保证xml和注解中不会出现相同的ID

你可能感兴趣的:(使用SqlSession手动查询)