Mybatis用接口的方式搭建

小编用的是IDEA2017.2  Maven环境

 

1.项目依赖的jar包

 1 <!--mybatis需要的依赖-->
 2     <dependency>
 3       <groupId>org.mybatis</groupId>
 4       <artifactId>mybatis</artifactId>
 5       <version>3.4.1</version>
 6     </dependency>
 7 
 8     <!--MySQL需要的依赖-->
 9     <dependency>
10       <groupId>mysql</groupId>
11       <artifactId>mysql-connector-java</artifactId>
12       <version>5.1.38</version>
13     </dependency>
14 
15     <!-- 日志文件管理包 -->
16     <dependency>
17       <groupId>log4j</groupId>
18       <artifactId>log4j</artifactId>
19       <version>1.2.17</version>
20     </dependency>
21     <dependency>
22       <groupId>org.slf4j</groupId>
23       <artifactId>slf4j-api</artifactId>
24       <version>1.7.12</version>
25     </dependency>
26     <dependency>
27       <groupId>org.slf4j</groupId>
28       <artifactId>slf4j-log4j12</artifactId>
29       <version>1.7.12</version>
30     </dependency>
pom.xml

2.存储值的接口

1 package com.mozi.bean;
2 
3 public interface EmployeeMapper {
4 
5      public Employee getEmpById(Integer id);
6 }
EmployeeMapper

3.bean

 1 package com.mozi.bean;
 2 
 3 public class Employee {
 4 
 5     private Integer id;
 6     private String lastName;
 7     private  String email;
 8     private String gender;
 9 
10     public  Employee(){}
11 
12     @Override
13     public String toString() {
14         return "Employee{" +
15                 "id=" + id +
16                 ", lastName='" + lastName + '\'' +
17                 ", email='" + email + '\'' +
18                 ", gender='" + gender + '\'' +
19                 '}';
20     }
21 
22     public Employee(Integer id, String lastName, String email, String gender) {
23         this.id = id;
24         this.lastName = lastName;
25         this.email = email;
26         this.gender = gender;
27     }
28 
29     public Integer getId() {
30 
31         return id;
32     }
33 
34     public void setId(Integer id) {
35         this.id = id;
36     }
37 
38     public String getLastName() {
39         return lastName;
40     }
41 
42     public void setLastName(String lastName) {
43         this.lastName = lastName;
44     }
45 
46     public String getEmail() {
47         return email;
48     }
49 
50     public void setEmail(String email) {
51         this.email = email;
52     }
53 
54     public String getGender() {
55         return gender;
56     }
57 
58     public void setGender(String gender) {
59         this.gender = gender;
60     }
61 }
Employee

4.bean的映射

注意namespace是接口的路径加类名,id要改为接口中实现id的方法

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <!--
 7    namespace:名称空间;指定为接口的全类名
 8    id:唯一标识
 9    resultType:返回值类型
10    #{id} 从传递过来的参数中取出id值
11 
12 -->
13 <mapper namespace="com.mozi.bean.EmployeeMapper">
14     <select id="getEmpById" resultType="com.mozi.bean.Employee">
15         select id,last_name lastName,email,gender from table_mybatis where id = #{id}
16     </select>
17 </mapper>
EmployeeMapper.xml

5.全局映射文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <environments default="development">
 7         <environment id="development">
 8             <transactionManager type="JDBC"/>
 9             <dataSource type="POOLED">
10                 <property name="driver" value="com.mysql.jdbc.Driver"/>
11                 <property name="url" value="jdbc:mysql://localhost:3306/mybtis"/>
12                 <property name="username" value="root"/>
13                 <property name="password" value=""/>
14             </dataSource>
15         </environment>
16     </environments>
17     <mappers>
18         <mapper resource="EmployeeMapper.xml"/>
19     </mappers>
20 </configuration>
mybatis.xml

6.测试类

 1 public void test2() throws IOException {
 2              String resource = "mybatis.xml";
 3              InputStream inputStream = Resources.getResourceAsStream(resource);
 4              SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 5              SqlSession openSession = sqlSessionFactory.openSession();
 6              try {
 7                  EmployeeMapper employeeMapper = openSession.getMapper(EmployeeMapper.class);
 8                  Employee employee = employeeMapper.getEmpById(1);
 9                  System.out.println(employee);
10              }finally {
11                  openSession.close();
12              }
13         }
Test

 

posted @ 2017-09-14 16:31  Limo1996  阅读(210)  评论(0)    收藏  举报