1 maven配置:pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>MybatisTest</groupId> 5 <artifactId>MybatisTest-Demo1</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>MybatisTest-Demo1 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 18 <!--mybatis--> 19 <dependency> 20 <groupId>org.mybatis</groupId> 21 <artifactId>mybatis</artifactId> 22 <version>3.4.1</version> 23 </dependency> 24 25 <!--mysql driver--> 26 <dependency> 27 <groupId>mysql</groupId> 28 <artifactId>mysql-connector-java</artifactId> 29 <version>5.1.6</version> 30 </dependency> 31 32 <dependency> 33 <groupId>junit</groupId> 34 <artifactId>junit</artifactId> 35 <version>RELEASE</version> 36 </dependency> 37 38 39 </dependencies> 40 <build> 41 <finalName>MybatisTest-Demo1</finalName> 42 </build> 43 </project>
2 实体类:Employee.java
1 package com.mybatis.model; 2 3 /** 4 * Created by wanggenshen_sx on 2016/12/23. 5 */ 6 public class Employee { 7 8 private int id; 9 private String lastName; 10 private String email; 11 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getLastName() { 19 return lastName; 20 } 21 public void setLastName(String lastName) { 22 this.lastName = lastName; 23 } 24 public String getEmail() { 25 return email; 26 } 27 public void setEmail(String email) { 28 this.email = email; 29 } 30 @Override 31 public String toString() { 32 return "Employee [id=" + id + ", lastName=" + lastName + ", email=" 33 + email+" ]" ; 34 } 35 }
3 EmployeeMapper接口:
1 package com.mybatis.mapper; 2 3 import com.mybatis.model.Employee; 4 import org.apache.ibatis.annotations.Select; 5 6 import java.util.List; 7 8 /** 9 * Created by wanggenshen_sx on 2016/12/26. 10 */ 11 public interface EmployeeMapper { 12 @Select("select * from employee where id=#{id}") 13 public Employee getUserById(int id); 14 15 @Select("select * from employee") 16 public List<Employee> getAllUsers(); 17 }
4 工具类
1 package com.mybatis.dao; 2 3 import com.mybatis.test.EmployeeTest; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 6 7 import java.io.InputStream; 8 9 /** 10 * Created by wanggenshen_sx on 2016/12/26. 11 * 获得SqlSessionFactory的工具类 12 */ 13 public class MybatisUtils { 14 public static SqlSessionFactory getSqlSessionFactory(){ 15 String resource = "mybatis.xml"; 16 //1 加载mybatis的配置文件,也加载关联的映射文件 17 InputStream is=EmployeeTest.class.getClassLoader().getResourceAsStream(resource); 18 19 //2 构建SqlSession工厂 20 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); 21 22 return sessionFactory; 23 } 24 }
5 测试
1 package com.mybatis.test; 2 3 import com.mybatis.dao.MybatisUtils; 4 import com.mybatis.mapper.EmployeeMapper; 5 import com.mybatis.model.Employee; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.Test; 10 11 import java.io.InputStream; 12 13 /** 14 * Created by wanggenshen_sx on 2016/12/26. 15 */ 16 public class EmployeeTest { 17 /** 18 * 根据一个id得到一条用户信息 19 */ 20 @Test 21 public void testGetUser(){ 22 SqlSession session = MybatisUtils.getSqlSessionFactory().openSession(); 23 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 24 25 Employee employee = mapper.getUserById(1); 26 System.out.println("*"+employee); 27 session.close(); 28 } 29 }
6 mybatis配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <environments default="development"> 6 <environment id="development"> 7 <transactionManager type="JDBC" /> 8 <dataSource type="POOLED"> 9 <property name="driver" value="com.mysql.jdbc.Driver" /> 10 <property name="url" value="jdbc:mysql://localhost:3306/mytest" /> 11 <property name="username" value="root" /> 12 <property name="password" value="920614" /> 13 </dataSource> 14 </environment> 15 </environments> 16 17 <!-- 注册userMapper.xml文件 --> 18 <mappers> 19 <mapper class="com.mybatis.mapper.EmployeeMapper"/> 20 </mappers> 21 22 </configuration>
结构: