Browse Source

新增 typeHanler 级联查询支持

jobob 8 years ago
parent
commit
dd6fb7f439

+ 71 - 58
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/URPTest.java

@@ -1,3 +1,18 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.test.mysql;
 
 import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
@@ -11,69 +26,67 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import java.io.InputStream;
 
 /**
+ * <p>
  * 对User, Role及el, typeHandler, resultMap进行测试
- *
+ * </p>
  *
  * @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);
-
-
-
-    }
-
+	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);
+
+	}
 
 }

+ 20 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/Role.java

@@ -1,12 +1,26 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.test.mysql.entity;
 
+import java.io.Serializable;
+
 import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableName;
 
-import java.io.Serializable;
-import java.util.List;
-
 /**
  * <p>
  * 测试角色类
@@ -15,7 +29,7 @@ import java.util.List;
  * @author sjy
  * @Date 2016-09-09
  */
-@TableName(value = "role", resultMap = "RoleMap")
+@TableName(resultMap = "RoleMap")
 public class Role implements Serializable {
 
 	@TableField(exist = false)
@@ -66,14 +80,9 @@ public class Role implements Serializable {
 		this.description = description;
 	}
 
-
 	@Override
 	public String toString() {
-		return "Role{" +
-				"id=" + id +
-				", name='" + name + '\'' +
-				", sort=" + sort +
-				", description='" + description + '\'' +
-				'}';
+		return "Role{" + "id=" + id + ", name='" + name + '\'' + ", sort=" + sort 
+				+ ", description='" + description + '\'' + '}';
 	}
 }

+ 37 - 18
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/typehandler/PhoneTypeHandler.java

@@ -1,3 +1,18 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
 package com.baomidou.mybatisplus.test.mysql.typehandler;
 
 import com.baomidou.mybatisplus.test.mysql.entity.PhoneNumber;
@@ -12,31 +27,35 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 
 /**
+ * <p>
+ * 自定义测试 TypeHandler
+ * </p>
+ * 
  * @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 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, 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(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));
-    }
+	@Override
+	public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+		return new PhoneNumber(cs.getString(columnIndex));
+	}
 }

+ 1 - 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="Yecha0828" />
+				<property name="password" value="123456" />
 			<!-- 
 				<property name="driver" value="${jdbc.driver}" />
 				<property name="url" value="${jdbc.url}" />

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

@@ -17,8 +17,11 @@
 		<result column="name" property="name"/>
 		<result column="age" property="age"/>
 		<result column="test_type" property="testType"/>
+
+		<!-- 自定义 typeHandler 语法 -->
 		<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>