发表于: 2019-10-31 22:55:14

1 941


今天完成的事情:

spring+mybatis


文件结构


实体文件   student


接口  interface

public interface StudentMapper {

public Student selectStudentId(int id );

public void selectStudent();

public void insertStudent();

public void deleteStudentStunum();

public void updateStudent();

}


spring    applicationContext.xml文件

    <context:component-scan base-package="com.ptteng"/>

   <!--数据源配置-->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="username" value="root"></property>
       <property name="password" value="451976"></property>
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC"></property>
   </bean>

   <!--mybatisspring的整合,不需要mybatis自己的配置映射文件-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--配置连接数据库数据源-->
       <property name="dataSource" ref="dataSource"></property>
       <!--配置Mapper文件所在位置-->
       <property name="mapperLocations" value="classpath*:StudentMapper.xml"></property>
   </bean>

   <!--MapperScannerConfigurer将会扫描basePackage并自动装配-->
   <bean id="mapperFactoryBean"  class="org.mybatis.spring.mapper.MapperFactoryBean">
       <!--        mapperInterface指定接口-->
       <property name="mapperInterface" value="com.ptteng.mapper.StudentMapper"></property>
       <!--        配置sqlSessionFactory-->
       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

   </bean>
</beans>


映射文件  StudentMapper.xml

<mapper namespace="com.ptteng.mapper.StudentMapper">

<!--    id给当前的sql语句起一个识别id,最好和接口一致
       parameterType  限制传入参数类型-->
<!--    resultType结果集封装的类型,只有查询才能用到
       #{id}占位符里面的名字任意-->

<!--    //查找单个   传入类型必须为包装型Integer-->
   <select id="selectStudentId"  parameterType="java.lang.Integer" resultType="com.ptteng.enity.Student">
     select * from bj where id = #{id}
</select>
<!--    //查找全部   输出类型必须为user定义类-->
   <select id ="selectStudent"    resultType="com.ptteng.enity.Student">
     select * from bj
</select>

<!--    //插入单个   传入类型必须为包装型user定义类-->
   <insert id="insertStudent"    parameterType="com.ptteng.enity.Student">
    insert into bj (name,qq,type,time,stunum) values (#{name},#{qq},#{type},#{time},#{stunum})
</insert>

<!--    //删除单个   传入类型必须为包装型user定义类-->
   <delete id="deleteStudentStunum"   parameterType="com.ptteng.enity.Student" >
   delete from bj where stunum = #{stunum}
</delete>

<!--    //更改单个    传入类型必须为包装型user定义类-->
   <update id="updateStudent"  parameterType="com.ptteng.enity.Student" >
   update bj set name = #{name} where id = #{id}
</update>

<!--            MyMapper.xml名字可以更改,位置任意-->
<!--            命名建议 StudentMapper.xml(mybatis)-->
<!--            建议命名为:类名+Mapper.xml-->


测试文件

public class AppTest {

@Test
   public void selectStudentId() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
       StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("mapperFactoryBean");
       Student student = studentMapper.selectStudentId(6);
       System.out.println(student);

   }
}

明天计划的事情:


继续推进任务


遇到的问题:


mapperLoctions   和  configlocation  


mapperLoctions的路径   是放mapper.xml       映射文件的


configlocation的路径    是放mybatis-Config.xml   主配置文件的


我把两个都搞混了,然后一直报     空指针的错误

花费了好长时间才解决这个问题





收获:

大概对mybatis 整合spring有了些了解


返回列表 返回列表
评论

    分享到