来宾住房与城乡建设网站,泰安网络安装,江苏省电力建设质量监督中心站网站,淄博亿泰前面一章#xff0c;已经搭建好了eclipse,mybatis,mysql的环境#xff0c;并且实现了一个简单的查询。请注意#xff0c;这种方式是用SqlSession实例来直接执行已映射的SQL语句#xff1a;session.selectOne(com.yihaomen.mybatis.models.UserMapper.selectUserByID已经搭建好了eclipse,mybatis,mysql的环境并且实现了一个简单的查询。请注意这种方式是用SqlSession实例来直接执行已映射的SQL语句session.selectOne(com.yihaomen.mybatis.models.UserMapper.selectUserByID, 1)其实还有更简单的方法而且是更好的方法使用合理描述参数和SQL语句返回值的接口比如IUserOperation.class这样现在就可以至此那个更简单更安全的代码没有容易发生的字符串文字和转换的错误.下面是详细过程:在src_user源码目录下建立 com.yihaomen.mybatis.inter 这个包并建立接口类 IUserOperation , 内容如下 程序代码package com.yihaomen.mybatis.inter;import com.yihaomen.mybatis.model.User;public interface IUserOperation { public User selectUserByID(int id); }请注意这里面有一个方法名 selectUserByID 必须与 User.xml 里面配置的 select 的id 对应select idselectUserByID重写测试代码 程序代码public static void main(String[] args) { SqlSession session sqlSessionFactory.openSession(); try { IUserOperation userOperationsession.getMapper(IUserOperation.class); User user userOperation.selectUserByID(1); System.out.println(user.getUserAddress()); System.out.println(user.getUserName()); } finally { session.close(); } }整个工程结构图现在如下运行这个测试程序就可以看到结果了。来源 http://www.yihaomen.com/article/java/304.htm来自为知笔记(Wiz)转载于:https://www.cnblogs.com/jeffen/p/6236302.html