Mybatis 使用一周总结
2011-07-01 11:33
435 查看
最近的项目 要用到MYBATIS,所以一周来一直在查相关的资料,并做实验.下面根据我的经历,随笔写下以下:
使用心得: 此框架的好处,在于管理SQL上,主要的心思在书写业务相关的SQL.有时候复杂的业务,用一条较复杂的SQL就可疑搞定,省了后台好多代码. 不过要想用它在简单的业务也要写SQL,呵呵,这一点就不如HIBERNET省事了.不过写SQL有种,说不出的好感.就是操作感好...闲话不说了 开始总结
一 当然要和SPRING接合,用AOP帮忙处理事物,dataSource就不用多说了,SqlSessionFactoryBean//和创建的SqlSessionTemplate,参考配置文件.相信你懂得!请看配置文件applicationContext-core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com" />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- DataSource proxool Configure -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
<property name="driver"><value>${jdbc.driver}</value></property>
<property name="driverUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="alias"><value>${jdbc.username}</value></property>
<!-- 最大活动时间(超过此时间线程将被kill,默认为5分钟) -->
<property name="maximumActiveTime" value="3600000"/>
<!-- 最少保持的空闲连接数 (默认2个) -->
<property name="prototypeCount" value="5"/>
<!-- 最大连接数 (默认5个) -->
<property name="maximumConnectionCount" value="150"/>
<!-- 最小连接数 (默认2个) -->
<property name="minimumConnectionCount" value="2"/>
<property name="trace" value="true"/>
<property name="verbose" value="true"/>
<property name="statistics" value="30s,15m"/>
<property name="simultaneousBuildThrottle" value="30"/>
</bean>
<!-- 数据连接管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务控制 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public * com.*.*.service.imp.*.*(..))" id="ServiceOpertation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOpertation" />
</aop:config>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mapping-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 这里实例化一个SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
注:其中sqlSessionTemplate肩负执行SQL的重任;<aop:config> 的拦截方法要写对;<tx:attributes>配置的不做事务的方法read-only="true";确定myBatis配置文件 SqlSessionFactoryBean下 如:<property name="configLocation" value="classpath:mapping-config.xml"/>
二 mapping-config.xml 配置文件 如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 实体Bean的映射 -->
<!-- 系统配置开始 -->
<typeAlias type="com.asc.sysuser.bean.SysUser" alias="SysUser" />
<!-- 系统配置结束 -->
</typeAliases>
<!-- SQL XML 的映射 -->
<mappersList path="E:/workspace/DPSOS/beanXml/"/>
</configuration>
相信个位都看的懂的 注: <mappersList path="E:/workspace/DPSOS/beanXml/"/> 是放SQL配置的绝对路径.
三 SQL XML 的映射
到了关键了!先举个例子
<?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.asc.dos.trigger.bean">
<resultMap type="TriggerConfig" id="triggerConfig">
<result property="id" column="ID"/>
<result property="orgId" column="ORG_ID"/>
<result property="jobName" column="JOB_NAME"/>
<result property="category" column="CATEGORY"/>
<result property="runtimes" column="RUNTIMES"/>
<result property="repeatInterval" column="REPEATINTERVAL"/>
<result property="startDate" column="STARTDATE"/>
<result property="endDate" column="ENDDATE"/>
<result property="cronExpression" column="CRONEXPRESSION" />
<!-- <result property="fileConf" column="FILE_CONF_ID" />-->
</resultMap>
<parameterMap type="hashmap" id="triggermap">
<parameter property="id" javaType="string"/>
</parameterMap>
<!-- 新增企业trigger配置-->
<insert id="addTriggerConfig" parameterMap="triggermap" >
<selectKey keyProperty="id" resultType="string" order="BEFORE" >
select DOS_SEQUENCE.NEXTVAL from dual
</selectKey>
<![CDATA[
insert into DOS_TRIGGER_CONFIG (ID,ORG_ID,JOB_NAME,CATEGORY,RUNTIMES,
REPEATINTERVAL,STARTDATE,ENDDATE,CRONEXPRESSION)
values(#{id},#{orgId,jdbcType=VARCHAR},#{jobName,jdbcType=VARCHAR},#{category,jdbcType=VARCHAR},
#{runtimes,jdbcType=NUMERIC},#{repeatInterval,jdbcType=VARCHAR},#{startDate,jdbcType=VARCHAR},
#{endDate,jdbcType=VARCHAR},#{cronExpression,jdbcType=VARCHAR})
]]>
</insert>
<!--删除企业trigger的配置-->
<delete id="deleteTriggerConfig" parameterType="string">
delete from DOS_TRIGGER_CONFIG where id=#{id}
</delete>
<!--查询企业trigger配置 -->
<select id="selectTriggerConfig" parameterType="hashmap" resultMap="triggerConfig">
select * from (
select ROWNUM-1 RN,tri.* from DOS_TRIGGER_CONFIG tri where 1=1
<if test="param!=null and param!=''">
and tri.ORG_ID=#{param}
</if>
<![CDATA[
)S1 where S1.RN>=#{pageInfo.start} and S1.RN<(#{pageInfo.start}+#{pageInfo.limit})
]]>
</select>
<!--查询企业trigger配置信息数量 -->
<select id="getTriggerConfigNum" parameterType="string" resultType="_int">
select COUNT(*)
from DOS_TRIGGER_CONFIG where 1=1
<if test="value!=null">
and ORG_ID=#{value}
</if>
</select>
<select id="getTriggerNameIsExist" parameterType="string" resultType="_int" >
select COUNT(*) FROM DOS_TRIGGER_CONFIG where 1=1
<if test="value!=null">
and JOB_NAME=#{value}
</if>
</select>
</mapper>
偶遇到的问题,基本都在这块啦,下面一点一点来。
1.resultMap 返回值映射,type就是后台对应的bean喽,起个ID下面sql好引用。property是bean的属性名,column是对应的sql(也
就是select语句)查出的结果集的列名。多表查询,起个好记的列名,sql里写对应的别名就OK了,用着舒服!
2、parameterMap 参数映射,当后台传值,无论是Map还是bean对象,都可以用parameterMap,自动找映射关系。比如本例中的
insert 中用的参数为parameterMap="triggermap" ,其实我后台传的是triggerBean对象,然好用。
3、说道insert 初用者常出现空指针NULL的错误。原因是service传参是NULL,Mybatis 无法判断转换的jdbcType的具体类型,解决
办法:如本例中的insert #{orgId,jdbcType=VARCHAR}/#{runtimes,jdbcType=NUMERIC} 指定jdbcType类型 就会把NULL插入数据
库了!
4、还有DATE数据类型,插入数据库的格式最初苦恼着我,书写#{endDate,jdbcType=DATE} 只有日期时间入库。后来我查询、实验发
现:jdbcType ...DATE 只显示日期;TIME 只显示时间;不填或写其他类型(本例子写成VARCHAR) 日期时间都显示。
5、传参字符串判定, 把参数传入有时需要IF标签来判断验证,多次实验得出以下结论:a)参数为String,int 时,判断test中的值
为value,如例子所见。 b)String 只支持非空验证不支持字符串判定 如 value!=''永久为真,而map和其他对象传参,其属性支持!
='',我也很奇怪,有高人指点就好了。
6. <![CDATA[ ]]> 标签用在把一些影响读取sql的字符,强转为可读,目前遇到<>
7.模糊查询时,like '%'||#{value}||'%' 我使用||连接符来连接字符串,可以用。应该有更好的方法!
这回就写这些,以后再遇到问题再更新,希望 有高手多指点指点!3Q OVER 11:30 2011-7-1
使用心得: 此框架的好处,在于管理SQL上,主要的心思在书写业务相关的SQL.有时候复杂的业务,用一条较复杂的SQL就可疑搞定,省了后台好多代码. 不过要想用它在简单的业务也要写SQL,呵呵,这一点就不如HIBERNET省事了.不过写SQL有种,说不出的好感.就是操作感好...闲话不说了 开始总结
一 当然要和SPRING接合,用AOP帮忙处理事物,dataSource就不用多说了,SqlSessionFactoryBean//和创建的SqlSessionTemplate,参考配置文件.相信你懂得!请看配置文件applicationContext-core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com" />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- DataSource proxool Configure -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
<property name="driver"><value>${jdbc.driver}</value></property>
<property name="driverUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="alias"><value>${jdbc.username}</value></property>
<!-- 最大活动时间(超过此时间线程将被kill,默认为5分钟) -->
<property name="maximumActiveTime" value="3600000"/>
<!-- 最少保持的空闲连接数 (默认2个) -->
<property name="prototypeCount" value="5"/>
<!-- 最大连接数 (默认5个) -->
<property name="maximumConnectionCount" value="150"/>
<!-- 最小连接数 (默认2个) -->
<property name="minimumConnectionCount" value="2"/>
<property name="trace" value="true"/>
<property name="verbose" value="true"/>
<property name="statistics" value="30s,15m"/>
<property name="simultaneousBuildThrottle" value="30"/>
</bean>
<!-- 数据连接管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务控制 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public * com.*.*.service.imp.*.*(..))" id="ServiceOpertation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOpertation" />
</aop:config>
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mapping-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 这里实例化一个SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
注:其中sqlSessionTemplate肩负执行SQL的重任;<aop:config> 的拦截方法要写对;<tx:attributes>配置的不做事务的方法read-only="true";确定myBatis配置文件 SqlSessionFactoryBean下 如:<property name="configLocation" value="classpath:mapping-config.xml"/>
二 mapping-config.xml 配置文件 如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 实体Bean的映射 -->
<!-- 系统配置开始 -->
<typeAlias type="com.asc.sysuser.bean.SysUser" alias="SysUser" />
<!-- 系统配置结束 -->
</typeAliases>
<!-- SQL XML 的映射 -->
<mappersList path="E:/workspace/DPSOS/beanXml/"/>
</configuration>
相信个位都看的懂的 注: <mappersList path="E:/workspace/DPSOS/beanXml/"/> 是放SQL配置的绝对路径.
三 SQL XML 的映射
到了关键了!先举个例子
<?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.asc.dos.trigger.bean">
<resultMap type="TriggerConfig" id="triggerConfig">
<result property="id" column="ID"/>
<result property="orgId" column="ORG_ID"/>
<result property="jobName" column="JOB_NAME"/>
<result property="category" column="CATEGORY"/>
<result property="runtimes" column="RUNTIMES"/>
<result property="repeatInterval" column="REPEATINTERVAL"/>
<result property="startDate" column="STARTDATE"/>
<result property="endDate" column="ENDDATE"/>
<result property="cronExpression" column="CRONEXPRESSION" />
<!-- <result property="fileConf" column="FILE_CONF_ID" />-->
</resultMap>
<parameterMap type="hashmap" id="triggermap">
<parameter property="id" javaType="string"/>
</parameterMap>
<!-- 新增企业trigger配置-->
<insert id="addTriggerConfig" parameterMap="triggermap" >
<selectKey keyProperty="id" resultType="string" order="BEFORE" >
select DOS_SEQUENCE.NEXTVAL from dual
</selectKey>
<![CDATA[
insert into DOS_TRIGGER_CONFIG (ID,ORG_ID,JOB_NAME,CATEGORY,RUNTIMES,
REPEATINTERVAL,STARTDATE,ENDDATE,CRONEXPRESSION)
values(#{id},#{orgId,jdbcType=VARCHAR},#{jobName,jdbcType=VARCHAR},#{category,jdbcType=VARCHAR},
#{runtimes,jdbcType=NUMERIC},#{repeatInterval,jdbcType=VARCHAR},#{startDate,jdbcType=VARCHAR},
#{endDate,jdbcType=VARCHAR},#{cronExpression,jdbcType=VARCHAR})
]]>
</insert>
<!--删除企业trigger的配置-->
<delete id="deleteTriggerConfig" parameterType="string">
delete from DOS_TRIGGER_CONFIG where id=#{id}
</delete>
<!--查询企业trigger配置 -->
<select id="selectTriggerConfig" parameterType="hashmap" resultMap="triggerConfig">
select * from (
select ROWNUM-1 RN,tri.* from DOS_TRIGGER_CONFIG tri where 1=1
<if test="param!=null and param!=''">
and tri.ORG_ID=#{param}
</if>
<![CDATA[
)S1 where S1.RN>=#{pageInfo.start} and S1.RN<(#{pageInfo.start}+#{pageInfo.limit})
]]>
</select>
<!--查询企业trigger配置信息数量 -->
<select id="getTriggerConfigNum" parameterType="string" resultType="_int">
select COUNT(*)
from DOS_TRIGGER_CONFIG where 1=1
<if test="value!=null">
and ORG_ID=#{value}
</if>
</select>
<select id="getTriggerNameIsExist" parameterType="string" resultType="_int" >
select COUNT(*) FROM DOS_TRIGGER_CONFIG where 1=1
<if test="value!=null">
and JOB_NAME=#{value}
</if>
</select>
</mapper>
偶遇到的问题,基本都在这块啦,下面一点一点来。
1.resultMap 返回值映射,type就是后台对应的bean喽,起个ID下面sql好引用。property是bean的属性名,column是对应的sql(也
就是select语句)查出的结果集的列名。多表查询,起个好记的列名,sql里写对应的别名就OK了,用着舒服!
2、parameterMap 参数映射,当后台传值,无论是Map还是bean对象,都可以用parameterMap,自动找映射关系。比如本例中的
insert 中用的参数为parameterMap="triggermap" ,其实我后台传的是triggerBean对象,然好用。
3、说道insert 初用者常出现空指针NULL的错误。原因是service传参是NULL,Mybatis 无法判断转换的jdbcType的具体类型,解决
办法:如本例中的insert #{orgId,jdbcType=VARCHAR}/#{runtimes,jdbcType=NUMERIC} 指定jdbcType类型 就会把NULL插入数据
库了!
4、还有DATE数据类型,插入数据库的格式最初苦恼着我,书写#{endDate,jdbcType=DATE} 只有日期时间入库。后来我查询、实验发
现:jdbcType ...DATE 只显示日期;TIME 只显示时间;不填或写其他类型(本例子写成VARCHAR) 日期时间都显示。
5、传参字符串判定, 把参数传入有时需要IF标签来判断验证,多次实验得出以下结论:a)参数为String,int 时,判断test中的值
为value,如例子所见。 b)String 只支持非空验证不支持字符串判定 如 value!=''永久为真,而map和其他对象传参,其属性支持!
='',我也很奇怪,有高人指点就好了。
6. <![CDATA[ ]]> 标签用在把一些影响读取sql的字符,强转为可读,目前遇到<>
7.模糊查询时,like '%'||#{value}||'%' 我使用||连接符来连接字符串,可以用。应该有更好的方法!
这回就写这些,以后再遇到问题再更新,希望 有高手多指点指点!3Q OVER 11:30 2011-7-1
相关文章推荐
- 使用springmvc+mybatis时遇到的问题总结
- Mybatis 使用技巧总结
- Mybatis使用过程中的一些总结
- 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- Spring + mybatis + mysql使用事物的几种方法总结
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- MyBatis使用总结
- mybatis foreach使用总结
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- MyBatis学习总结(16)——Mybatis使用的几个建议
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- MyBatis学习总结_02_使用MyBatis对表执行CRUD操作
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- 使用mybatis时paramType传入参数总结
- Mybatis使用总结
- MyBatis持久层框架使用总结
- 使用mybatis分页插件PageHelper5.0.0遇到的问题总结
- MyBatis学习总结(16)——Mybatis使用的几个建议
- Mybatis自定义ResultMap使用总结