使用mybatis3--mapping檔案方式

來源:互聯網
上載者:User

標籤:

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檔案方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.