MyBatis

MyBatis 是一个可以自定义 SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的 JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的 XML 和注解来配置和映射基本数据类型 、Map 接口和 POJO 到数据库记录(MyBatis开发文档)

1、MyBatis的“Helloword”

  1. 建立数据表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`) );

  2. 创建Customer类(添加对应的set/get方法、无参构造器、toString()方法等)
    package org.mybatis.example;
    
    public class Customer {
    
    	private int id;
    	private String lastName;
    	private String email;
    	private int age;
  3. 在Customer类的同一个包中创建CustomerMapper.xml,内容如下:
  4. <span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="UTF-8" ?> </span>
  5. <!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>
  6. 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>
     
  7. 创建测试类CustomerTest
     
  8. 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>
  9. 基于MyBatis注解的方式,首先建立CustomerMapper
     
  10. 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>
  11. 再者,在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);
    }

     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值