From dfcdf0b0a93e66afd1add6fde181ce1daa394b76 Mon Sep 17 00:00:00 2001 From: qiurunze Date: Mon, 21 Jan 2019 16:39:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4mybatis=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/mybatis-code.md | 85 ++++++++++++++++- pom.xml | 19 +++- .../miaosha/access/AccessInterceptor.java | 6 +- .../miaosha/mybatis/Mapper/UserMapper.java | 5 + .../mybatis/controller/UbatisController.java | 28 ++++++ .../miaosha/mybatis/{ => vo}/TeacherVo.java | 13 ++- src/main/resources/application.properties | 21 ++++- src/main/resources/generatorConfig.xml | 92 +++++++++++++++++++ src/main/resources/mybatis/conf.xml | 3 + .../resources/mybatis/mapper/userMapper.xml | 40 +++++++- src/main/resources/mybatis/mybatis-config.xml | 30 ++++++ 12 files changed, 329 insertions(+), 15 deletions(-) rename src/main/java/com/geekq/miaosha/mybatis/{ => vo}/TeacherVo.java (50%) create mode 100644 src/main/resources/generatorConfig.xml create mode 100644 src/main/resources/mybatis/mybatis-config.xml diff --git a/README.md b/README.md index e3fd493..e01be32 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ [![Travis](https://img.shields.io/badge/language-Java-yellow.svg)](https://github.com/qiurunze123) 高并发大流量如何进行秒杀架构,我对这部分知识做了一个系统的整理,写了一套系统。本GitHub还有许多其他的知识,随时欢迎探讨与骚扰!本文还在更新如果文章出现瑕疵请及时与我联系! -文章还有许多不足,我仍在不断改进!如果你本地没有这些环境,可以先找我要我的阿里云地址,看效果! ps: 本文章基础思路来自于若鱼1919老师!大家可以关注老师的课和博客很不错,老师很nice! 谢谢大家 ! +文章还有许多不足,我仍在不断改进!如果你本地没有这些环境,可以先找我要我的阿里云地址,看效果! ps: 本文章基础思路来自于若鱼1919老师!大家可以关注老师的课和博客很不错,老师很nice! 谢谢大家 !课程地址:https://coding.imooc.com/class/168.html 一点小建议:学习本系列知识之前,如果你完全没接触过 `MQ`、`SpringBoot`、`Redis`、`Dubbo`、`ZK` 、`Maven`,`lua`等,那么我建议你可以先在网上搜一下每一块知识的快速入门, 也可以下载本项目边做边学习,我的项目完全是实战加讲解不想写一堆的文章,浪费我们的生命,你还不懂内层含义,想要明白就边实际操作边学习,效果会更好!加油💪💪 diff --git a/docs/mybatis-code.md b/docs/mybatis-code.md index ff4c323..5e244fc 100644 --- a/docs/mybatis-code.md +++ b/docs/mybatis-code.md @@ -2,9 +2,90 @@ 有问题或者宝贵意见联系我的QQ,非常希望你的加入! -> mybatis 使用 +> mybatis 使用总结 #### resultType 和 resultMap MyBatis的每一个查询映射的返回类型都是ResultMap, 只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性, - 而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。 \ No newline at end of file + 而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。 + +#### typeAliases类型命名 + + 存在的意义在于减少类的完全限定名的冗余 + + _user可以用在任何需要com.geekq.miaosha.mybatis.User + +### 当实体类中的属性名和表中的字段名不一致时使用MyBatis进行查询操作时无法查询出相应的结果的问题以及针对问题采用的两种办法 + + 解决办法一: 通过在查询的sql语句中定义字段名的别名,让字段名的别名和实体类的属性名一致, + 这样就可以表的字段名和实体类的属性名一一对应上了,这种方式是通过在sql语句中定义别名来解决字段名和属性名的映射关系的 + 解决办法二: 通过来映射字段名和实体类属性名的一一对应关系。 + 这种方式是使用MyBatis提供的解决方式来解决字段名和属性名的映射关系的。 + +### 为什么order by 要用${xxx} 而不用 #{} + + 对于形如#{variable} + 的变量,Mybatis会将其视为字符串值,在变量替换成功后,缺省地给变量值加上引号。"variable" + (2)对于形如${variable}的变量,Mybatis会将其视作直接变量,即在变量替换成功后,不会再给其加上引号。 + variable + 所以在动态sql中,#{variable} + 需要去掉 "",比如正常sql赋值一般是这样的and name= #{name},因为是=赋值,所以会获取内容,去掉"" + ${variable}可以直接使用,比如order + by ${name} 传入的直接是name,不带双引号,可以直接使用, + 并且order + by不是 =赋值,所以如果直接order by #{name},结果是order + by "name",自然无法执行了 + +### 如何打印sql日志 ? + + xml方式 + + + + + 配置方式: + + #打印mybatis sql + log4j.logger.com.ibatis=DEBUG + log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG + log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG + log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG + log4j.logger.Java.sql.Connection=DEBUG + log4j.logger.java.sql.Statement=DEBUG + log4j.logger.java.sql.PreparedStatement=DEBUG +### 动态sql标签 + + if + choose (when, otherwise) + trim (where, set) + foreach + +### 如何使用mybatis-generator:generate + + pom 配置: + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.2 + + true + false + + + + mysql + mysql-connector-java + 5.1.21 + + + + + +![使用](https://raw.githubusercontent.com/qiurunze123/imageall/master/mybatis1.png) +![使用](https://raw.githubusercontent.com/qiurunze123/imageall/master/mybatis2.png) + +### generatorConfig.xml 内容解析? + 已在其中备注,详细内容请见generatorConfig.xml + +### generatorConfig.xml 内容解析? \ No newline at end of file diff --git a/pom.xml b/pom.xml index 776c0eb..80733b8 100644 --- a/pom.xml +++ b/pom.xml @@ -175,7 +175,22 @@ org.springframework.boot spring-boot-maven-plugin - + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.2 + + true + false + + + + mysql + mysql-connector-java + 5.1.21 + + + org.springframework.boot spring-boot-maven-plugin @@ -193,6 +208,8 @@ + + src/main/java diff --git a/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java b/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java index 9f7ea8f..4513ea2 100644 --- a/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java +++ b/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java @@ -44,9 +44,9 @@ public class AccessInterceptor extends HandlerInterceptorAdapter{ logger.info("打印拦截方法handler :{} ",handler); HandlerMethod hm = (HandlerMethod)handler; //方便mybatis 测试 - if(hm.getMethod().getName().startsWith("test")){ - return true; - } +// if(hm.getMethod().getName().startsWith("test")){ +// return true; +// } MiaoshaUser user = getUser(request, response); UserContext.setUser(user); AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); diff --git a/src/main/java/com/geekq/miaosha/mybatis/Mapper/UserMapper.java b/src/main/java/com/geekq/miaosha/mybatis/Mapper/UserMapper.java index be95d7a..c59e052 100644 --- a/src/main/java/com/geekq/miaosha/mybatis/Mapper/UserMapper.java +++ b/src/main/java/com/geekq/miaosha/mybatis/Mapper/UserMapper.java @@ -2,6 +2,7 @@ package com.geekq.miaosha.mybatis.Mapper; import com.geekq.miaosha.mybatis.entity.User; +import com.geekq.miaosha.mybatis.vo.TeacherVo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.omg.CORBA.INTERNAL; @@ -36,4 +37,8 @@ public interface UserMapper { */ public List getUserListMap(@Param("id") Integer id , @Param("name") String name); + public List getTeacherAndUser( @Param("uId") Integer uId ); + + public List getTeacherAndUserList( @Param("uId") List uId ); + } diff --git a/src/main/java/com/geekq/miaosha/mybatis/controller/UbatisController.java b/src/main/java/com/geekq/miaosha/mybatis/controller/UbatisController.java index e1a732b..7541e80 100644 --- a/src/main/java/com/geekq/miaosha/mybatis/controller/UbatisController.java +++ b/src/main/java/com/geekq/miaosha/mybatis/controller/UbatisController.java @@ -3,6 +3,7 @@ package com.geekq.miaosha.mybatis.controller; import com.geekq.miaosha.access.AccessLimit; import com.geekq.miaosha.mybatis.Mapper.UserMapper; import com.geekq.miaosha.mybatis.entity.User; +import com.geekq.miaosha.mybatis.vo.TeacherVo; import com.geekq.miaosha.redis.KeyPrefix; import com.geekq.miaosha.redis.RedisService; import org.apache.commons.lang3.StringUtils; @@ -18,6 +19,7 @@ import org.thymeleaf.spring4.view.ThymeleafViewResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; +import java.util.ArrayList; import java.util.List; @Controller @@ -73,4 +75,30 @@ public class UbatisController { int result = userMapper.delete(2 ); System.out.println(result); } + + + /** + * 测试多表联合查询 + */ + @RequestMapping(value = "/testTandU", produces = "text/html") + @ResponseBody + public void testTandU(){ + List teacherAndUser = userMapper.getTeacherAndUser(1 ); + System.out.println(teacherAndUser.size()); + } + + + /** + * 测试多表联合查询 in + */ + @RequestMapping(value = "/testTandUIn", produces = "text/html") + @ResponseBody + public void testTandUIn(){ + List list = new ArrayList(); + + list.add(1); + list.add(2); + List teacherAndUser = userMapper.getTeacherAndUserList(list ); + System.out.println(teacherAndUser.size()); + } } diff --git a/src/main/java/com/geekq/miaosha/mybatis/TeacherVo.java b/src/main/java/com/geekq/miaosha/mybatis/vo/TeacherVo.java similarity index 50% rename from src/main/java/com/geekq/miaosha/mybatis/TeacherVo.java rename to src/main/java/com/geekq/miaosha/mybatis/vo/TeacherVo.java index a426c2c..255d5ed 100644 --- a/src/main/java/com/geekq/miaosha/mybatis/TeacherVo.java +++ b/src/main/java/com/geekq/miaosha/mybatis/vo/TeacherVo.java @@ -1,4 +1,4 @@ -package com.geekq.miaosha.mybatis; +package com.geekq.miaosha.mybatis.vo; import lombok.Getter; import lombok.Setter; @@ -8,11 +8,16 @@ import java.io.Serializable; @Setter @Getter public class TeacherVo implements Serializable { - private Integer cid ; - private String cName; + private String uId; - private String teacherId ; + private String tId ; private String tName ; + + private String name; + + private Integer age ; + + private String address ; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1241787..6c07bc2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,18 +9,33 @@ spring.thymeleaf.mode=HTML5 #是否开启缓存 pageCache.enbale=true + + +#打印mybatis sql +log4j.logger.com.ibatis=DEBUG +log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG +log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG +log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG +log4j.logger.Java.sql.Connection=DEBUG +log4j.logger.java.sql.Statement=DEBUG +log4j.logger.java.sql.PreparedStatement=DEBUG + #mybatis mybatis.type-aliases-package=com.geekq.miaosha.domain -#开启驼峰转换 +#开启驼峰转换 configuration config-location 不能同時存在 如果要走流程 请 放开注释 mybatis.configuration.map-underscore-to-camel-case=true #mybatis.mapperLocations = classpath:com/geekq/miaosha/dao/*.xml + mybatis.mapperLocations=classpath:mybatis/mapper/*.xml +#配置xml方式 因为与 mybatis.configuration.map-underscore-to-camel-case 仅用于测试 +#mybatis.config-location=classpath:mybatis/mybatis-config.xml + #add mybatis -mybatis.config-locations=classpath:mybatis/conf.xml +mybatis. #datasource spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false spring.datasource.username=root -spring.dataource.password=nihaoma +spring.datasource.password=nihaoma spring.datasource.driver-class-name=com.mysql.jdbc.Driver #druid spring.datasource.type=com.alibaba.druid.pool.DruidDataSource diff --git a/src/main/resources/generatorConfig.xml b/src/main/resources/generatorConfig.xml new file mode 100644 index 0000000..f61c618 --- /dev/null +++ b/src/main/resources/generatorConfig.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
\ No newline at end of file diff --git a/src/main/resources/mybatis/conf.xml b/src/main/resources/mybatis/conf.xml index 0ca8dc9..592618d 100644 --- a/src/main/resources/mybatis/conf.xml +++ b/src/main/resources/mybatis/conf.xml @@ -2,6 +2,9 @@ + + + diff --git a/src/main/resources/mybatis/mapper/userMapper.xml b/src/main/resources/mybatis/mapper/userMapper.xml index ca7fdc1..92184fe 100644 --- a/src/main/resources/mybatis/mapper/userMapper.xml +++ b/src/main/resources/mybatis/mapper/userMapper.xml @@ -7,12 +7,18 @@ userMapper(userMapper.xml文件去除后缀)保证唯一性 + + + + + + + + + + + + id,name,age,_address @@ -56,6 +78,22 @@ userMapper(userMapper.xml文件去除后缀)保证唯一性 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file