MyBatis使用手册(一)

开始学习使用MyBatis,对于框架不熟悉,上手确定很快,留存一份记录。

1、准备jar包:
    mybatis-3.1.1.jar
    mysql.jar
2、创建web项目,项目路径如下:
    src:domain包
    test:domain包
    webroot:放置jar包

  在mysql中创建数据库:Mybatis
  创建表sysuser:包含三个字段
  uid:int primary key
  uname:varchar 
  passwd:varhcar

3、创建配置文件,包含数据库连接:在classpath下创建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 type="domain.Sysuser" alias="sysuser"/>
	</typeAliases>
  <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"/> 
        <property name="username" value="root"/> 
        <property name="password" value=""/> 
      </dataSource> 
    </environment>
  </environments> 
  <mappers>  
    <mapper resource="domain/Sysuser.xml"/>  
  </mappers> 
</configuration> 

4、在src/domain包中创建Sysuser实体类
package domain;

/**
 * 系统用户类
 * @author  Drama
 * @version 1.0
 */
public class Sysuser {

	private int uid;
	private String uname;
	private String passwd;
	
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	@Override
	public String toString(){
		return "SysUser[id = "+uid+", name = "+uname+"]";
	}
}

5、创建map文件:Sysuser.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="Sysuser">
		<!-- 
			查询用的语句,
			其中parameterType表示传递参数类型,
			resultType表示查询结果组成的对象
		 -->
		<select id="selectSysuser" parameterType="int" resultType="Sysuser">
			select * from sysuser where uid = #{uid}
		</select>
		
		<insert id="insertNewSysuser"  parameterType="Sysuser">
			insert into sysuser(uname,passwd)values(#{uname},#{passwd})
		</insert>
	</mapper>

6、在Configration.xml中引用map文件
7、创建测试类:在test/domain中创建TestSysuser.java类
package domain;

import java.io.IOException;
import java.io.Reader;

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 TestSysuser {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		//读取数据库连接,取得SqlSessionFactory
		String resource = "Configuration.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
		//取得session
		SqlSession session = ssf.openSession();
		try{
			/**
			 * step 1:insert a new user
			 */
			Sysuser user = new Sysuser();
			user.setUname("aaa");
			user.setPasswd("bbb");
			session.insert("insertNewSysuser", user);
			System.out.println("------insert a new Sysuser."+user.getUname()+"---"+user.getPasswd());
			
			/**
			 * step 2:get the sysuser
			 */
			Sysuser user2 = (Sysuser)session.selectOne("selectSysuser", 1);
			System.out.println(user2.toString());
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

}

-----

完成。

没有什么技术含量,不过,从点滴做起,即使很简单,这本来不过是个helloworld程序,
准备将spring与mybatis结合到一起。

你可能感兴趣的:(MyBatis使用手册(一))