MyBatis 是一个可以自定义 SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的 JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的 XML 和注解来配置和映射基本数据类型 、Map 接口和 POJO 到数据库记录(MyBatis开发文档)
1、MyBatis的“Helloword”
- 建立数据表customers
CREATE TABLE `mybatis_study`.`Customer`( `id` INT(10) NOT NULL AUTO_INCREMENT, `last_name` VARCHAR(30), `email` VARCHAR(30), `age` INT(5), PRIMARY KEY (`id`) );
- 创建Customer类(添加对应的set/get方法、无参构造器、toString()方法等)
package org.mybatis.example; public class Customer { private int id; private String lastName; private String email; private int age;
在Customer类的同一个包中创建CustomerMapper.xml,内容如下:
<span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="UTF-8" ?> </span>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 指定映射器的命名空间 --> <mapper namespace="org.mybatis.example.CustomerMapper"> <!-- 映射 SQL 语句 查询--> <select id="selectCustomer" resultType="org.mybatis.example.Customer"> select id,last_name lastName,email,age from customers where id = #{id} </select> <!-- 插入 --> <insert id="insertCustomer" parameterType="org.mybatis.example.Customer" useGeneratedKeys="true" keyProperty="id"> insert into customers(last_name,email,age) values(#{lastName},#{email},#{age}) </insert> <!-- 更新 --> <update id="updateCustomer" parameterType="org.mybatis.example.Customer"> update customers set last_name = #{lastName}, email = #{email}, age = #{age} where id = #{id} </update> <!-- 删除 --> <delete id="deleteCustomer" parameterType="int"> delete from customers where id = #{id} </delete> </mapper></span>
src目录下创建Configuration.xml,内容如下:<?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/mybatis_study" /> <property name="username" value="root" /> <property name="password" value="mysql" /> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/CustomerMapper.xml" /> </mappers> </configuration></span>
创建测试类CustomerTest
private SqlSession openSession; @Before public void testBefore() throws IOException { String resource = "Configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(reader); openSession = sqlSessionFactory.openSession(); } @Test public void testDelete(){ int deleteSuccess = openSession.delete("org.mybatis.example.CustomerMapper.deleteCustomer", 1); openSession.commit(true); System.out.println(deleteSuccess); } @Test public void testUpdate(){ Customer customer = new Customer(); customer.setId(5); customer.setLastName("xiaohong"); customer.setEmail("xiaohong@126.com"); customer.setAge(18); int updateSuccess = openSession.update("org.mybatis.example.CustomerMapper.updateCustomer", customer); openSession.commit(true); System.out.println(updateSuccess); } @Test public void testInsert() { Customer customer = new Customer(); customer.setLastName("Lisi"); customer.setEmail("lisi@163.com"); customer.setAge(25); int insertSuccess = openSession.insert("org.mybatis.example.CustomerMapper.insertCustomer",customer); openSession.commit(true); System.out.println(insertSuccess); } @Test public void testSelect() { Customer customer = openSession.selectOne( "org.mybatis.example.CustomerMapper.selectCustomer", 1); System.out.println(customer); } @After public void testAfter() { openSession.close(); }</span>
基于MyBatis注解的方式,首先建立CustomerMapper
package org.mybatis.example.mapper; import org.apache.ibatis.annotations.Select; import org.mybatis.example.Customer; public interface CustomerMapper { @Select("select id,last_name lastName,email,age from customers where last_name = #{lastName}") public Customer getCustomerByLastName(String lastName); }</span>
在测试类中测试再者,在Configuration.xml 的<mappers>中加入<mapper class="org.mybatis.example.mapper.CustomerMapper"/>
@Test public void testSelectAnnotation(){ CustomerMapper customerMapper = openSession.getMapper(CustomerMapper.class); Customer customer = customerMapper.getCustomerByLastName("xiaohong"); System.out.println(customer); }