mybatis 注解配置

来源:互联网 发布:windows update安装慢 编辑:程序博客网 时间:2024/06/05 20:12

mapper.java

public interface StudentMapper {
    
    @Select("select * from student where id = #{id} and name = #{name}")
    List<Student> getById(@Param("id")int id,@Param("name")String name);
    
    @Select("select * from student where date > #{date} and id = #{id}")
    Student getByID1(Student student);
}

applicationContext.xml

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8" />
        <property name="password" value="root" />
        <property name="username" value="root" />
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <!-- 引入mybatis配置文件 -->
      <!-- <property name="configLocation" value="classpath:mybatis-Config.xml"></property> -->
    </bean>
    <!-- 注册mybatis映射器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
       <!-- 对应mapper类所在包 -->
       <property name="basePackage" value="com.mapper"/>  
    </bean>