我们先来看一个例子,简单的了解一下mybatis的mapper接口方式的使用。

 1 package org.mybatis.spring.sample;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 import org.junit.Test;
 8 import org.mybatis.spring.sample.bean.User;
 9 import org.mybatis.spring.sample.mapper.UserMapper;
10 
11 import java.io.IOException;
12 
13 public class MybatisTest {
14 
15     /**
16      * 读取mybatis的配置文件,生成SqlSessionFactory
17      *
18      * @return
19      */
20     private static SqlSessionFactory getSessionFactory() {
21         SqlSessionFactory sessionFactory = null;
22         String resource = "mybatisConfig.xml";
23         try {
24             sessionFactory = new SqlSessionFactoryBuilder().build(Resources
25                     .getResourceAsReader(resource));
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29         return sessionFactory;
30     }
31 
32     @Test
33     public void findUserById() {
34         SqlSessionFactory sqlSessionFactory = getSessionFactory();
35         SqlSession sqlSession = sqlSessionFactory.openSession();
36         UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
37         User user = userMapper.selectByPrimaryKey(1l);
38         System.out.println(user.getId() + " /  " + user.getName());
39     }
40 
41 }

输出结果

1 /  赵大

 

数据库表 user

MyBatis框架的使用及源码分析(一)     配置与使用

 

User.java

  1 /*
  2  * User.java
  3  * Copyright(C) 2015-2017 Jstudio.org
  4  * All rights reserved.
  5  * --------------------------------------
  6  * 2017-09-17 Created.
  7  */
  8 package org.mybatis.spring.sample.bean;
  9 
 10 import java.io.Serializable;
 11 
 12 /**
 13  *
 14  * This class was generated by MyBatis Generator.
 15  * This class corresponds to the database table user
 16  *
 17  * @mbg.generated do_not_delete_during_merge 2017-09-17
 18  */
 19 public class User implements Serializable {
 20     /**
 21      * 主键
 22      */
 23     private Long id;
 24 
 25     /**
 26      * 用户名
 27      */
 28     private String name;
 29 
 30     /**
 31      * 密码
 32      */
 33     private String password;
 34 
 35     /**
 36      * 电子邮件
 37      */
 38     private String email;
 39 
 40     /**
 41      * 年龄
 42      */
 43     private Integer age;
 44 
 45     private static final long serialVersionUID = 1L;
 46 
 47     /**
 48      * 获取主键
 49      *
 50      * @return id - 主键
 51      */
 52     public Long getId() {
 53         return id;
 54     }
 55 
 56     /**
 57      * 设置主键
 58      *
 59      * @param id 主键
 60      */
 61     public void setId(Long id) {
 62         this.id = id;
 63     }
 64 
 65     /**
 66      * 获取用户名
 67      *
 68      * @return name - 用户名
 69      */
 70     public String getName() {
 71         return name;
 72     }
 73 
 74     /**
 75      * 设置用户名
 76      *
 77      * @param name 用户名
 78      */
 79     public void setName(String name) {
 80         this.name = name == null ? null : name.trim();
 81     }
 82 
 83     /**
 84      * 获取密码
 85      *
 86      * @return password - 密码
 87      */
 88     public String getPassword() {
 89         return password;
 90     }
 91 
 92     /**
 93      * 设置密码
 94      *
 95      * @param password 密码
 96      */
 97     public void setPassword(String password) {
 98         this.password = password == null ? null : password.trim();
 99     }
100 
101     /**
102      * 获取电子邮件
103      *
104      * @return email - 电子邮件
105      */
106     public String getEmail() {
107         return email;
108     }
109 
110     /**
111      * 设置电子邮件
112      *
113      * @param email 电子邮件
114      */
115     public void setEmail(String email) {
116         this.email = email == null ? null : email.trim();
117     }
118 
119     /**
120      * 获取年龄
121      *
122      * @return age - 年龄
123      */
124     public Integer getAge() {
125         return age;
126     }
127 
128     /**
129      * 设置年龄
130      *
131      * @param age 年龄
132      */
133     public void setAge(Integer age) {
134         this.age = age;
135     }
136 }
View Code

相关文章:

  • 2021-09-08
  • 2021-10-14
  • 2021-07-18
  • 2021-06-04
  • 2021-06-04
  • 2021-06-03
  • 2022-12-23
  • 2020-02-06
猜你喜欢
  • 2021-07-15
  • 2021-10-24
  • 2021-12-06
  • 2021-10-21
  • 2021-12-12
  • 2021-10-27
  • 2021-07-25
相关资源
相似解决方案