Jelajahi Sumber

编写了el,typehandler, resultMap的测试用例

junyu_shi 8 tahun lalu
induk
melakukan
ba0c7cbab9

+ 2 - 2
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -129,8 +129,8 @@ public class TableInfoHelper {
 				if (StringUtils.isNotEmpty(tableField.el())) {
 					el = tableField.el();
 				}
-				String[] columns = columnName.split(",");
-				String[] els = el.split(",");
+				String[] columns = columnName.split(";");
+				String[] els = el.split(";");
 				if (null != columns && null != els && columns.length == els.length) {
 					for (int i = 0; i < columns.length; i++) {
 						fieldList.add(new TableFieldInfo(true, columns[i], field.getName(), els[i]));

+ 12 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/RoleMapper.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.mysql;
+
+import com.baomidou.mybatisplus.mapper.AutoMapper;
+import com.baomidou.mybatisplus.test.mysql.entity.Role;
+
+/**
+ * @author junyu
+ * @Date 2016-09-09
+ */
+public interface RoleMapper extends AutoMapper<Role> {
+
+}

+ 79 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/URPTest.java

@@ -0,0 +1,79 @@
+package com.baomidou.mybatisplus.test.mysql;
+
+import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
+import com.baomidou.mybatisplus.test.mysql.entity.PhoneNumber;
+import com.baomidou.mybatisplus.test.mysql.entity.Role;
+import com.baomidou.mybatisplus.test.mysql.entity.User;
+import com.baomidou.mybatisplus.toolkit.IdWorker;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import java.io.InputStream;
+
+/**
+ * 对User, Role及el, typeHandler, resultMap进行测试
+ *
+ *
+ * @author junyu
+ * @Date 2016-09-09
+ */
+public class URPTest {
+    public static void main(String[] args) {
+
+        // 加载配置文件
+        InputStream in = UserMapperTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
+        MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
+        mf.setSqlInjector(new MySqlInjector());
+        mf.setMetaObjectHandler(new MyMetaObjectHandler());
+        SqlSessionFactory sessionFactory = mf.build(in);
+        SqlSession session = sessionFactory.openSession();
+
+        UserMapper userMapper = session.getMapper(UserMapper.class);
+        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
+
+        /**
+         * sjy
+         * 测试@TableField的el属性, 级联resultMap
+         */
+        Role role = new Role();
+        role.setId(IdWorker.getId());
+        role.setName("admin");
+        int rlt = roleMapper.insert(role);
+        System.err.println("--------- insert role --------- " + rlt);
+
+        PhoneNumber phone = new PhoneNumber("81", "0576", "82453832");
+
+        User userA = new User();
+        userA.setId(IdWorker.getId());
+        userA.setName("junyu_shi");
+        userA.setAge(15);
+        userA.setTestType(1);
+        userA.setRole(role);
+        userA.setPhone(phone);
+        rlt = userMapper.insert(userA);
+        System.err.println("--------- insert user --------- " + rlt);
+
+        User whereUser = userMapper.selectOne(userA);
+        System.err.println("--------- select user --------- " + whereUser.toString());
+
+        //如果不使用el表达式, User类中就同时需要roleId用于对应User表中的字段, 和Role属性用于保存resultmap的级联查询. 在插入时, 就需要写user.setRoleId(), 然后updateUser.
+        role = new Role();
+        role.setId(IdWorker.getId());
+        role.setName("root");
+        roleMapper.insert(role);
+        userA.setRole(role);
+        userMapper.updateById(userA);
+        System.err.println("--------- upadte user's role --------- " + rlt);
+
+        whereUser = userMapper.selectOne(userA);
+        System.err.println("--------- select user --------- " + whereUser.toString());
+
+        userMapper.deleteSelective(userA);
+        System.err.println("--------- delete user --------- " + rlt);
+
+
+
+    }
+
+
+}

+ 65 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/PhoneNumber.java

@@ -0,0 +1,65 @@
+package com.baomidou.mybatisplus.test.mysql.entity;
+
+/**
+ * @author junyu
+ * @Date 2016-09-09
+ */
+
+public class PhoneNumber {
+    private String countryCode;
+    private String stateCode;
+    private String number;
+
+    public PhoneNumber() {
+    }
+
+    public PhoneNumber(String countryCode, String stateCode, String
+            number) {
+        this.countryCode = countryCode;
+        this.stateCode = stateCode;
+        this.number = number;
+    }
+
+    public PhoneNumber(String string) {
+        if (string != null) {
+            String[] parts = string.split("-");
+            if (parts.length > 0) this.countryCode = parts[0];
+            if (parts.length > 1) this.stateCode = parts[1];
+            if (parts.length > 2) this.number = parts[2];
+        }
+    }
+
+    public String getAsString() {
+        return countryCode + "-" + stateCode + "-" + number;
+
+    }
+
+    public String getCountryCode() {
+        return countryCode;
+    }
+
+    public void setCountryCode(String countryCode) {
+        this.countryCode = countryCode;
+    }
+
+    public String getStateCode() {
+        return stateCode;
+    }
+
+    public void setStateCode(String stateCode) {
+        this.stateCode = stateCode;
+    }
+
+    public String getNumber() {
+        return number;
+    }
+
+    public void setNumber(String number) {
+        this.number = number;
+    }
+
+    @Override
+    public String toString() {
+        return getAsString();
+    }
+}

+ 12 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/Role.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableName;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * <p>
@@ -14,7 +15,7 @@ import java.io.Serializable;
  * @author sjy
  * @Date 2016-09-09
  */
-@TableName(value = "role")
+@TableName(value = "role", resultMap = "RoleMap")
 public class Role implements Serializable {
 
 	@TableField(exist = false)
@@ -65,4 +66,14 @@ public class Role implements Serializable {
 		this.description = description;
 	}
 
+
+	@Override
+	public String toString() {
+		return "Role{" +
+				"id=" + id +
+				", name='" + name + '\'' +
+				", sort=" + sort +
+				", description='" + description + '\'' +
+				'}';
+	}
 }

+ 25 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/User.java

@@ -50,9 +50,13 @@ public class User implements Serializable {
 	@TableField(value = "test_type")
 	private Integer testType;
 
-	@TableField(value = "roleId,roleName", el = "role.id,role.name")
+	@TableField(el = "role.id")
 	private Role role;
 
+	//或@TableField(el = "role,jdbcType=BIGINT)
+	@TableField(el = "phone, typeHandler=com.baomidou.mybatisplus.test.mysql.typehandler.PhoneTypeHandler")
+	private PhoneNumber phone;
+
 	public User() {
 
 	}
@@ -133,6 +137,26 @@ public class User implements Serializable {
 		this.role = role;
 	}
 
+	public PhoneNumber getPhone() {
+		return phone;
+	}
+
+	public void setPhone(PhoneNumber phone) {
+		this.phone = phone;
+	}
+
+	@Override
+	public String toString() {
+		return "User{" +
+				"id=" + id +
+				", name='" + name + '\'' +
+				", age=" + age +
+				", testType=" + testType +
+				", role=" + role +
+				", phone=" + phone +
+				'}';
+	}
+
 	/**
 	 * 测试类型
 	 */

+ 42 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/typehandler/PhoneTypeHandler.java

@@ -0,0 +1,42 @@
+package com.baomidou.mybatisplus.test.mysql.typehandler;
+
+import com.baomidou.mybatisplus.test.mysql.entity.PhoneNumber;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.MappedJdbcTypes;
+import org.apache.ibatis.type.MappedTypes;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author junyu
+ * @Date 2016-09-09
+ */
+
+@MappedTypes(PhoneNumber.class)
+@MappedJdbcTypes(JdbcType.VARCHAR)
+public class PhoneTypeHandler extends BaseTypeHandler<PhoneNumber> {
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, PhoneNumber parameter, JdbcType jdbcType) throws SQLException {
+        System.err.println("--------- Executing PhoneTypeHandler --------- ");
+        ps.setString(i, parameter.getAsString());
+    }
+
+    @Override
+    public PhoneNumber getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        return new PhoneNumber(rs.getString(columnName));
+    }
+
+    @Override
+    public PhoneNumber getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        return new PhoneNumber(rs.getString(columnIndex));
+    }
+
+    @Override
+    public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        return new PhoneNumber(cs.getString(columnIndex));
+    }
+}

+ 4 - 1
mybatis-plus/src/test/resources/mysql-config.xml

@@ -82,7 +82,7 @@
 				<property name="driver" value="com.mysql.jdbc.Driver" />
 				<property name="url" value="jdbc:mysql://localhost:3306/mybatis-plus" />
 				<property name="username" value="root" />
-				<property name="password" value="123456" />
+				<property name="password" value="Yecha0828" />
 			<!-- 
 				<property name="driver" value="${jdbc.driver}" />
 				<property name="url" value="${jdbc.url}" />
@@ -93,6 +93,7 @@
 		</environment>
 	</environments>
 
+
     <!--
      | SQL映射定义Mappers
      |
@@ -103,6 +104,8 @@
      | -->
     <mappers>
         <mapper class="com.baomidou.mybatisplus.test.mysql.UserMapper" />
+        <mapper class="com.baomidou.mybatisplus.test.mysql.RoleMapper" />
         <mapper resource="mysql/UserMapper.xml"/>
+        <mapper resource="mysql/RoleMapper.xml"/>
     </mappers>
 </configuration>

+ 8 - 0
mybatis-plus/src/test/resources/mysql/RoleMapper.xml

@@ -0,0 +1,8 @@
+<?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.baomidou.mybatisplus.test.mysql.RoleMapper">
+    
+    <resultMap id="RoleMap" type="Role" autoMapping="true">
+        <id property="id" column="id"/>
+    </resultMap>
+</mapper>

+ 3 - 0
mybatis-plus/src/test/resources/mysql/UserMapper.xml

@@ -17,5 +17,8 @@
 		<result column="name" property="name"/>
 		<result column="age" property="age"/>
 		<result column="test_type" property="testType"/>
+		<result column="phone" property="phone" typeHandler="com.baomidou.mybatisplus.test.mysql.typehandler.PhoneTypeHandler"/>
+
+		<association column="role" property="role" select="com.baomidou.mybatisplus.test.mysql.RoleMapper.selectById"/>
 	</resultMap>
 </mapper>

+ 32 - 18
mybatis-plus/src/test/resources/mysql/mysql.sql

@@ -1,32 +1,46 @@
 /*
-Navicat MySQL Data Transfer
+ Navicat Premium Data Transfer
 
-Source Server         : localhost
-Source Server Version : 50615
-Source Host           : localhost:3306
-Source Database       : mybatis-plus
+ Source Server         : mysql-admin
+ Source Server Type    : MySQL
+ Source Server Version : 50621
+ Source Host           : localhost
+ Source Database       : mybatis-plus
 
-Target Server Type    : MYSQL
-Target Server Version : 50615
-File Encoding         : 65001
+ Target Server Type    : MySQL
+ Target Server Version : 50621
+ File Encoding         : utf-8
 
-Date: 2016-02-29 18:03:37
+ Date: 09/09/2016 16:38:46 PM
 */
 
-SET FOREIGN_KEY_CHECKS=0;
+SET NAMES utf8;
+SET FOREIGN_KEY_CHECKS = 0;
 
 -- ----------------------------
--- Table structure for `user`
+--  Table structure for `role`
+-- ----------------------------
+DROP TABLE IF EXISTS `role`;
+CREATE TABLE `role` (
+  `id` bigint(20) NOT NULL,
+  `name` varchar(64) DEFAULT NULL,
+  `sort` int(11) DEFAULT NULL,
+  `description` varchar(255) DEFAULT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+--  Table structure for `user`
 -- ----------------------------
 DROP TABLE IF EXISTS `user`;
 CREATE TABLE `user` (
   `test_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
-  `name` varchar(30) COLLATE utf8_bin NOT NULL COMMENT '名称',
-  `age` int(11) NOT NULL COMMENT '年龄',
-  `test_type` int(11) NOT NULL DEFAULT '0' COMMENT '测试下划线字段命名类型',
+  `name` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '名称',
+  `age` int(11) DEFAULT NULL COMMENT '年龄',
+  `test_type` int(11) DEFAULT '0' COMMENT '测试下划线字段命名类型',
+  `role` bigint(20) DEFAULT NULL,
+  `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL,
   PRIMARY KEY (`test_id`)
-) ENGINE=MyISAM AUTO_INCREMENT=398402012225470465 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户表';
+) ENGINE=MyISAM AUTO_INCREMENT=774164658700161025 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户表';
 
--- ----------------------------
--- Records of user
--- ----------------------------
+SET FOREIGN_KEY_CHECKS = 1;