mybatis--MyBatis动态SQL语句

Posted longlyseul

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis--MyBatis动态SQL语句相关的知识,希望对你有一定的参考价值。

mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:
1. if 语句 (简单的条件判断)
2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
5. set (主要用于更新时)
6. foreach (用于mybatis语句查询)

 
1.if语句(简单的条件判断)
(1)数据库my的user表数据为:
 
技术图片
 
(2)首先创建一个mybatis项目,mybatis_008
技术图片

 

(3)建立com.zk.dao包下的IUser接口:

IUser.java

package com.zk.dao;


import org.apache.ibatis.annotations.Param;

import com.zk.pojo.User;


public interface IUser 
		public User findUserWithNameLike(@Param("id")int id, @Param("name")String name);

(4)在com.zk.pojo包下建立User实现IUser接口中的方法,并配置UserMapper.xml

User.java

package com.zk.pojo;

public class User 
	private int id;
	private String name;
	private String level;
	private String phone;
	public int getId() 
		return id;
	
	public void setId(int id) 
		this.id = id;
	
	public String getName() 
		return name;
	
	public void setName(String name) 
		this.name = name;
	
	public String getLevel() 
		return level;
	
	public void setLevel(String level) 
		this.level = level;
	
	public String getPhone() 
		return phone;
	
	public void setPhone(String phone) 
		this.phone = phone;
	
	@Override
	public String toString() 
		return "User [id=" + id + ", name=" + name + ", level=" + level
				+ ", phone=" + phone + "]";
	
	

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.zk.dao.IUser">
<select id="findUserWithNameLike" resultType="User">
  SELECT * FROM user 
  WHERE id = #id <if test="name!= null">
    AND name like #name
  </if>
</select>
</mapper>

(5)配置config/configure.xml

configure.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.zk.pojo.User" />
</typeAliases>

<environments default="development">
	    <environment id="development">
		<transactionManager type="JDBC" />
		<dataSource type="POOLED">
		<property name="driver" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/my"/>
		<property name="username" value="root"/>
		<property name="password" value="123456"/>
		</dataSource>
		</environment>
	</environments>

	<mappers>
	     <!-- // power by http://www.yiibai.com -->
	     <mapper resource="com/zk/pojo/UserMapper.xml" />
	</mappers>
</configuration>

(6)实现main函数

MainApp.java

package Main;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.zk.dao.IUser;
import com.zk.pojo.User;

public class MainApp 
	private static SqlSessionFactory sqlsessionfactory;
	private static Reader reader;
	
	static
		try 
			reader=Resources.getResourceAsReader("config/configure.xml");
			sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader);
		 catch (IOException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		
	
	
	public SqlSessionFactory getSqlSessionFactory()
	
		return sqlsessionfactory;
	
	
	public static void main(String[]args)
	
		select(1,"c");
	
	
	public static void select(int id,String name)
		//获取session连接
		SqlSession session=sqlsessionfactory.openSession();
		//获取mapper
		IUser usermap=session.getMapper(IUser.class);
		//selectAllUser();
		User user = usermap.findUserWithNameLike(id, name);
		//执行查询
		System.out.println(user.toString());
		session.commit();
		//selectAllUser();
	

  Main执行结果如下:

技术图片

(未完待续)

 

以上是关于mybatis--MyBatis动态SQL语句的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis

什么是Mybatis?

MyBatis-- MyBatis介绍

MyBatis学习总结-MyBatis快速入门的系列教程

MyBatis7:MyBatis插件及示例----打印每条SQL语句及其执行时间

Mybatis的原理及在idea中创建mybatis项目流程