MyBatis入门案例-HelloWorld

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/mybatis" /> 12 <property name="username" value="root" /> 13 <property name="password" value="123456" /> 14 </dataSource> 15 </environment> 16 </environments> 17 <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --> 18 <mappers> 19 <mapper resource="EmployeeMapper.xml" /> 20 </mappers> 21 </configuration>
1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
![]()
![]()
2、获取sqlSession实例,能直接执行已经映射的sql语句
![]()
第一个参数:sql的唯一标识:statement Unique identifier matching the statement to use.
第二个参数:执行sql要用的参数:parameter A parameter object to pass to the statement.
namespace:名称空间;指定为接口的全类名
id:唯一标识 方法名
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
![]()


![]()

![]()
Employee.java
1 package com.atguigu.mybatis.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 Integer getId() { 11 return id; 12 } 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 public String getLastName() { 17 return lastName; 18 } 19 public void setLastName(String lastName) { 20 this.lastName = lastName; 21 } 22 public String getEmail() { 23 return email; 24 } 25 public void setEmail(String email) { 26 this.email = email; 27 } 28 public String getGender() { 29 return gender; 30 } 31 public void setGender(String gender) { 32 this.gender = gender; 33 } 34 @Override 35 public String toString() { 36 return "Employee [id=" + id + ", lastName=" + lastName + ", email=" 37 + email + ", gender=" + gender + "]"; 38 } 39 }
mybatis-config.xml
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/mybatis" /> 12 <property name="username" value="root" /> 13 <property name="password" value="root" /> 14 </dataSource> 15 </environment> 16 </environments> 17 <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --> 18 <mappers> 19 <mapper resource="EmployeeMapper.xml" /> 20 </mappers> 21 </configuration>
EmployeeMapper.java
1 package com.atguigu.mybatis.dao; 2 3 import com.atguigu.mybatis.bean.Employee; 4 5 public interface EmployeeMapper { 6 7 public Employee getEmpById(Integer id); 8 9 }
EmployeeMapper.xml
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 <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper"> 6 7 <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee"> 8 select id,last_name lastName,email,gender from tb1_employee where id = #{id} 9 </select> 10 11 </mapper>

MyBatisTest.java
1 package com.atguigu.mybatis.test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 import com.atguigu.mybatis.bean.Employee; 13 import com.atguigu.mybatis.dao.EmployeeMapper; 14 15 public class MyBatisTest { 16 17 18 public SqlSessionFactory getSqlSessionFactory() throws IOException { 19 String resource = "mybatis-config.xml"; 20 InputStream inputStream = Resources.getResourceAsStream(resource); 21 return new SqlSessionFactoryBuilder().build(inputStream); 22 } 23 24 @Test 25 public void test() throws IOException { 26 27 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 28 29 SqlSession openSession = sqlSessionFactory.openSession(); 30 try { 31 Employee employee = openSession.selectOne( 32 "com.atguigu.mybatis.dao.EmployeeMapper.getEmpById", 1); 33 System.out.println(employee); 34 } finally { 35 openSession.close(); 36 } 37 38 } 39 40 @Test 41 public void test01() throws IOException { 42 // 1、获取sqlSessionFactory对象 43 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 44 // 2、获取sqlSession对象 45 SqlSession openSession = sqlSessionFactory.openSession(); 46 try { 47 // 3、获取接口的实现类对象 48 //会为接口自动的创建一个代理对象,代理对象去执行增删改查方法 49 EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); 50 Employee employee = mapper.getEmpById(1); 51 System.out.println(mapper.getClass()); 52 System.out.println(employee); 53 } finally { 54 openSession.close(); 55 } 56 } 57 58 }







浙公网安备 33010602011771号