MyBatis-DAO(1)
MyBatis--DAO(一)
一、springside中mybatis的例子已经很详细了(Spring-mybatis)
http://www.tuicool.com/articles/QBVVFr
<!-- mybatis config --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描dto目录, 省掉Configuration.xml里的手工配置 --> <property name="typeAliasesPackage" value="k.dto" /> <!-- 显式指定Mapper文件位置 --> <property name="mapperLocations" value="classpath:/k/dao/**/*Mapper.xml" /> </bean> <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="k.dao" /> <property name="annotationClass" value="k.dao.base.MyBatisRepository"/> </bean>
/** * 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyBatisRepository {}
@Component @MyBatisRepository public interface MybatisDemoMapper { List<MybatisDemoDTO> find(Map<String,Object> param); MybatisDemoDTO findById(int id); int insert(MybatisDemoDTO dto); void update(MybatisDemoDTO dto); @Delete("delete from test where id = #{id}") void delete(int id); }
<?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="k.dao.demo.MybatisDemoMapper"> <cache /> <sql id="testTableCols"> id,col </sql> <insert id="insert" parameterType="MybatisDemoDTO" useGeneratedKeys="true" keyProperty="id"> insert into test (<include refid="testTableCols"/>) values( 0,#{col} ) </insert> <update id="update" parameterType="MybatisDemoDTO"> update test set col = #{col} where id = #{id} </update> <!-- 已在接口中使用注解定义了 <delete id="delete" parameterType="int"> delete from test where id = #{id} </delete> --> <select id="findById" parameterType="int" resultType="MybatisDemoDTO"> select <include refid="testTableCols"/> from test where id = #{id} </select> <select id="find" parameterType="map" resultType="MybatisDemoDTO"> select <include refid="testTableCols"/> from test <where> <if test="col != null"> col like #{col} </if> </where> </select> </mapper>
二、Mybatis 使用Dao代码方式进行增、删、改、查
http://www.cnblogs.com/maocs/p/5047860.html
三、DAO继承了SqlSessionDaoSupport(mybatis-spring包)的方式
http://blog.****.net/sskicgah/article/details/12575939
四、Mybatis数据访问层DAO层BaseDao实现类模板
http://blog.sina.com.cn/s/blog_d00b69580102w9f3.html
五、Mybatis 泛型DAO接口的设计
http://www.360doc.com/content/12/0429/04/1542811_207413269.shtml
使用mybatis自动创建dao、mapping
http://wenku.baidu.com/link?url=FEM6CZFkB_76efmGstdlvKRfck0D4p_4IzXyDqYTOEtCHt3HOgiQE0EjolQdWCWR8boVeuoTmuf2wqWxnoueDbUqut88oW62kdHZkHeANC3