Commit 2a7a53d3 authored by 闲人's avatar 闲人

Revert "Merge branch 'master' into 'dev201912'"

This reverts merge request !3
parent 7c4d6dba
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="MybatisTraning" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="MybatisTraning" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.alibaba:fastjson:1.2.56">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/alibaba/fastjson/1.2.56/fastjson-1.2.56.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/alibaba/fastjson/1.2.56/fastjson-1.2.56-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/alibaba/fastjson/1.2.56/fastjson-1.2.56-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.google.protobuf:protobuf-java:3.6.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: junit:junit:4.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: mysql:mysql-connector-java:8.0.16">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.hamcrest:hamcrest-core:1.3">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.mybatis:mybatis:3.4.5">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.4.5/mybatis-3.4.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.4.5/mybatis-3.4.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.4.5/mybatis-3.4.5-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/MybatisTraning.iml" filepath="$PROJECT_DIR$/MybatisTraning.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.16" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.5" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.56" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cesgroup</groupId>
<artifactId>MybatisTraning</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
\ No newline at end of file
package com.cesgroup.mybatis.entity;
import java.io.Serializable;
import com.alibaba.fastjson.JSONObject;
public class User implements Serializable{
int id;
String name;
JSONObject info;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public JSONObject getInfo() {
return info;
}
public void setInfo(JSONObject info) {
this.info = info;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", info=" + JSONObject.toJSONString(info) + "]";
}
}
package com.cesgroup.mybatis.handler;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MyTypeHandler extends BaseTypeHandler<JSONObject> {
@Override
public JSONObject getNullableResult(ResultSet rs, String s) throws SQLException {
String infoJsonString= rs.getString(s);
return infoJsonString==null?null:JSONObject.parseObject(infoJsonString);
}
@Override
public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
return null;
}
@Override
public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {
try {
String userInfoString= JSONObject.toJSONString(infoObject);
statement.setString(i, userInfoString);
} catch (Exception e) {
System.out.println("----将对象转化为字符串失败!----");
}
}
@Override
public JSONObject getNullableResult(ResultSet arg0, int arg1) throws SQLException {
return null;
}
}
package com.cesgroup.mybatis.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.cesgroup.mybatis.handler.MyTypeHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@Intercepts({
@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
@Signature(type = StatementHandler.class, method = "batch", args = { Statement.class })
})
public class MybatisLogInterceptor implements Interceptor {
final Log log = LogFactory.getLog(MyTypeHandler.class);
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
long startTime = System.currentTimeMillis();
StatementHandler statementHandler = (StatementHandler)target;
try {
return invocation.proceed();
} finally {
long endTime = System.currentTimeMillis();
long sqlCost = endTime - startTime;
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappingList = boundSql.getParameterMappings();
// 格式化Sql语句,去除换行符,替换参数
sql = formatSql(sql, parameterObject, parameterMappingList);
log.warn("执行耗时[" + sqlCost + "ms] " + "SQL:" + sql + ";");
}
}
private String formatSql(String sql, Object parameterObject, List<ParameterMapping> parameterMappingList) {
sql = sql.replaceAll("\r|\n|\t", " ").replaceAll("\\s+", " ");
if(parameterObject == null){
sql = sql.replaceAll("\\?", " null");
return sql;
}
if(Map.class.isAssignableFrom(parameterObject.getClass())){
Map pb = (Map)parameterObject;
for(ParameterMapping temp: parameterMappingList){
if (pb.get(temp.getProperty()) != null){
if (pb.get(temp.getProperty()).getClass().isAssignableFrom(JSONObject.class)){
JSONObject infObject = (JSONObject) pb.get(temp.getProperty());
sql = sql.replaceFirst("\\?","'" + infObject.toJSONString() + "'");
}else {
sql = sql.replaceFirst("\\?","'" + pb.get(temp.getProperty()) + "'");
}
}else {
sql = sql.replaceFirst("\\?","'null'");
}
}
}
if(String.class.isAssignableFrom(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", parameterObject == null ? "null" : "'" + parameterObject + "'");
}
if(Number.class.isAssignableFrom(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", parameterObject == null ? "null" : "'" + parameterObject.toString() +"'");
}
return sql;
}
public Object plugin(Object o) {
return Plugin.wrap(o, this);
}
public void setProperties(Properties properties) {
}
}
<?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="MyMapper">
<select id="selectUser" resultType="com.cesgroup.mybatis.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.cesgroup.mybatis.entity.User">
INSERT INTO user (id, name, info)
VALUES(#{id}, #{name}, #{info})
</insert>
</mapper>
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?setUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username=root
password=123456
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 根标签 -->
<configuration>
<properties resource="db.properties"></properties>
<typeHandlers>
<typeHandler handler="com.cesgroup.mybatis.handler.MyTypeHandler"></typeHandler>
</typeHandlers>
<plugins>
<plugin interceptor="com.cesgroup.mybatis.interceptor.MybatisLogInterceptor">
</plugin>
</plugins>
<environments default="mysql">
<environment id="mysql">
<!-- 事务管理器 -->
<transactionManager type="JDBC" />
<!-- 连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="MyMapper.xml" />
</mappers>
</configuration>
/*
Navicat Premium Data Transfer
Source Server : 本机
Source Server Type : MySQL
Source Server Version : 80015
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80015
File Encoding : 65001
Date: 11/12/2019 14:34:10
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for mybatis
-- ----------------------------
DROP TABLE IF EXISTS `mybatis`;
CREATE TABLE `mybatis` (
`id` int(32) NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`info` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
package com.cesgroup.mybatis;
import com.alibaba.fastjson.JSONObject;
import com.cesgroup.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class MybatisTest {
private SqlSession sqlSession=null;
private SqlSessionFactory sqlSessionFactory;
@Before
public void configMybatis() {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
sqlSession = sqlSessionFactory.openSession(false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSelectUser() {
User user = sqlSession.selectOne("MyMapper.selectUser", 1);
System.out.println(user);
sqlSession.close();
}
@Test
public void testInsertJsonObject() {
JSONObject userInfObject = new JSONObject();
userInfObject.put("test", "测试");
Map userMap = new HashMap();
userMap.put("id", 20);
userMap.put("name", "测试");
userMap.put("info", userInfObject);
int num = sqlSession.insert("MyMapper.insert",userMap);
sqlSession.commit();
System.out.println("影响的行数:" + num);
}
}
<?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="MyMapper">
<select id="selectUser" resultType="com.cesgroup.mybatis.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.cesgroup.mybatis.entity.User">
INSERT INTO user (id, name, info)
VALUES(#{id}, #{name}, #{info})
</insert>
</mapper>
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?setUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username=root
password=123456
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 根标签 -->
<configuration>
<properties resource="db.properties"></properties>
<typeHandlers>
<typeHandler handler="com.cesgroup.mybatis.handler.MyTypeHandler"></typeHandler>
</typeHandlers>
<plugins>
<plugin interceptor="com.cesgroup.mybatis.interceptor.MybatisLogInterceptor">
</plugin>
</plugins>
<environments default="mysql">
<environment id="mysql">
<!-- 事务管理器 -->
<transactionManager type="JDBC" />
<!-- 连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="MyMapper.xml" />
</mappers>
</configuration>
/*
Navicat Premium Data Transfer
Source Server : 本机
Source Server Type : MySQL
Source Server Version : 80015
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80015
File Encoding : 65001
Date: 11/12/2019 14:34:10
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for mybatis
-- ----------------------------
DROP TABLE IF EXISTS `mybatis`;
CREATE TABLE `mybatis` (
`id` int(32) NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`info` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment