標籤:
1.pom.xml環境搭建參考http://my.oschina.net/u/555061/blog/507474
2.給出目錄結構,
3.跟註解方式不同,這裡的dao包下面的UsersMapper.java不再有註解,
package com.xuebaosoft.mybatis3.mybatis_mapper.dao;import java.util.List;import java.util.Map;import com.xuebaosoft.mybatis3.mybatis_mapper.model.Users;public interface UsersMapper { int deleteByPrimaryKey(String id); int insert(Users record); int insertSelective(Users record); Users selectByPrimaryKey(String id); int updateByPrimaryKeySelective(Users record); int updateByPrimaryKey(Users record);List<Users> userPagingList(Map<String, Object> offsetAndPageSize);}
其實這裡是將帶註解篇http://my.oschina.net/u/555061/blog/507474介紹的UserDao.java替換為了UsersMapper.java
4.查看設定檔,看下註解的功能是如何用mapping方式代替實現的,mybatis-config.xml配置如下,
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"><configuration><environments default="environment"><environment id="environment"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql://192.168.191.1:3306/xuebaodb?characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><mappers><mapper resource="UsersMapper.xml" /></mappers></configuration>
其實這裡就是增加了:
<mappers><mapper resource="UsersMapper.xml" /></mappers>
也就說在載入配置mybatis-config.xml的時候就已經將UsersMapper.xml連帶著一起註冊到環境中了。而在UsersMapper.xml中會引用com.xuebaosoft.mybatis3.mybatis_mapper.dao.UsersMapper這個類,因此相當於Dao層的Mapper類有了,映射sql的mapper.xml有了,datasource也已經配置完畢,因此到此為止所有的環境均已構建好。
5.UsersMapper.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.xuebaosoft.mybatis3.mybatis_mapper.dao.UsersMapper"><resultMap id="BaseResultMap"type="com.xuebaosoft.mybatis3.mybatis_mapper.model.Users"><id column="id" property="id" jdbcType="VARCHAR" /><result column="username" property="username" jdbcType="VARCHAR" /><result column="password" property="password" jdbcType="VARCHAR" /><result column="name" property="name" jdbcType="VARCHAR" /><result column="nickname" property="nickname" jdbcType="VARCHAR" /><result column="sex" property="sex" jdbcType="VARCHAR" /><result column="picture" property="picture" jdbcType="VARCHAR" /><result column="createtime" property="createtime" jdbcType="VARCHAR" /><result column="lastlogintime" property="lastlogintime"jdbcType="VARCHAR" /><result column="tilepath" property="tilepath" jdbcType="VARCHAR" /></resultMap><sql id="Base_Column_List">id, username, password, name, nickname, sex, picture, createtime,lastlogintime,tilepath</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap"parameterType="java.lang.String">select<include refid="Base_Column_List" />from userswhere id = #{id,jdbcType=VARCHAR}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.String">delete from userswhere id = #{id,jdbcType=VARCHAR}</delete><insert id="insert" parameterType="com.xuebaosoft.mybatis3.mybatis_mapper.model.Users">insert into users (id, username, password,name, nickname, sex,picture, createtime, lastlogintime,tilepath)values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},#{picture,jdbcType=VARCHAR}, #{createtime,jdbcType=VARCHAR},#{lastlogintime,jdbcType=VARCHAR},#{tilepath,jdbcType=VARCHAR})</insert><insert id="insertSelective" parameterType="com.xuebaosoft.mybatis3.mybatis_mapper.model.Users">insert into users<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">id,</if><if test="username != null">username,</if><if test="password != null">password,</if><if test="name != null">name,</if><if test="nickname != null">nickname,</if><if test="sex != null">sex,</if><if test="picture != null">picture,</if><if test="createtime != null">createtime,</if><if test="lastlogintime != null">lastlogintime,</if><if test="tilepath != null">tilepath,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="id != null">#{id,jdbcType=VARCHAR},</if><if test="username != null">#{username,jdbcType=VARCHAR},</if><if test="password != null">#{password,jdbcType=VARCHAR},</if><if test="name != null">#{name,jdbcType=VARCHAR},</if><if test="nickname != null">#{nickname,jdbcType=VARCHAR},</if><if test="sex != null">#{sex,jdbcType=VARCHAR},</if><if test="picture != null">#{picture,jdbcType=VARCHAR},</if><if test="createtime != null">#{createtime,jdbcType=VARCHAR},</if><if test="lastlogintime != null">#{lastlogintime,jdbcType=VARCHAR},</if><if test="tilepath != null">#{tilepath,jdbcType=VARCHAR},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.xuebaosoft.mybatis3.mybatis_mapper.model.Users">update users<set><if test="username != null">username = #{username,jdbcType=VARCHAR},</if><if test="password != null">password = #{password,jdbcType=VARCHAR},</if><if test="name != null">name = #{name,jdbcType=VARCHAR},</if><if test="nickname != null">nickname = #{nickname,jdbcType=VARCHAR},</if><if test="sex != null">sex = #{sex,jdbcType=VARCHAR},</if><if test="picture != null">picture = #{picture,jdbcType=VARCHAR},</if><if test="createtime != null">createtime = #{createtime,jdbcType=VARCHAR},</if><if test="lastlogintime != null">lastlogintime = #{lastlogintime,jdbcType=VARCHAR},</if><if test="tilepath != null">tilepath = #{tilepath,jdbcType=VARCHAR},</if></set>where id = #{id,jdbcType=VARCHAR}</update><update id="updateByPrimaryKey" parameterType="com.xuebaosoft.mybatis3.mybatis_mapper.model.Users">update usersset username = #{username,jdbcType=VARCHAR},password = #{password,jdbcType=VARCHAR},name = #{name,jdbcType=VARCHAR},nickname = #{nickname,jdbcType=VARCHAR},sex = #{sex,jdbcType=VARCHAR},picture = #{picture,jdbcType=VARCHAR},createtime = #{createtime,jdbcType=VARCHAR},lastlogintime = #{lastlogintime,jdbcType=VARCHAR},tilepath = #{tilepath,jdbcType=VARCHAR}where id = #{id,jdbcType=VARCHAR}</update><select id="userPagingList" parameterType="map"resultMap="BaseResultMap">select<include refid="Base_Column_List" />from users limit #{offset},#{pageSize}</select></mapper>
6.junit進行測試,
package com.xuebaosoft.mybatis3.mybatis_mapper;import java.util.List;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import com.xuebaosoft.mybatis3.mybatis_mapper.model.Users;import com.xuebaosoft.mybatis3.mybatis_mapper.service.UserService;import com.xuebaosoft.mybatis3.mybatis_mapper.service.impl.UserServiceImpl;public class AppTest extends TestCase {public AppTest(String testName) {super(testName);}public static Test suite() {return new TestSuite(AppTest.class);}public void testApp() {UserService userService = new UserServiceImpl();//Users user = new Users("123321", "zhangsan", "111111", "name",//"nickname", "sex", "picture", "createtime",//"lastlogintime", "tilepath");//userService.update(user);Users user = userService.getUserById("123321");System.out.println(user);List<Users> list = userService.getUsers(0, 10);System.out.println(list);}}
使用mybatis3--mapping檔案方式