总配置文件sqlMapConfig 
<?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> 
  <transactionManager type="JDBC"  commitRequired="false"> 
    <dataSource type="soccer"> 
      <property name="JDBC.Driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> 
      <property name="JDBC.ConnectionURL" value="jdbc:sqlserver://localhost:1433"/> 
      <property name="JDBC.Username" value="sa"/> 
      <property name="JDBC.Password" value="123"/> 
    </dataSource> 
  </transactionManager> 
  <sqlMap resource="model/league.xml"/> 
</sqlMapConfig> 
映射文件 
<?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 > 
  <typeAlias alias="League" type="model.League"/> 
  <resultMap id="LeagueResult" class="League"> 
  
    <result property="year" column="year"/> 
    <result property="season" column="season"/> 
    <result property="title" column="title"/> 
  </resultMap> 
  <insert id="insertLeague" parameterClass="League"> 
    insert into league ( 
      year, 
      season, 
      title) 
    values ( 
      #year#, #season#,#title# 
    ) 
  </insert> 
</sqlMap> 
package model; public class League { 
private int year; 
private String season; 
private String title; public int getYear() { 
return year; 

public void setYear(int year) { 
this.year = year; 

public String getSeason() { 
return season; 

public void setSeason(String season) { 
this.season = season; 

public String getTitle() { 
return title; 

public void setTitle(String title) { 
this.title = title; 

    

public class LeagueBLL { 
  
  private static SqlMapClient sqlMap; 
  static { 
    try { 
      Reader reader = Resources.getResourceAsReader("model/SqlMapConfig.xml"); 
      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); 
      reader.close(); 
    } catch (Exception e) { 
      // Fail fast. 
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e); 
    } 
  } 
  public void AddLeague(League league) 
  { 
  try { 
sqlMap.insert("insertLeague", league); 
} catch (SQLException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 

  } public static void main(String []args){  
        LeagueBLL stu = new LeagueBLL();  
          
        System.out.println("------------------------------- start ------------------------------"); 
  
        League league = new League(); 
            league.setYear(year); 
            league.setSeason(season); 
            league.setTitle(title); 
            
            LeagueBLL lb=new LeagueBLL(); 
            lb.AddLeague(league); 

我想用ibatis框架把他写进sql server2005数据库 但出现异常 偶是新手不会  高手帮忙 

解决方案 »

  1.   

    数据库名咋都没找到?这个类LeagueBLL  里面 获取这个SqlMapClient 对象的代码放到构造函数里去。
      

  2.   

    楼主给你个例子吧
    http://blog.163.com/songze39@126/blog/static/610390652008823105927970/
    另外CSDN有下载的可运行的例子。
      

  3.   

    我用了那个例子 怎么还是出异常啊 
    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Servlet execution threw an exception
    root cause java.lang.NoClassDefFoundError
    com.ibatis.league.doGet(league.java:45)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
     
      

  4.   

     感觉像是 缺少 jar 包。
      

  5.   

    java.lang.NoClassDefFoundError 
    com.ibatis.league.doGet(league.java:45) 标红的地方全不对吧··
    你有com.ibatis这个包??包下面有个类叫league??
      

  6.   

    你可以跟踪你的代码来检查你具体错误的地方啊,这样看很累的。
    你先确认你的数据库是否连接上,连接上后是否执行过了sql,如果中间出现异常的话,你就可以定位到那句话出现了异常,然后看看这个异常是否是缺少包或者其他可能的错误造成的。还有你贴出来的时候,你可以把异常信息也贴出来,我们好能帮你快速的定位问题。