1. The example of mybatis in springside is already very detailed ( Spring-mybatis )
http://www.tuicool.com/articles/QBVVFr
<!-- mybatis config --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- Automatically scan the dto directory, saving manual configuration in Configuration.xml --> <property name="typeAliasesPackage" value="k.dto" /> <!-- Explicitly specify the Mapper file location--> <property name="mapperLocations" value="classpath:/k/dao/**/*Mapper.xml" /> </bean> <!-- Scan all interfaces identified by @MyBatisRepository under basePackage --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="k.dao" /> <property name="annotationClass" value="k.dao.base.MyBatisRepository"/> </bean>
/** * Identifies the DAO of MyBatis to facilitate the scanning of {@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> <!-- has been defined in the interface using annotations <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>
2. Mybatis uses Dao code to add, delete, modify and check
http://www.cnblogs.com/maocs/p/5047860.html
3. DAO inherits the method of SqlSessionDaoSupport ( mybatis-spring package )
http://blog.csdn.net/sskicgah/article/details/12575939
Fourth, Mybatis data access layer DAO layer BaseDao implementation class template
http://blog.sina.com.cn/s/blog_d00b69580102w9f3.html
Five, Mybatis generic DAO interface design
http://www.360doc.com/content/12/0429/04/1542811_207413269.shtml
Use mybatis to automatically create dao and mapping