发表于: 2018-04-25 23:04:11

1 632


今天完成的事情

a.今天上午学习了spring与spring的整合,深度的了解任务一的要求

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   <!-- 导入数据库配置 -->
       <context:property-placeholder location="db.properties"/>

   <!-- 开启      -->
   <context:annotation-config/>

   <!-- 扫描service包下所有使用注解的类型 -->
   <context:component-scan base-package="com.jnshu.taskone.serviceImpl" />


   <!-- 配置数据源 需导入commons-dbcp 包 destroy-method="close" 使用完毕后自动关闭 -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${driver}"/>
       <property name="url" value="${url}"/>
       <property name="username" value="${db_username}"/>
       <property name="password" value="${db_password}"/>
       <!-- 连接池的最大数据库连接数。设为0表示无限制。 -->
       <property name="maxActive" value="10"/>
       <!-- 最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。-->
       <property name="maxIdle" value="5"/>
   </bean>

   <!-- 配置工厂 需导入mybatis-spring包-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 定义使用的数据源 -->
       <property name="dataSource" ref="dataSource"/>
       <!-- 导入mybatis配置,接管sqlSessionFactory -->
       <property name="configLocation" value="mybatis/mybatis-config.xml"/>
   </bean>

   <!-- 定义实现类 原生dao -->
   <!--<bean id="userDao" class="com.jnshu.taskone.dao.UserDao">-->
       <!--&lt;!&ndash;ref="sqlSessionFactory" 调用上面的工厂 &ndash;&gt;-->
       <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
   <!--</bean>-->

   <!-- 配置mapper代理 MapperFactoryBean:用于生成mapper代理对象-->
   <!-- 手动定义-->
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
     <property name="mapperInterface" value="com.jnshu.taskone.dao.UserDao"></property>
     <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  </bean>

    <!--MapperScannerConfigurer mapper自动扫描器,将包下边的mapper接口自动创建代理对象到spring容器中,bean的id是mapper的类名(首字母小写)-->
   <!-- 配置扫描路径 -->
   <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
       <!--&lt;!&ndash; basePackage:指明一个包 &ndash;&gt;-->
       <!--<property name="basePackage" value="com.jnshu.taskone.dao.UserDao"/>-->
       <!--&lt;!&ndash; sqlSessionFactoryBeanName 指定注入sqlSessionFactory名称&ndash;&gt;-->
       <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
   <!--</bean>-->

</beans>



b.今天下午参考之前师兄的任务总结,并往自己的任务中经行代码的书写

<?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/beans
                          http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:property-placeholder location="db.properties"/>
   <bean id="datasource"  class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${driver}"/>
       <property name="url" value="${url}"/>
       <property name="username" value="${username}"/>
       <property name="password" value="${password}" />
   </bean>
   <bean id="service" class="com.jnshu.service.impl.UserServiceImpl">
       <property name="jdbcTemplate" ref="JDBCTemplate"></property>
   </bean>
   <bean id="JDBCTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <property name="dataSource" ref="datasource"/>
   </bean>



明天计划的事情:

a.完成服务器的连接及上传

b.完成任务一中需要调试的地方,测试插入数据有没有问题,争取今天完成任务一的要求
遇到的问题

a.首先在整合mybatis和spring中自己存在一个误区,那就是spring和mybatis都可以单独操作数据库进行增删改查,那将他们整合在一起的目的又是什么呢?

在百度上找的四种整合方式

1、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。

2、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。
  mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。

3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。

通过这三种常见的方式进行整合,但是我还是不太理解,然后又去找师兄的代码参考

在这里看到了引入工厂将我们的mybatis的配置文件引入,通过读取配置文件来建立数据库的连接

开始有点理解了我们说的整合的作用是什么了,因为spring可以直接读取配置文件生成对象,就不需要我们通过映射的方式将带有属性的参数传进来,那么就只是需要它进行连接数据库就好了,使用的是第二种方式,通过工厂的引入将带有 user的容器传给了datasource,这样就可以直接使用了

b.在理解了整合之后我还想了一个问题就是那如果仅仅只是去构造了这个容器那么这个mybatis根本就没有存在的价值,然后又看到了师兄的配置里面写了一个映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jnshu.taskone.dao.UserDao">
   <!-- sql片段块  -->
   <!-- 判断属性是否为空,为空不设置该属性-->
   <sql id="value_not_null">
     <trim suffixOverrides=",">
         <!-- and不能大写  -->
         <if test="username!='' and username!=null">username=#{username}, </if>
         <if test="qq!='' and qq!=null">qq=#{qq}, </if>
         <if test="profession!='' and profession!=null">profession=#{profession}, </if>
         <if test="join_date!='' and join_date!=null">join_date=#{join_date}, </if>
         <if test="school!='' and school!=null">school=#{school}, </if>
         <if test="online_id!='' and online_id!=null">online_id=#{online_id}, </if>
         <if test="daily_url!='' and daily_url!=null">daily_url=#{daily_url}, </if>
         <if test="declaration!='' and declaration!=null">declaration=#{declaration}, </if>
         <if test="counselor!='' and counselor!=null">counselor=#{counselor}, </if>
         <if test="update_time!='' and update_time!=null">update_time=#{update_time}, </if>
     </trim>
   </sql>
   <!-- sql片段块end -->

   <!-- 查找所有用户 -->
   <select id="findUserAll" resultType="User">
       select * from taskone
</select>
   <!-- 查找所有用户end -->

   <!-- 根据传入参数查找 -->
   <select id="findUserMore" resultType="User" parameterType="User">
       SELECT * FROM taskone
<where>
           <if test="id!=0 and id!=null">AND id=#{id} </if>
           <if test="username!='' and username!=null">AND username=#{username}</if>
           <if test="qq!='' and qq!=null">AND qq=#{qq}, </if>
           <if test="profession!='' and profession!=null">AND profession=#{profession}</if>
           <if test="join_date!='' and join_date!=null">AND join_date=#{join_date}</if>
           <if test="school!='' and school!=null">AND school=#{school}</if>
           <if test="online_id!='' and online_id!=null">AND online_id=#{online_id}</if>
           <if test="daily_url!='' and daily_url!=null">AND daily_url=#{daily_url}</if>
           <if test="declaration!='' and declaration!=null">AND declaration=#{declaration}</if>
           <if test="counselor!='' and counselor!=null">AND counselor=#{counselor}</if>
       </where>
   </select>
   <!-- 根据传入参数查找end -->

   <!-- 插入用户 -->
   <!-- insert into taskone(id,username,qq,profession,join_date,school,online_id,daily_url,declaration,counselor,create_time,update_time) value(2,"赵四","554564","web","2018123654","北","1554","www.baidu.com","jiayou","老王","201554633","46546")-->
   <!-- 插入时创建时间与更新时间一致 -->
   <insert id="insertUser" parameterType="User">
       INSERT INTO taskone(
username,qq,profession,join_date,school,online_id,daily_url,declaration,counselor,create_time,update_time)
VALUE (
#{username},#{qq},#{profession},#{join_date},#{school},#{online_id},#{daily_url},#{declaration},#{counselor},#{create_time},#{create_time})
<!-- 将返回的主键id 赋值给user.id -->
       <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
           SELECT LAST_INSERT_ID()
</selectKey>
   </insert>
   <!-- 插入用户end -->

   <!-- 删除用户 delete接口的返回类型设为Integer, 不需要在<delete></delete>中 定义resultType,即可自动返回受影响的行数。 -->
   <!-- jdbc:mysql://${jdbc.host}/${jdbc.db}?useAffectedRows=true 添加?useAffectedRows=true获取真实的收影响条数-->
   <delete id="deleteUser" parameterType="int" >
       DELETE FROM taskone WHERE id=#{id}
   </delete>
   <!-- 删除用户end-->

   <!-- 修改用户-->
   <update id="updateUser" parameterType="User">
       UPDATE taskone SET
<include refid="value_not_null"/>
       WHERE id=#{id}
   </update>
   <!-- 修改用户end-->

</mapper>

我是很不理解这段代码的作用是什么,就去问了师兄这个大概是什么意思,师兄说这个是为了让它判断我插入的内容,就算我插入的内容有缺少的,也不会影响它插入成功

后面还是有很多不理解的地方,比如我去调用方法的时候怎么判断使用什么方法,实现的执行完成需要去创建哪些对象,这些东西还是有些绕,自己有点不理解


收获:

今天收获spring与mybatis的整合的几种方式,以及实现数据操作的过程,遇到的问题还是很多的,主要是理解实现的过程还是有很多不太懂的地方,但是自己还没有想明白


返回列表 返回列表
评论

    分享到