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