ibatis

主配置文件

MySqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/ibatis"/>
      <property name="JDBC.Username" value="root"/>
      <property name="JDBC.Password" value=""/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the 
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="com/mydomain/data/Account.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>

在这个文件当中可以看到,数据库为ibatis,使用jdbc连接,所以还需要一个mysql-connector-java.jar

同时这个文件引用了Account.xml这个文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Account">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Account" type="com.mydomain.domain.Account"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties 
       exactly. -->
  <resultMap id="AccountResult" class="Account">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>

  <!-- A simpler select example without the result map.  Note the 
       aliases to match the properties of the target result class. -->
  <select id="selectAccountById" parameterClass="int" resultClass="Account">
    select
      ACC_ID as id,
      ACC_FIRST_NAME as firstName,
      ACC_LAST_NAME as lastName,
      ACC_EMAIL as emailAddress
    from ACCOUNT
    where ACC_ID = #id#
  </select>
   
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    )values (
      #firstName#, #lastName#, #emailAddress#
    )
  </insert>

  <!-- Update example, using the Account parameter class -->
  <update id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>
</sqlMap>

在这个文件当中书写了所有的sql语句,没一个语句有一个id,有的含有参数类型,有的含有返回结果类型


当然通过这个配置文件,我们可以创建一个account表,和一个account.java.

account类有
id (毫无疑问,这个是integer类型)
firstName string(我感觉应该是这个类型)
lastName  string(我感觉应该是这个类型)
emailAddress string(我感觉应该是这个类型)


account
这个表的内容
acc_id        int
acc_first_name varchar
acc_last_name  varchar
acc_email      varchar


注意!!!!!!!!!!!!!!!!!!!!

一定要勾选id为auto_increament

java

package com.mydomain.domain;

public class Account {
	private int id;
	private String firstName;
	private String lastName;
	private String emailAddress;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
}


SimpleExample.java

package com.mydomain.data;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account;

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

public class SimpleExample {
	private static SqlMapClient sqlMapper;
	static {
		try {
			Reader reader = Resources.getResourceAsReader("com/mydomain/data/MySqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close(); 
		}catch (IOException e) {
			// Fail fast.
			throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
		}
	}

	public static List selectAllAccounts () throws SQLException {
		return sqlMapper.queryForList("selectAllAccounts", sqlMapper);
		//return null;
	}

	public static Account selectAccountById  (int id) throws SQLException {
		return (Account) sqlMapper.queryForObject("selectAccountById", id);
	}

	public static void insertAccount (Account account) throws SQLException {
		sqlMapper.insert("insertAccount", account);
	}

	public static void updateAccount (Account account) throws SQLException {
		sqlMapper.update("updateAccount", account);
	}

	public static void deleteAccount (int id) throws SQLException {
		sqlMapper.delete("deleteAccountById", id);
	}
}


最后如果测试的话,我们还需要一个测试类

MyTest.java

/**
 * 
 */
package com.mydomain.data;

import java.sql.SQLException;  
import java.util.Arrays;  
import java.util.Collections;  
import java.util.List;  
import org.junit.Test; 

import com.mydomain.domain.Account;

public class MyTest {
	    @Test  
	    public void selectAllAccounts(){  
	        try {  
	            List list=SimpleExample.selectAllAccounts();  
	            System.out.println(Arrays.toString(list.toArray()));  
	        } catch (SQLException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	    }  
	    @Test
	    public void selectAccountById(){  
	        try {  
	            Account account=SimpleExample.selectAccountById(3);  
	            System.out.println(account);  
	        } catch (SQLException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	    }  
	    @Test
	    public void insertAccount(){  
	        Account account=new Account();  
	        account.setFirstName("tom");  
	        account.setLastName("jam");  
	        account.setEmailAddress("china");  
	        try {  
	            SimpleExample.insertAccount(account);  
	        } catch (SQLException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	    }  
	    @Test
	    public void updateAccount(){  
	        try {  
	            Account account=SimpleExample.selectAccountById(2);  
	            account.setFirstName("gates");  
	            SimpleExample.updateAccount(account);  
	        } catch (SQLException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	          
	    }  
	    @Test
	    public void deleteAccount(){  
	        try {  
	            SimpleExample.deleteAccount(1);  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	    }  
}



让后工作就完成了,注意!!!!!!!!!!!!!!!!

每一个测试类需要一个@test


不用启动tomcat服务器。



注意!!!!!!!!!!!!!!!!!!

今天很神奇,该开始的时候我测试的时候一直反应 connection time out,后来我请教别人的时候,突然ok了,真奇怪。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值