我想要一个spring 结合mybatis的实例,只要实现从数据库的表里能取出数据实现select就行不要web的,只要有main函数,输出从数据库里查询出来的结果一天地,可是我不会配置,有谁帮帮我,我刚学java,谢谢了

解决方案 »

  1.   

    运行 mybatis-generator 
    <?xml version="1.0" encoding="UTF-8"?>  
    <project default="genfiles" basedir=".">  
        <target name="genfiles" description="Generate the files">  
            <taskdef name="mbgenerator" classname="org.mybatis.generator.ant.GeneratorAntTask"  
                classpath="mybatis-generator-core-1.3.0.jar" />  
            <mbgenerator overwrite="true" configfile="src/main/resource/config.xml"  
                verbose="false">  
            </mbgenerator>  
        </target>  
    </project>
    使用 mybatis-generator 生成的代码      修改config.xml中jdbc jar包的位置
    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xmlns:context="http://www.springframework.org/schema/context"  
         xsi:schemaLocation="   
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"   
         default-autowire="byName">  
      
        <bean  
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations" value="classpath:datasource.properties" />  
        </bean>  
           
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
            <property name="driverClassName" value="${jdbc.driverClassName}" />  
            <property name="url" value="${jdbc.url}" />  
            <property name="username" value="${jdbc.username}" />  
            <property name="password" value="${jdbc.password}" />  
        </bean>  
      
        <bean id="transactionManager"  
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource" ref="dataSource" />  
        </bean>  
      
        <!-- beware that mapper-config.xml is not needed if you use injected mappers -->  
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <!--    <property name="configLocation" value="classpath:MapperConfig.xml" /> -->  
            <property name="dataSource" ref="dataSource" />  
        </bean>  
      
        <bean id="petMapper1" class="org.mybatis.spring.MapperFactoryBean">  
            <!-- SqlSessionFactory property is autowired -->  
            <property name="mapperInterface" value="test.dao.PetMapper" />  
        </bean>  
      
    </beans>  <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
         default-autowire="byName"> <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:datasource.properties" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>    <!-- beware that mapper-config.xml is not needed if you use injected mappers -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--    <property name="configLocation" value="classpath:MapperConfig.xml" /> -->
            <property name="dataSource" ref="dataSource" />
        </bean>    <bean id="petMapper1" class="org.mybatis.spring.MapperFactoryBean">
            <!-- SqlSessionFactory property is autowired -->
            <property name="mapperInterface" value="test.dao.PetMapper" />
        </bean></beans>
      Java代码 package test;   
      
    import org.springframework.context.ApplicationContext;   
    import org.springframework.context.support.ClassPathXmlApplicationContext;   
      
    import test.dao.PetMapper;   
    import test.model.PetExample;   
      
    public class Test {   
      
        public static void main(String[] args) {   
            ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");   
            PetExample pet = new PetExample();   
            pet.or().andDeathIsNotNull();   
            PetMapper map = (PetMapper) context.getBean("petMapper1");   
            System.out.println(map.selectByExample(pet));   
        }   
    }  
      

  2.   

    javaeye里面有一篇文章,好像是你要的,在文章的最下面有个book.rar,你可以下一下
    http://yanxunjian.javaeye.com/blog/807660
    http://yanxunjian.javaeye.com/blog/802632
      

  3.   

    <?xml version="1.0" encoding="UTF-8"?>   
    <project default="genfiles" basedir=".">   
      <target name="genfiles" description="Generate the files">   
      <taskdef name="mbgenerator" classname="org.mybatis.generator.ant.GeneratorAntTask"   
      classpath="mybatis-generator-core-1.3.0.jar" />   
      <mbgenerator overwrite="true" configfile="src/main/resource/config.xml"   
      verbose="false">   
      </mbgenerator>   
      </target>   
    </project>
    使用 mybatis-generator 生成的代码 修改config.xml中jdbc jar包的位置
    <?xml version="1.0" encoding="UTF-8"?>   
    <beans xmlns="http://www.springframework.org/schema/beans"   
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      xmlns:context="http://www.springframework.org/schema/context"   
      xsi:schemaLocation="   
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"   
      default-autowire="byName">   
       
      <bean   
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
      <property name="locations" value="classpath:datasource.properties" />   
      </bean>   
        
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
      <property name="driverClassName" value="${jdbc.driverClassName}" />   
      <property name="url" value="${jdbc.url}" />   
      <property name="username" value="${jdbc.username}" />   
      <property name="password" value="${jdbc.password}" />   
      </bean>   
       
      <bean id="transactionManager"   
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
      <property name="dataSource" ref="dataSource" />   
      </bean>   
       
      <!-- beware that mapper-config.xml is not needed if you use injected mappers -->   
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   
    <!-- <property name="configLocation" value="classpath:MapperConfig.xml" /> -->   
      <property name="dataSource" ref="dataSource" />   
      </bean>   
       
      <bean id="petMapper1" class="org.mybatis.spring.MapperFactoryBean">   
      <!-- SqlSessionFactory property is autowired -->   
      <property name="mapperInterface" value="test.dao.PetMapper" />   
      </bean>   
       
    </beans>   <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
      default-autowire="byName"><bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:datasource.properties" />
    </bean><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>  <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
      </bean>  <!-- beware that mapper-config.xml is not needed if you use injected mappers -->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- <property name="configLocation" value="classpath:MapperConfig.xml" /> -->
      <property name="dataSource" ref="dataSource" />
      </bean>  <bean id="petMapper1" class="org.mybatis.spring.MapperFactoryBean">
      <!-- SqlSessionFactory property is autowired -->
      <property name="mapperInterface" value="test.dao.PetMapper" />
      </bean></beans>
       Java代码  package test;   
       
    import org.springframework.context.ApplicationContext;   
    import org.springframework.context.support.ClassPathXmlApplicationContext;   
       
    import test.dao.PetMapper;   
    import test.model.PetExample;   
       
    public class Test {   
       
      public static void main(String[] args) {   
      ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");   
      PetExample pet = new PetExample();   
      pet.or().andDeathIsNotNull();   
      PetMapper map = (PetMapper) context.getBean("petMapper1");   
      System.out.println(map.selectByExample(pet));   
      }   
    }   
      

  4.   


    你的项目是 ssh ssh2的么,给我看看行么?我给你200分,
    我现在要做个ssh的框架,参考下你们的项目
      

  5.   


    刚学java 就慢慢学 不要直接给代码