Java SSM架構之MyBatis3(八)MyBatis之動態SQL

來源:互聯網
上載者:User

標籤:ima   prefix   動態   username   去除   isp   insert   XML   建立   

前言:

  mybatis架構中最具特色的便是sql語句中的自訂,而動態sql的使用又使整個架構更加靈活。

建立User表
/*Table structure for table `user` */DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(20) NOT NULL,  `username` varchar(20) NOT NULL,  `age` int(3) NOT NULL,  `phone` varchar(11) NOT NULL,  `email` varchar(50) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Data for the table `user` */insert  into `user`(`id`,`name`,`username`,`age`,`phone`,`email`) values (1,‘張三‘,‘zs‘,18,‘15010998046‘,‘[email protected]‘),(2,‘李四‘,‘ls‘,19,‘15019087600‘,‘[email protected]‘),(3,‘王五‘,‘ww‘,20,‘15010898065‘,‘[email protected]‘);/*!40101 SET [email protected]_SQL_MODE */;/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;/*!40014 SET [email protected]_UNIQUE_CHECKS */;/*!40111 SET [email protected]_SQL_NOTES */;
View Code

if標籤

User實體

View Code

UserMapper

View Code

UserMapper.xml

View Code

UserController

View Code

測試

http://localhost:8080/getUser

where標籤
<select id="getUser" resultType="cn.cnki.ref.pojo.User">        select * from user        <where>            <if test="id != null">and id=#{id} </if>            <if test="age != null">and age=#{age} </if>            <if test="name != null">and name=#{name}</if>        </where>    </select>
View Code

trim標籤下的四個屬性:

prefix:在標籤開始添加上該字串

suffixOverrides:在標籤末尾去除上該字串

suffix:在標籤末尾添加上該字串

prefixOverrides:在標籤開始去除上該字串

set標籤

 

trim標籤

 

switch\when標籤

 

foreach標籤

 

Java SSM架構之MyBatis3(八)MyBatis之動態SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.