mybatis练习



time:2011/3/17 13:46
此myibatisdemo3与mybatisdemo2有不同之处


步骤一:
  新建项目:myibatisdemo3
步骤二:[color=green][/color]
  导入JAR包
  mybatis-3.0.4.jar
  mysql-connector-java-5.0.3-bin.jar
步骤三:
创建数据库,新建表:
create database test;[align=right]

use test;
create table user(
id int primary key auto_increment,
userName varchar(15),
password varchar(15),
age int
);
insert into user values(null,'tangdeng','tangdeng',25);
步骤四:
      新建实体类User.java
package com.fineweaver.vo;
public class User {
private int id;
private String userName;
private String password;
private int age;
public int getId() {
return id;}
public void setId(int id) {
this.id = id;}
public String getUserName() {
return userName;}
public void setUserName(String userName) {
this.userName = userName;}
public String getPassword() {
return password;}
public void setPassword(String password) {
this.password = password;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
public User() { super();}
}
步骤五:
UserMap.xml的编写

<?xml version="1.0" encoding="gbk" ?>    
<!DOCTYPE mapper    
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"    
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.fineweaver.vo.User">
    <select id="selectOne" parameterType="int" resultType="User">
select * from User where id = #{id}    
    </select>
</mapper>

步骤六:
Configuration.xml编写

<?xml version="1.0" encoding="gbk" ?>
<!-- 在有些情况下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.fineweaver.vo.User"/>
</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/test"/>   
<property name="username" value="root"/>   
<property name="password" value="root"/>   
    </dataSource>   
</environment>   
    </environments> 
    <!-- 用户XML配制 --> 
    <mappers>   
<mapper resource="com/fineweaver/xml/User.xml"/>   
    </mappers> 
</configuration>
步骤七:
   DAO的编写
package com.fineweaver.dao;
import org.apache.ibatis.session.SqlSession;
import com.fineweaver.util.Util;
import com.fineweaver.vo.User;
public class UserDAO {
public User query(int id){
         SqlSession sqlSession = Util.getSqlSessionFactory().openSession();  
         User user = null;  
         try{  
              user = (User)sqlSession.selectOne("com.fineweaver.vo.User.selectOne", id);  
         }catch(Exception e){  
             e.printStackTrace();  
         }finally{  
        sqlSession.close();  
         }  
         return user;  
     }  
}
步骤八:
工具Util.java编写

package com.fineweaver.util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Util {
private static SqlSessionFactory sqlSessionFactory = null;  
//这里是要将configuration集成到SqlSessionFactory中去
     static {  
String resource = "com/fineweaver/xml/Configuration.xml";        
Reader reader = null;  
try {  
     reader = Resources.getResourceAsReader(resource);  
} catch (IOException e) {  
     e.printStackTrace();  
}         
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  
     }  
     public static SqlSessionFactory getSqlSessionFactory(){  
return sqlSessionFactory;  
     }  
}
步骤九:
测试:

import org.junit.BeforeClass;
import org.junit.Test;
import com.fineweaver.dao.UserDAO;
import com.fineweaver.vo.User;
public class UserTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {}
@Test
public void UTest() {
try{
UserDAO userDao = new UserDAO();
User user = userDao.query(3);
System.out.println(user.getId());
System.out.println(user.getUserName());
System.out.println(user.getPassword());
System.out.println(user.getAge());
}catch(Exception e)
{
e.printStackTrace();
}
}
}
[/align]

你可能感兴趣的:(DAO,apache,xml,ibatis,JUnit)