java学习笔记1 - Mybatis简单实例

写在前面

2011年6月iBatis 更名为 MyBatis,从 iBatis 到 MyBatis,不只是名称上的变化,MyBatis 提供了更为强大的功能,同时并没有损失其易用性,相反,在很多地方都借助于 JDK 的泛型和注解特性进行了简化。所以你懂的,开始学习就从MyBatis吧.


例子使用MyBatis+MySQL实现一个用户表的查询,如下:

①数据库 在MySQL中,test数据库下,简历user表,字段:id,name,password,建表语句略。 

②需导入的包 只有两个: mybatis-3.0.3.jar mysql-connector-java-5.1.16-bin.jar(JDBC包) 

③目录结构 采用最简单的结构,com.mybatis包下有且仅有四个文件 configuration.xml user.xml User.java Test.java 

java学习笔记1 - Mybatis简单实例_第1张图片

④mybatis配置文件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>
	<typeAliases>
		<typeAlias alias="User" type="com.mybatis.User"></typeAlias>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="user.xml"/>
	</mappers>

</configuration>

⑤User表sql文件User.xml,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="User">
	<select id="selectUser" parameterType="int" resultType="User">
		SELECT * FROM user WHERE id = #{id}
	</select>
	
	<select id="selectUsers" resultType="User">
		SELECT * FROM user
	</select>
	
</mapper>


⑥表结构文件User.java,

package com.mybatis;
public class User {
	private int id;
	private String name;
	private String password;
	
	public User(){}
	public User(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return this.password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "User [id=" + this.id + ", name=" + this.name + ", password=" + this.password + "]";
	}
	
}


⑦测试用例Test.java

package com.mybatis;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {
	public static void main(String[] args) throws IOException {
		String resource = "configuration.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = ssf.openSession();
		try{
			User user = session.selectOne("selectUser", "1");
			System.out.println(user.getName());
			System.out.println(user);
			System.out.println("--------------分隔线---------------");
		
			List<User> users = session.selectList("selectUsers");
			for(int i=0; i<users.size(); i++) {
				System.out.println(users.get(i).getName());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}



你可能感兴趣的:(java学习笔记1 - Mybatis简单实例)