发表于: 2017-10-07 23:38:25

2 805


今天完成的事情:

Spring+Mybatis的整合


实体类:                   Mapper类


配置文件:

ApplicationMapper.xml映射SQL语句


<mapper namespace="org.shunly.ApplicationMapper">

    <resultMap type="org.shunly.Application" id="applicationResult">

<id column="id" jdbcType="INTEGER" property="id"/>

<result column="name" jdbcType="VARCHAR" property="name"/>

<result column="qq" jdbcType="INTEGER" property="qq"/>

...省略

</resultMap>

<!-- insert语句 

id=标识符

parameterType=完整的实体类名或别名

-->

<insert id="insert" parameterType="org.shunly.Application">

insert into app1(name,qq,position,time,college,online_id,url,ambition,senior,info)

values(#{name},#{qq},#{position},#{time},#{college},#{onlineId},

#{url},#{ambition},#{senior},#{info});

</insert>


<delete id="delete" parameterType="org.shunly.Application">

delete from app1 where id=#{id};

</delete>


<!-- sql仅用于复用? -->

<sql id="selectAll" >

        select * from app1

  </sql>

 

<select id="getById" parameterType="org.shunly.Application" resultMap="applicationResult">

<include refid="selectAll"/>

where id=#{id};

</select>

...省略


mybatis-config.xml只用来定义别名和定位mapper

<typeAliases>

    <typeAlias alias="Application" type="org.shunly.Application"/>

    </typeAliases>

    <mappers>  

        <mapper resource="ApplicationMapper.xml"/>  

    </mappers>  


jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver  

jdbc.url=jdbc:mysql://localhost:3306/demo?user=root&password=1111&useUnicode=true&characterEncoding=UTF-8

 

Springconfig.xml

 <!-- 导入属性配置文件 -->  

    <context:property-placeholder location="classpath:jdbc.properties" />  

  <!-- 配置数据源 -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

        <property name="driverClassName" value="${jdbc.driverClassName}" />  

        <property name="url" value="${jdbc.url}" />  

    </bean>  

    

    <!-- 将数据源映射到sqlSessionFactory中 -->  

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

        <property name="configLocation" value="mybatis-config.xml" />  

        <property name="dataSource" ref="dataSource" />  

    </bean>  

  

    <!-- SqlSession模板类实例 -->  

    <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">  

        <constructor-arg index="0" ref="sqlSessionFactory" />  

    </bean>   

      

    <!--mapper配置-->  

    <bean id="applicationMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  

        <property name="mapperInterface" value="org.shunly.ApplicationMapper" />  

        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  

    </bean>  



明天计划的事情:

完成数据库的控制台操作



遇到的问题:

mybatis中的查询必须返回resultMap或者resultType

报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException:

A query was run and no Result Maps were found for the Mapped Statement 'org.shunly.ApplicationMapper.getById'.

It's likely that neither a Result Type nor a Result Map was specified.


错误:

<select id="getById" parameterType="org.shunly.Application">

<include refid="selectAll"/>

where id=#{id};

</select>

正确:

<select id="getById" parameterType="org.shunly.Application" resultMap="applicationResult">

<include refid="selectAll"/>

where id=#{id};

</select>


2 使用select映射语句时,注意id和方法名相同。。

报错:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): org.shunly.ApplicationMapper.ListApplication

at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225)

at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)

at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)

at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)

错误:

select语句的id="ListApplication"的l大写了。。。

正确:<select id="listApplication"  ...


收获:

整合过程中进一步加强了Spring和mybatis的使用和概念。



返回列表 返回列表
评论

    分享到