标签:temp char Opens def void cal 类型 lan out
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
中文官网地址:http://www.mybatis.org/mybatis-3/zh/index.html
GitHub地址:https://github.com/mybatis/mybatis-3/tree/master/src/site
1、准备一个数据库,本例使用的是mysql数据库,建一张员工表(Employee),并且插入一条数据,sql如下:
1 -- ---------------------------- 2 -- Table structure for employee 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `employee`; 5 CREATE TABLE `employee` ( 6 `id` int(11) NOT NULL AUTO_INCREMENT, 7 `last_name` varchar(255) DEFAULT NULL, 8 `gender` char(1) DEFAULT NULL, 9 `email` varchar(255) DEFAULT NULL, 10 PRIMARY KEY (`id`) 11 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 12 13 -- ---------------------------- 14 -- Records of employee 15 -- ---------------------------- 16 BEGIN; 17 INSERT INTO `employee` VALUES (1, ‘test‘, ‘1‘, ‘test@163.com‘); 18 COMMIT;
2、新建一个Maven工程,引入mybatis依赖以及连接mysql数据库依赖
1 <!-- Mybatis --> 2 <dependency> 3 <groupId>org.mybatis</groupId> 4 <artifactId>mybatis</artifactId> 5 <version>3.4.6</version> 6 </dependency> 7 8 <!-- mysql --> 9 <dependency> 10 <groupId>mysql</groupId> 11 <artifactId>mysql-connector-java</artifactId> 12 <version>8.0.13</version> 13 </dependency>
3、添加Mybatis全局配置文件mybatis-config.xml,放在src/main/resources目录下,内容如下:
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 <!-- 配置数据库连接信息 --> 10 <dataSource type="POOLED"> 11 <property name="driver" value="com.mysql.jdbc.Driver" /> 12 <property name="url" value="jdbc:mysql://localhost:3306/test_mybatis?allowPublicKeyRetrieval=true" /> 13 <property name="username" value="admin" /> 14 <property name="password" value="123456" /> 15 </dataSource> 16 </environment> 17 </environments> 18 <mappers> 19 <!-- 添加映射文件到Mybatis的全局配置文件中 --> 20 <mapper resource="mapper/EmployeeMapper.xml" /> 21 </mappers> 22 </configuration>
4、添加映射文件EmployeeMapper.xml到src/main/resources/mapping目录下,内容如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- 4 namespace:名称空间 5 id:唯一标识 6 resultType:返回值类型 7 #{id}:从传过来的参数中取出id值 8 --> 9 <mapper namespace="com.hd.test.mapper.EmployeeMapper"> 10 <select id="getEmployeeById" 11 resultType="com.hd.test.pojo.Employee"> 12 select id, last_name lastName, gender, email from employee where id = 13 #{id} 14 </select> 15 </mapper>
5、定义一个Java类Employee.java
1 package com.hd.test.pojo; 2 3 public class Employee { 4 5 private Integer id; 6 private String lastName; 7 private String gender; 8 private String email; 9 public Integer getId() { 10 return id; 11 } 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 public String getLastName() { 16 return lastName; 17 } 18 public void setLastName(String lastName) { 19 this.lastName = lastName; 20 } 21 public String getGender() { 22 return gender; 23 } 24 public void setGender(String gender) { 25 this.gender = gender; 26 } 27 public String getEmail() { 28 return email; 29 } 30 public void setEmail(String email) { 31 this.email = email; 32 } 33 @Override 34 public String toString() { 35 return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]"; 36 } 37 38 39 }
8、一定一个测试Mybatis的Java类TestMybatis.java
1 package com.hd.test.mybatis; 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.hd.test.pojo.Employee; 13 14 15 public class TestMybatis { 16 17 @Test 18 public void test() throws IOException { 19 // 1、根据mybatis全局配置文件,获取SqlSessionFactory 20 String resource = "mybatis-config.xml"; 21 // 使用MyBatis提供的Resources类加载mybatis的配置文件,获取输入流 22 InputStream inputStream = Resources.getResourceAsStream(resource); 23 // 构建sqlSession的工厂 24 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 25 26 // 2、从SqlSession工厂中,获取sqlsession,用来执行sql 27 SqlSession session = sqlSessionFactory.openSession(); 28 try { 29 // 查询selectOne 30 // @param statement Unique identifier matching the statement to use. 一个唯一标识 31 // @param parameter A parameter object to pass to the statement. 参数 32 Employee employee = (Employee) session.selectOne("com.hd.test.mapper.EmployeeMapper.getEmployeeById", 1); 33 // 输出信息 34 System.out.println(employee); 35 } finally { 36 // 关闭session 37 session.close(); 38 } 39 } 40 41 }
8、junit测试方法,输出结果:
1、在上例的基础上编写一个Java接口EmployeeMapper
package com.hd.test.mapper; import com.hd.test.pojo.Employee; public interface EmployeeMapper { public Employee getEmployeeById(Integer id); }
注意,sql配置文件要与接口一一对应
2、编辑测试方法内容如下:
1 @Test 2 public void test() throws IOException { 3 // 1、根据mybatis全局配置文件,获取SqlSessionFactory 4 String resource = "mybatis-config.xml"; 5 // 使用MyBatis提供的Resources类加载mybatis的配置文件,获取输入流 6 InputStream inputStream = Resources.getResourceAsStream(resource); 7 // 构建sqlSession的工厂 8 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 9 10 // 2、从SqlSession工厂中,获取sqlsession,用来执行sql 11 SqlSession session = sqlSessionFactory.openSession(); 12 try { 13 // 3、获取接口的实现对象 14 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); 15 Employee employee = mapper.getEmployeeById(1); 16 // 输出信息 17 System.out.println(mapper); 18 System.out.println(employee); 19 } finally { 20 // 关闭session 21 session.close(); 22 } 23 }
3、junit测试方法,输出结果:
标签:temp char Opens def void cal 类型 lan out
原文地址:https://www.cnblogs.com/h--d/p/10163074.html