Mybatis如何自動生成sql語句
Mybatis自動生成sql語句
創(chuàng)建maven項目,將該配置文件運行即可生成 sql 語句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- MyBatis 自動生成sql代碼 -->
<generatorConfiguration>
<!-- 導入jar包(路徑) -->
<classPathEntry location="E:\CourseWare\MYSQL\mysql-connector-java-5.1.26-bin.jar" />
<!-- 設置生成代碼的規(guī)則 targetRuntime 開發(fā)環(huán)境使用Mybatis3的版本 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<commentGenerator>
<!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護 -->
<!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發(fā)生變化,不利于版本控制,所以設置為true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!-- 連接數(shù)據(jù)庫的四要素 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/user"
userId="root"
password="root">
</jdbcConnection>
<!-- 該屬性用于指定MyBatis生成器是否應該強制使用java.math。小數(shù)點和數(shù)字域的BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 定義實體類 bean -->
<javaModelGenerator targetPackage="en.et.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 接口映射的注解 或者xml文件路徑 -->
<sqlMapGenerator targetPackage="cn.et.resource" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成的接口所在的位置 type="xml 或者 注解" -->
<javaClientGenerator type="ANNOTATEDMAPPER"
targetPackage="en.et.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 告訴mbg 需要生成代碼的數(shù)據(jù)庫的表 -->
<table tableName="emp"></table>
</context>
</generatorConfiguration>
Mybatis的動態(tài)sql語句
Mybatis的動態(tài)sql語句主要解決的問題是不同條件sql語句的拼接。
例如:根據(jù)用戶信息,查詢用戶列表,當不知道根據(jù)的是用戶的什么信息時,寫出查詢的SQL語句是有一定困難的,而動態(tài)SQL語句主要解決的就是此類問題。
if標簽的使用
在持久層接口定義方法
/**
* 根據(jù)用戶信息,查詢用戶列表
* @param user
* @return
*/
List<User> findByUser(User user);
編寫持久層接口對應的映射文件
<!-- 根據(jù)用戶信息,查詢用戶列表 -->
<select id="findByUser" resultType="User" parameterType="User">
select *from user where 1 = 1
<if test="id != 0">
and id = #{id}
</if>
<if test="username != null and username != '' ">
and username like #{username}
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="sex != null">
and sex = #{sex}
</if>
<if test="address != null">
and address = #{address}
</if>
</select>
編寫測試方法
/**
* 根據(jù)用戶信息,查詢用戶列表
*/
@Test
public void testFindByUser()
{
User user = new User();
user.setUsername("%王%");
List<User> users = userDao.findByUser(user);
for (User u : users)
{
System.out.println(u);
}
}
where標簽的使用
為了簡化上面 where 1=1 的條件拼接,我們可以采用標簽來簡化開發(fā),因此修改持久層映射文件
<!-- 根據(jù)用戶信息,查詢用戶列表 -->
<select id="findByUser" resultType="User" parameterType="User">
select *from user
<where>
<if test="id != 0">
and id = #{id}
</if>
<if test="username != null and username != '' ">
and username like #{username}
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="sex != null">
and sex = #{sex}
</if>
<if test="address != null">
and address = #{address}
</if>
</where>
</select>
foreach標簽的使用
froeach是對一個集合進行遍歷,通常在構建in條件語句的時候應用
例如:根據(jù)一個用戶id集合查詢用戶。
對id集合進行封裝,加入到List集合

編寫持久層接口方法
/**
* 根據(jù)id集合查詢用戶
* @param queryUR
* @return
*/
List<User> findInIds(QueryUR queryUR);
編寫持久層接口映射文件
<!--根據(jù)id集合查詢用戶 -->
<select id="findInIds" resultType="user" parameterType="int">
select *from user
<where>
<if test="ids != null and ids.size() > 0">
<!-- foreach:用于遍歷集合
collection:代表要遍歷的集合
open:代表語句的開始部分
close:代表語句的結束部分
item:代表需要遍歷的集合的每個元素
sperator:代表分隔符
-->
<foreach collection="ids" open="id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
編寫測試方法
/**
* 根據(jù)id集合查詢用戶
*/
@Test
public void testFindInIds()
{
QueryUR queryUR = new QueryUR();
List<Integer> ids = new ArrayList<Integer>();
ids.add(41);
ids.add(43);
ids.add(45);
queryUR.setIds(ids);
List<User> users = userDao.findInIds(queryUR);
for (User user : users)
{
System.out.println(user);
}
}
sql語句的簡化編寫
在映射文件中,可以將重復的sql語句通過sql標簽提取出來,使用include標簽引用即可,已達到sql重用的效果。如下所示:
<!--抽取重復的語句代碼片段-->
<sql id="querySql">
select *from user
</sql>
<select id="findAll" resultType="USER" >
<include refid="querySql"></include>
</select>
<!--根據(jù)id查詢用戶-->
<select id="findById" resultType="User" parameterType="int">
<include refid="querySql"></include> where id= #{userID};
</select>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Data MongoDB中實現(xiàn)自定義級聯(lián)的方法詳解
這篇文章主要給大家介紹了關于Spring Data MongoDB中實現(xiàn)自定義級聯(lián)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11
spring cloud 的監(jiān)控turbine-rabbitmq的示例
這篇文章主要介紹了spring cloud 的監(jiān)控turbine-rabbitmq的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Mybatis實現(xiàn)動態(tài)增刪改查功能的示例代碼
這篇文章主要介紹了Mybatis實現(xiàn)動態(tài)增刪改查功能的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Presto支持Elasticsearch數(shù)據(jù)源配置詳解
這篇文章主要為大家介紹了Presto支持Elasticsearch數(shù)據(jù)源配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
排查Failed?to?validate?connection?com.mysql.cj.jdbc.Connec
這篇文章主要介紹了Failed?to?validate?connection?com.mysql.cj.jdbc.ConnectionImpl問題排查,具有很好的參考價值,希望對大家有所幫助2023-02-02
java中String StringBuffer和StringBuilder的區(qū)別詳解
大家好,本篇文章主要講的是java中String StringBuffer和StringBuilder的區(qū)別詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

