Mybatis如何自動(dòng)生成sql語(yǔ)句
Mybatis自動(dòng)生成sql語(yǔ)句
創(chuàng)建maven項(xiàng)目,將該配置文件運(yùn)行即可生成 sql 語(yǔ)句
<?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 自動(dòng)生成sql代碼 --> <generatorConfiguration> <!-- 導(dǎo)入jar包(路徑) --> <classPathEntry location="E:\CourseWare\MYSQL\mysql-connector-java-5.1.26-bin.jar" /> <!-- 設(shè)置生成代碼的規(guī)則 targetRuntime 開(kāi)發(fā)環(huán)境使用Mybatis3的版本 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin> <commentGenerator> <!-- 這個(gè)元素用來(lái)去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) --> <!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段,整個(gè)實(shí)體類所有屬性都會(huì)發(fā)生變化,不利于版本控制,所以設(shè)置為true --> <property name="suppressDate" value="true" /> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!-- 連接數(shù)據(jù)庫(kù)的四要素 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/user" userId="root" password="root"> </jdbcConnection> <!-- 該屬性用于指定MyBatis生成器是否應(yīng)該強(qiáng)制使用java.math。小數(shù)點(diǎn)和數(shù)字域的BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 定義實(shí)體類 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ù)庫(kù)的表 --> <table tableName="emp"></table> </context> </generatorConfiguration>
Mybatis的動(dòng)態(tài)sql語(yǔ)句
Mybatis的動(dòng)態(tài)sql語(yǔ)句主要解決的問(wèn)題是不同條件sql語(yǔ)句的拼接。
例如:根據(jù)用戶信息,查詢用戶列表,當(dāng)不知道根據(jù)的是用戶的什么信息時(shí),寫(xiě)出查詢的SQL語(yǔ)句是有一定困難的,而動(dòng)態(tài)SQL語(yǔ)句主要解決的就是此類問(wèn)題。
if標(biāo)簽的使用
在持久層接口定義方法
/** * 根據(jù)用戶信息,查詢用戶列表 * @param user * @return */ List<User> findByUser(User user);
編寫(xiě)持久層接口對(duì)應(yīng)的映射文件
<!-- 根據(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>
編寫(xiě)測(cè)試方法
/** * 根據(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標(biāo)簽的使用
為了簡(jiǎn)化上面 where 1=1 的條件拼接,我們可以采用標(biāo)簽來(lái)簡(jiǎn)化開(kāi)發(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標(biāo)簽的使用
froeach是對(duì)一個(gè)集合進(jìn)行遍歷,通常在構(gòu)建in條件語(yǔ)句的時(shí)候應(yīng)用
例如:根據(jù)一個(gè)用戶id集合查詢用戶。
對(duì)id集合進(jìn)行封裝,加入到List集合
編寫(xiě)持久層接口方法
/** * 根據(jù)id集合查詢用戶 * @param queryUR * @return */ List<User> findInIds(QueryUR queryUR);
編寫(xiě)持久層接口映射文件
<!--根據(jù)id集合查詢用戶 --> <select id="findInIds" resultType="user" parameterType="int"> select *from user <where> <if test="ids != null and ids.size() > 0"> <!-- foreach:用于遍歷集合 collection:代表要遍歷的集合 open:代表語(yǔ)句的開(kāi)始部分 close:代表語(yǔ)句的結(jié)束部分 item:代表需要遍歷的集合的每個(gè)元素 sperator:代表分隔符 --> <foreach collection="ids" open="id in (" close=")" item="uid" separator=","> #{uid} </foreach> </if> </where> </select>
編寫(xiě)測(cè)試方法
/** * 根據(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語(yǔ)句的簡(jiǎn)化編寫(xiě)
在映射文件中,可以將重復(fù)的sql語(yǔ)句通過(guò)sql標(biāo)簽提取出來(lái),使用include標(biāo)簽引用即可,已達(dá)到sql重用的效果。如下所示:
<!--抽取重復(fù)的語(yǔ)句代碼片段--> <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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Data MongoDB中實(shí)現(xiàn)自定義級(jí)聯(lián)的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Data MongoDB中實(shí)現(xiàn)自定義級(jí)聯(lián)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

spring cloud 的監(jiān)控turbine-rabbitmq的示例

Mybatis實(shí)現(xiàn)動(dòng)態(tài)增刪改查功能的示例代碼

Presto支持Elasticsearch數(shù)據(jù)源配置詳解

排查Failed?to?validate?connection?com.mysql.cj.jdbc.Connec

java中String StringBuffer和StringBuilder的區(qū)別詳解