博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis初探
阅读量:7121 次
发布时间:2019-06-28

本文共 2954 字,大约阅读时间需要 9 分钟。

首先下载MyBatis jar包, 可以去MyBatis中文官网下载

项目中导入MyBatis jar包和JDBC jar包(此处用的MySQL)

新建conf.xml

内容如下:

  配置好后写个Student类测试:

1 public class Student { 2     int id; 3     String name; 4     int age; 5     String sex; 6  7     public int getId() { 8         return id; 9     }10 11     public void setId(int id) {12         this.id = id;13     }14 15     public String getName() {16         return name;17     }18 19     public void setName(String name) {20         this.name = name;21     }22 23     public int getAge() {24         return age;25     }26 27     public void setAge(int age) {28         this.age = age;29     }30 31     public String getSex() {32         return sex;33     }34 35     public void setSex(String sex) {36         this.sex = sex;37     }38 39     @Override40     public String toString() {41         return "Student{" +42                 "id=" + id +43                 ", name='" + name + '\'' +44                 ", age=" + age +45                 ", sex='" + sex + '\'' +46                 '}';47     }48 }
View Code

带着toString()一会print方便

同时在数据库中创建student表,并插入几条数据:

1 CREATE TABLE student 2 ( 3     id int PRIMARY KEY, 4     name VARCHAR(20), 5     age INT, 6     sex VARCHAR(4) 7 ) 8 insert into student values(1, '张三', 20, '女'); 9 insert into student values(2, '李四', 21, '女');10 insert into student values(3, '王五', 22, '男');11 insert into student values(4, '马六', 23, '男');
View Code

配置Mapper.xml文件(上方Mapper resource写此文件)

用Mapper方式, 定义接口:

1 import java.util.List;2 3 public interface StudentMapper {4     public Student getOneStudent(int id);5     public List
getAllStudents();6 }
View Code

 

下面编写测试类:

1 import org.apache.ibatis.io.Resources; 2 import org.apache.ibatis.session.SqlSession; 3 import org.apache.ibatis.session.SqlSessionFactory; 4 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 5  6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.List; 9 10 public class Test {11     public static void main(String[] args) throws IOException {12         String resource = "conf.xml";13         InputStream inputStream = Resources.getResourceAsStream(resource);14         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);15         SqlSession session = sqlSessionFactory.openSession();16         StudentMapper studentMapper = session.getMapper(StudentMapper.class);17         Student s = studentMapper.getOneStudent(1);18         List
arrayStudents = studentMapper.getAllStudents();19 System.out.println(s);20 System.out.println(arrayStudents);21 }
View Code

 

测试成功:

 

转载于:https://www.cnblogs.com/sqdtss/p/10098945.html

你可能感兴趣的文章
PgSQL · 最佳实践 · 从 MaxCompute (ODPS) 迁移数据到 HybridDB
查看>>
《iOS 6高级开发手册(第4版)》——2.5节秘诀:Quick Look预览控制器
查看>>
每日Ubuntu小技巧 - 在Ubuntu上面安装VMware Workstation
查看>>
《写给PHP开发者的Node.js学习指南》一2.2 预定义的PHP变量
查看>>
Linux下常用文本处理命令
查看>>
《Spring 5 官方文档》5. 验证、数据绑定和类型转换(二)
查看>>
《像计算机科学家一样思考Python(第2版)》——2.7 注释
查看>>
域名行业将带来高达98亿美元的巨大商机
查看>>
《HTML5+JavaScript动画基础》——2.4 JavaScript对象
查看>>
《JavaScript面向对象精要》——1.2 原始类型
查看>>
《jQuery、jQuery UI及jQuery Mobile技巧与示例》——9.7 技巧:指定过渡动画
查看>>
5 个最受人喜爱的开源 Django 包
查看>>
安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(四)
查看>>
为什么 Cloudera 要创建 Hadoop 安全组件 Sentry ?
查看>>
数学之美:两点之间最快的路径
查看>>
Ansible 详细用法部署安装
查看>>
CPU
查看>>
Spark的这些事<二>——几个概念
查看>>
【知云】第六期:数据级异地容灾如何实现?阿里云帮你打通数据的“任督二脉”...
查看>>
Retrofit2.0使用
查看>>