经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Mybatis如何自动生成sql语句
来源:jb51  时间:2021/12/15 8:46:12  对本文有异议

Mybatis自动生成sql语句

创建maven项目,将该配置文件运行即可生成 sql 语句

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <!-- MyBatis 自动生成sql代码 -->
  4. <generatorConfiguration>
  5. <!-- 导入jar包(路径) -->
  6. <classPathEntry location="E:\CourseWare\MYSQL\mysql-connector-java-5.1.26-bin.jar" />
  7. <!-- 设置生成代码的规则 targetRuntime 开发环境使用Mybatis3的版本 -->
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
  10. <commentGenerator>
  11. <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
  12. <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
  13. <property name="suppressDate" value="true" />
  14. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  15. <property name="suppressAllComments" value="false" />
  16. </commentGenerator>
  17. <!-- 连接数据库的四要素 -->
  18. <jdbcConnection
  19. driverClass="com.mysql.jdbc.Driver"
  20. connectionURL="jdbc:mysql://localhost:3306/user"
  21. userId="root"
  22. password="root">
  23. </jdbcConnection>
  24. <!-- 该属性用于指定MyBatis生成器是否应该强制使用java.math。小数点和数字域的BigDecimal -->
  25. <javaTypeResolver>
  26. <property name="forceBigDecimals" value="false" />
  27. </javaTypeResolver>
  28. <!-- 定义实体类 bean -->
  29. <javaModelGenerator targetPackage="en.et.entity" targetProject="src/main/java">
  30. <property name="enableSubPackages" value="true" />
  31. <property name="trimStrings" value="true" />
  32. </javaModelGenerator>
  33. <!-- 接口映射的注解 或者xml文件路径 -->
  34. <sqlMapGenerator targetPackage="cn.et.resource" targetProject="src/main/java">
  35. <property name="enableSubPackages" value="true" />
  36. </sqlMapGenerator>
  37. <!-- 生成的接口所在的位置 type="xml 或者 注解" -->
  38. <javaClientGenerator type="ANNOTATEDMAPPER"
  39. targetPackage="en.et.dao" targetProject="src/main/java">
  40. <property name="enableSubPackages" value="true" />
  41. </javaClientGenerator>
  42. <!-- 告诉mbg 需要生成代码的数据库的表 -->
  43. <table tableName="emp"></table>
  44. </context>
  45. </generatorConfiguration>

Mybatis的动态sql语句

Mybatis的动态sql语句主要解决的问题是不同条件sql语句的拼接。

例如:根据用户信息,查询用户列表,当不知道根据的是用户的什么信息时,写出查询的SQL语句是有一定困难的,而动态SQL语句主要解决的就是此类问题。

if标签的使用

在持久层接口定义方法

  1. /**
  2. * 根据用户信息,查询用户列表
  3. * @param user
  4. * @return
  5. */
  6. List<User> findByUser(User user);

编写持久层接口对应的映射文件

  1. <!-- 根据用户信息,查询用户列表 -->
  2. <select id="findByUser" resultType="User" parameterType="User">
  3. select *from user where 1 = 1
  4. <if test="id != 0">
  5. and id = #{id}
  6. </if>
  7. <if test="username != null and username != '' ">
  8. and username like #{username}
  9. </if>
  10. <if test="birthday != null">
  11. and birthday = #{birthday}
  12. </if>
  13. <if test="sex != null">
  14. and sex = #{sex}
  15. </if>
  16. <if test="address != null">
  17. and address = #{address}
  18. </if>
  19. </select>

编写测试方法

  1. /**
  2. * 根据用户信息,查询用户列表
  3. */
  4. @Test
  5. public void testFindByUser()
  6. {
  7. User user = new User();
  8. user.setUsername("%王%");
  9. List<User> users = userDao.findByUser(user);
  10. for (User u : users)
  11. {
  12. System.out.println(u);
  13. }
  14. }

where标签的使用

为了简化上面 where 1=1 的条件拼接,我们可以采用标签来简化开发,因此修改持久层映射文件

  1. <!-- 根据用户信息,查询用户列表 -->
  2. <select id="findByUser" resultType="User" parameterType="User">
  3. select *from user
  4. <where>
  5. <if test="id != 0">
  6. and id = #{id}
  7. </if>
  8. <if test="username != null and username != '' ">
  9. and username like #{username}
  10. </if>
  11. <if test="birthday != null">
  12. and birthday = #{birthday}
  13. </if>
  14. <if test="sex != null">
  15. and sex = #{sex}
  16. </if>
  17. <if test="address != null">
  18. and address = #{address}
  19. </if>
  20. </where>
  21. </select>

foreach标签的使用

froeach是对一个集合进行遍历,通常在构建in条件语句的时候应用

例如:根据一个用户id集合查询用户。

对id集合进行封装,加入到List集合

在这里插入图片描述

编写持久层接口方法

  1. /**
  2. * 根据id集合查询用户
  3. * @param queryUR
  4. * @return
  5. */
  6. List<User> findInIds(QueryUR queryUR);

编写持久层接口映射文件

  1. <!--根据id集合查询用户 -->
  2. <select id="findInIds" resultType="user" parameterType="int">
  3. select *from user
  4. <where>
  5. <if test="ids != null and ids.size() > 0">
  6. <!-- foreach:用于遍历集合
  7. collection:代表要遍历的集合
  8. open:代表语句的开始部分
  9. close:代表语句的结束部分
  10. item:代表需要遍历的集合的每个元素
  11. sperator:代表分隔符
  12. -->
  13. <foreach collection="ids" open="id in (" close=")" item="uid" separator=",">
  14. #{uid}
  15. </foreach>
  16. </if>
  17. </where>
  18. </select>

编写测试方法

  1. /**
  2. * 根据id集合查询用户
  3. */
  4. @Test
  5. public void testFindInIds()
  6. {
  7. QueryUR queryUR = new QueryUR();
  8. List<Integer> ids = new ArrayList<Integer>();
  9. ids.add(41);
  10. ids.add(43);
  11. ids.add(45);
  12. queryUR.setIds(ids);
  13. List<User> users = userDao.findInIds(queryUR);
  14. for (User user : users)
  15. {
  16. System.out.println(user);
  17. }
  18. }

sql语句的简化编写

在映射文件中,可以将重复的sql语句通过sql标签提取出来,使用include标签引用即可,已达到sql重用的效果。如下所示:

  1. <!--抽取重复的语句代码片段-->
  2. <sql id="querySql">
  3. select *from user
  4. </sql>
  5. <select id="findAll" resultType="USER" >
  6. <include refid="querySql"></include>
  7. </select>
  8. <!--根据id查询用户-->
  9. <select id="findById" resultType="User" parameterType="int">
  10. <include refid="querySql"></include> where id= #{userID};
  11. </select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号