16boke - 一路博客

MyBatis学习1---初次接触

MyBatis是一种轻量级的ORM框架,相对于Hibernate来说比较简单易用,对于较小的系统或者对于SQL语句有独特爱好的朋友可以使用MyBatis进行开发。网上大多是直接照抄MyBatis官网提供的docs例子进行粘贴,具体能不能用还不得而知。下面以一个例子来介绍MyBatis的基本使用。

一、建立数据库表

创建用户表:

DROP TABLE IF EXISTS user;
CREATE TABLE user (
  username varchar(50) NOT NULL,
  password varchar(50) DEFAULT NULL,
  address varchar(50) DEFAULT NULL,
  age varchar(5) DEFAULT NULL,
  sex varchar(2) DEFAULT NULL,
  PRIMARY KEY (username)
)


二、建立对应的DTO对象

public class UserDto implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private String address;
	private String age;
	private String sex;

	public UserDto() {
	}

	public UserDto(String username, String password, String address, String age, String sex) {
		this.username = username;
		this.password = password;
		this.address = address;
		this.age = age;
		this.sex = sex;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAge() {
		return this.age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return this.sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}


三、建立MyBatis系统配置文件[mybatis-config.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://127.0.0.1:3306/test" />
				<property name="username" value="test" />
				<property name="password" value="test" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="UserMapper.xml" />
	</mappers>
</configuration>


四、建立user表的映射文件(UserMapper.xml)

select * from user where username = #{username}

五、建立映射接口类(如果sql语句写在映射的配置XML文件,这一步可以不用)

package com.mapper;

import org.apache.ibatis.annotations.Select;

import com.dto.UserDto;

public interface UserMapper {
	@Select("select * from user where username = #{username}")
	public UserDto seleteUser(String username);

}


六、建立测试类

package com.test;


import java.io.InputStream;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.dto.UserDto;
import com.mapper.UserMapper;

public class UserTest {
	SqlSessionFactory sqlSessionFactory = null;
	@Before
	public void setUp() throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		Configuration configuration = sqlSessionFactory.getConfiguration();
		MapperRegistry mapperRegistry = configuration.getMapperRegistry();
		System.out.println(mapperRegistry.getMappers());
		
	}
	@Test
	public void seleteXMLTest(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
		  UserDto userDto = (UserDto) session.selectOne("com.mapper.UserMapper.selectUser", "xyz");
		  System.out.println(userDto.getUsername());
		  System.out.println(userDto.getPassword());
		  System.out.println(userDto.getAddress());
		  System.out.println(userDto.getAge());
		  System.out.println(userDto.getSex());
		} finally {
		  session.close();
		}
	}
	@Test
	public void seleteAnnotaionTest(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserMapper userMapper = session.getMapper(UserMapper.class);
			UserDto userDto = userMapper.seleteUser("xyz");
			System.out.println(userDto.getUsername());
			System.out.println(userDto.getPassword());
			System.out.println(userDto.getAddress());
			System.out.println(userDto.getAge());
			System.out.println(userDto.getSex());
		} finally {
			session.close();
		}
	}

}