Mybatis+spring笔记
1 <mappernamespace="com.yihaomen.mybatis.models.UserMapper">
<selectid="selectUserByID" parameterType="int"resultType="User">
select* from `user` where id = #{id}
</select>
</mapper>
Namespace需要对应需要匹配的类的路径和类名(需要存在这个类com.yihaomen.mybatis.models.UserMapper)
其中id="selectUserByID"接口中需要存在selectUserById的方法
Parameter为输入参数,resultType为输出参数
User为typealias定义的别名
resultType返回基本类型或者定义的JAVABEAN,resultMap返回在mybatis中配置的自定义类型,其中resultMap中的type表示List中的类型
#{name}大小写敏感
useGeneratedKeys="true"keyProperty="id"其中seGeneratedKeys设置 为"true"表明要MyBatis获取由数据库自动生成的主键;keyProperty="id"指定把获取到的主键值注入到Student的id属性
2 多对一情况
<!-- User 联合文章进行查询方法之二的配置 (多对一的方式)-->
<resultMapid="resultUserArticleList-2" type="Article">
<idproperty="id" column="aid" />
<resultproperty="title" column="title" />
<resultproperty="content" column="content"/>
<associationproperty="user" javaType="User"resultMap="resultListUser"/>
</resultMap>
<select id="getUserArticles"parameterType="int" resultMap="resultUserArticleList">
selectuser.id,user.userName,user.userAddress,article.id aid,article.title,article.content fromuser,article
whereuser.id=article.userid and user.id=#{id}
</select>
3 动态sql
<select id="dynamicIfTest"parameterType="Blog" resultType="Blog">
select* from t_blog where 1 = 1
<if test="title != null">
andtitle = #{title}
</if>
</select>//如果你提供了title参数,那么就满足title=#{title}
<select id="dynamicChooseTest"parameterType="Blog" resultType="Blog">
select* from t_blog where 1 = 1
<choose>
<when test="title != null">
andtitle = #{title}
</when>
<whentest="content != null">
andcontent = #{content}
</when>
<otherwise>
andowner = "owner1"
</otherwise>
</choose>
</select>//相当于java中的switch
<selectid="dynamicTrimTest" parameterType="Blog"resultType="Blog">
select* from t_blog
<trim prefix="where" prefixOverrides="and |or">
<iftest="title != null">
title= #{title}
</if>
<iftest="content != null">
andcontent = #{content}
</if>
<iftest="owner != null">
orowner = #{owner}
</if>
</trim>
</select>//select* from t_blog where ….
<select id="dynamicTrimTest"parameterType="Blog" resultType="Blog">
select* from t_blog
<trim suffix="where" prefixOverrides="and |or">
<iftest="title != null">
title= #{title}
</if>
<iftest="content != null">
andcontent = #{content}
</if>
<iftest="owner != null">
orowner = #{owner}
</if>
</trim>
</select>//select* from t_blog …. Where
<select id="dynamicWhereTest"parameterType="Blog" resultType="Blog">
select* from t_blog
<where>
<iftest="title != null">
title= #{title}
</if>
<iftest="content != null">
andcontent = #{content}
</if>
<iftest="owner != null">
andowner = #{owner}
</if>
</where>
</select>// select * fromt_blog content = #{content} 智能处理and/or
<update id="dynamicSetTest"parameterType="Blog">
updatet_blog
<set>
<iftest="title != null">
title= #{title},
</if>
<iftest="content != null">
content= #{content},
</if>
<iftest="owner != null">
owner= #{owner}
</if>
</set>
whereid = #{id}
</update>//updatet_blog set content=#{content} 智能处理“,”
<select id="dynamicForeachTest"resultType="Blog">
select* from t_blog where id in
<foreach collection="list" index="index"item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>//select* from t_blog where id int ( , ,) //单参数List的类型
<select id="dynamicForeach3Test"resultType="Blog">
select* from t_blog where title like "%"#{title}"%" and id in
<foreachcollection="Array" index="index"item="item" open="(" separator=","close=")">
#{item}
</foreach>
</select>//select* from t_blog title like "%"#{title}"%" and id in ( , ,)//ids为数组的名称
public List<BpmDefUser> getByMap(Map<String,List<Long>> map){
Map<String,Object> params=new HashMap<String, Object>();
params.put("relationMap", map);
return this.getBySqlKey("getByMap", params);
}
<foreach collection="relationMap" index="key" item="ent" separator="union">
SELECT *
FROM BPM_DEF_USER
where RIGHT_TYPE=#{key}
and OWNER_ID in
<foreach collection="ent" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</foreach>
</select>//map中必须要求每个key对应的值为List/Array, index 作为map 的key。item为map的值,index和item的值自定义
4 与spring3/springMVC的集成
mybatis原理:sqlSessionFactoryBuilder---sqlSessionFactory---sqlSession---操作数据库
mapperScannerConfig(多个)/MapperFactoryBean–-getObject()--Mapper
mybatis-spring原理:
sqlSessionFactoryBean-sqlSessoionFactoryBuilder-sqlSessionFactory—sqlSession
mapperScannerConfig(多个)/MapperFactoryBean–-getObject()--Mapper
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations"
value="classpath:com/tiantian/ckeditor/mybatis/mappers/*Mapper.xml"/>
<property name="typeAliasesPackage"value="com.tiantian.ckeditor.model" />
</bean>
<bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.tiantian.mybatis.mapper.BlogMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> //为每一个MapperInterface定义一个MapperFactoryBean
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.tiantian.mybatis.mapper" />
</bean>//basePackage必须指定,为接口所在的位置,会把basePackage中的数据自动生成MapperFactoryBean
//dataSource必须指定,链接数据库的配置
// mapperLocations表示Mapper文件的存放位置,当Mapper文件与对应的Mapper接口处于同一位置时,可以不指定
// configLocation指定mybatis配置文件的位置,如果指定了该属性,会以该配置文件的内容构建sqlSessionFactoryBuilder,但是后面的配置信息可能会覆盖相应的信息。
// typeAliasesPackage用来为实体类所在的包定义包的别名
//typeAliases用来指定别名,前提是该类上没有标注@Alias注解,否则将使用该注解对应的值作为此种类型的别名
// plugins:数组类型,用来指定Mybatis的Interceptor,定义的类需要@Interceptor(…)
// typeHandlers:数组类型,表示TypeHandler (javaBean boolean 数据库char)
类需要继承typeHander,重写getResult()方法
// typeHandlersPackage:用来指定TypeHandler所在的包,如果指定了该属性,SqlSessionFactoryBean会自动把该包下面的类注册为对应的TypeHandler。多个package之间可以用逗号或者分号等来进行分隔
Eg1:
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.tiantian.mybatis"/>
<context:property-placeholderlocation="classpath:config/jdbc.properties" />
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<propertyname="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username"value="${jdbc.username}" />
<property name="password"value="${jdbc.password}" />
</bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations"value="classpath:com/tiantian/mybatis/mapper/*.xml" />
<property name="typeAliasesPackage"value="com.tiantian.mybatis.model" />
</bean>
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.tiantian.mybatis.mapper" />
</bean>
</beans>
Eg2:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.tiantian.mybatis"/>
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username"value="${jdbc.username}" />
<property name="password"value="${jdbc.password}" />
</bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations"value="classpath:com/tiantian/mybatis/mapper/*.xml" />
<property name="typeAliasesPackage"value="com.tiantian.mybatis.model" />
</bean>
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.tiantian.mybatis.mapper" />
</bean>
</beans>
Eg3
<context:annotation-config />
<context:component-scanbase-package="com.zxh.customer.template" />
<context:property-placeholder
location="classpath:sysconfig/jdbc_config.properties"/>
<!--JNDI数据源配置 <bean id="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jdbc/JTORDER</value> </property> </bean> -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="jtorder"/>
<property name="password" value="jtorder"/>
</bean>
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations"
value="classpath*:com/zxh/customer/template/model/**/*.xml"/>
<property name="configLocation" value="classpath:sysconfig/Configuration.xml"/>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
<!-- <constructor-arg index="1"value="BATCH" /> 如果想要进行批量操作可加入这个属性 -->
</bean>
</beans>
版权声明:本文为博主原创文章,未经博主允许不得转载。