标签:
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 users where id = #{id,jdbcType=VARCHAR} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> delete from users where 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 users set 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); } }
标签:
原文地址:http://my.oschina.net/u/555061/blog/507487