1.编写持久化对象

public class User {
private String id;//用户编号
private String username;//用户名
private String password;//用户密码
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

2.编写数据协议层

public interface UserDao {
public List<User> getAllUser();//得到所有的用户
}

3.编写数据层

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.UserDao">
<select id="getAllUser" resultType="User">
select * from user_inf
</select>
</mapper>

4.编写工具类

public class SqlSessionFactorytest {
//定义参数
public static SqlSessionFactory sqlSessionFactory=null;
//定义配置文件
public static String resource="mybatis-config.xml";
public static InputStream inputStream;
//构建SesseionFactory
public static SqlSessionFactory getSqlSessionFactoryBuider() {
try {
//读取配置文件
inputStream=Resources.getResourceAsStream(resource);
System.out.println("配置文件加载成功!!");
//SqlSessionFactoryBuilder通过输入流的相关配置得到sqlSessionFactory
}catch(IOException e) {
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
System.out.println("sqlSessionFactory创造成功!!");
return sqlSessionFactory; }
}

这个工具主要是获得SqlSessionFactory

5.编写主类

public class test {
public static void main(String[] args) {
//有工具类获得SqlSessionFactory
SqlSessionFactory sqlSessionFactory=SqlSessionFactorytest.getSqlSessionFactoryBuider();
//由SqlSessionFactoty获得SqlSession
SqlSession sqlSession=sqlSessionFactory.openSession();
//获得Mapper接口
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> user=userDao.getAllUser();
for(int x=0;x<user.size();x++) {
User u=user.get(x);
System.out.println(u.getId());
System.out.println(u.getUsername());
System.out.println(u.getPassword());
}
}
}

最新文章

  1. “此网页上的某个 Web 部件或 Web 表单控件无法显示或导入。找不到该类型,或该类型未注册为安全类型。”
  2. SqlHelper中IN集合场景下的参数处理
  3. [翻译+山寨]Hangfire Highlighter Tutorial
  4. apple配置WIFI热点
  5. 使用SQL Server存储ASP.NET Session变量
  6. JavaWeb项目开发案例精粹-第4章博客网站系统-002辅助类及配置文件
  7. C++ Primer 学习笔记_46_STL实践与分析(20)--容器特有的算法
  8. hdu4893Wow! Such Sequence! (线段树)
  9. 剑指offer第10题
  10. Codeforces Round#309 C Kyoya and Colored Balls
  11. js 验证文本框只能输入数字和小数点
  12. 运行出错之未能加载文件或程序集“Microsoft.ReportViewer.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91”或它的某一个依赖项。系统找不到指定的文件。文件名:“Microsoft.ReportViewer.Common, Version=11.0.0.0,
  13. 自己动手写java 字节流输入输出流
  14. C 指针的几个注意点
  15. Mysql使用规范文档 20180223版
  16. volume_manager.go
  17. Angular系列文章之angular路由
  18. 9.代码抽取(adapter)
  19. MySQL中支持emoji表情的存储
  20. Elasticsearch Java API的基本使用

热门文章

  1. Mybatis:插入数据返回自增主键
  2. 美妙绝伦面向node引用-zico图标(逐浪矢量全真图标)1.9发布
  3. HTTP1.1 Keep-Alive到底算不算长连接?
  4. Python 爬取 豆瓣
  5. 力扣 - 剑指 Offer 52. 两个链表的第一个公共节点
  6. [loj3049]字符串问题
  7. docker 启动报错:Docker.Core.Backend.BackendException: Error response from daemon: open \\.\pipe\docker_e
  8. 区分wsgi、uWSGI、uwsgi、php-fpm、CGI、FastCGI
  9. 【R】爬虫案例
  10. Metabolomics Workfolw