首頁 > 資料庫 > mysql教程 > mybatis-的使用(使用配置文件)

mybatis-的使用(使用配置文件)

WBOY
發布: 2016-06-07 16:03:58
原創
1226 人瀏覽過

简介: MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plan Old Java Objects,普

简介: MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plan Old Java Objects,普通的 Java对象)映射成数据库中的记录。
使用步骤:
1、首先,导入包: 导入mybatis和其三方的jar包,还有 数据库的 驱动包;
2、然后,写mybatis。xml配置文件: @、在类根目录创建mybatis.xml文件,里面的内容是这样子滴:
(里面需要更改的就是,连接的数据库要素,还有最后的sql映射的配置文件,下一点就说明)
<code class="language-xml"><?xml version="1.0" encoding="UTF-8" ?><code class="language-xml"><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"<code class="language-xml">"http://mybatis.org/dtd/mybatis-3-config.dtd"><code class="language-xml"><configuration><code class="language-xml">
<code class="language-xml"><!-- 这个是po类的设置 这样不必在sql中写po类的全限定名 -->
<code class="language-xml">    <typeAliases>
		<typeAlias alias="Photo" type="com.bjsxt.po.Photo"/>
	</typeAliases>
<code class="language-xml">
<code class="language-xml">	<environments default="development"><code class="language-xml">		<environment id="development"><code class="language-xml">			<transactionManager type="JDBC" /><code class="language-xml">			<dataSource type="POOLED"><code class="language-xml">				<property name="driver" value="com.mysql.jdbc.Driver" /><code class="language-xml">				<property name="url" value="jdbc:mysql://localhost/photo" /><code class="language-xml">				<property name="username" value="root" /><code class="language-xml">				<property name="password" value="xiaohang" /><code class="language-xml">			</dataSource><code class="language-xml">		</environment><code class="language-xml">	</environments><code class="language-xml">	<mappers><code class="language-xml">		<!-- sql 映射文件 这个是对应的配置文件  这个配置文件和dao里面的接口类联系 --><code class="language-xml">		<mapper resource="com/smallang/dao/PhotoDao.xml" /><code class="language-xml">	</mappers><code class="language-xml"></configuration></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">3、接着,创建dao和对应xml sql映射文件:

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><strong>@、</strong> 创建一个接口,里面就是一个或多个方法:比如像这样:

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">(这个里面就是各种方法,sql语句是放在映射的xml文件里面的,当然这个参数和返回类型是会面)
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">public interface PhotoDao {<code class="language-xml">	public void savePhoto(Photo photo);<code class="language-xml">	public Photo getPhotoById(int id);	<code class="language-xml">	public Photo findPhotoByName(String name,int uid);	<code class="language-xml">//	public Photo findPhotoByName2(Map<String, Object> v);	<code class="language-xml">	public Photo findPhotoByName2(Photo photo);	<code class="language-xml">	public List<Photo> findPhotos();<code class="language-xml">}	</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><strong>@、</strong> 创建于类名相同的xml,像这样PhotoDao.xml 里面的配置是这样的:
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><?xml version="1.0" encoding="UTF-8" ?><code class="language-xml"><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"<code class="language-xml">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><code class="language-xml"><mapper namespace="com.bjsxt.dao.PhotoDao"><code class="language-xml">	<!-- Id :dao中的方法名字 parameterType:方法中传参的类型 --><code class="language-xml">	<insert id="savePhoto" parameterType="Photo"> <!--  name 代表方法中第一个参数的name属性值 --><code class="language-xml">		insert into t_photo (name,uploadDate) values (#{name},#{uploadDate})<code class="language-xml">	</insert><code class="language-xml">	<!--  java中基本数据类型和string 都不需指定类型 parameterType="int" 不要--><code class="language-xml">	<select id="getPhotoById" resultType="Photo"> <!-- dsfsfs 代表方法中第一个参数的值 --><code class="language-xml">		select * from t_photo where id=#{dsfsfs}<code class="language-xml">	</select><code class="language-xml">	<code class="language-xml">	<!-- #{和方法中参数的名字无关}  {表示第n个参数的值或者其属性的值,或者其key的值}--><code class="language-xml">	<select id="findPhotoByName" resultType="Photo"> <!--  {}可以使用下标表示n --><code class="language-xml">		select * from t_photo where name=#{0}  and uid=#{1}<code class="language-xml">	</select>	<code class="language-xml">	<code class="language-xml">	<select id="findPhotoByName2" resultType="Photo"> <!--  {}: map中的key --><code class="language-xml">		select * from t_photo where name=#{name}  and uid=#{uid}<code class="language-xml">	</select><code class="language-xml">	<code class="language-xml">	<!--  resultType表示sql语句执行之后返回的数据类型 --><code class="language-xml">	<select id="findPhotos" resultType="Photo"><code class="language-xml">		select * from t_photo<code class="language-xml">	</select><code class="language-xml"></mapper></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">(解释上面的东东: 绿色:id= 对应的方法名;

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">蓝色:parameterType 参数的类型 特殊类必学写

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">黄色:sql 的 语句 (里面的#{参数类属性名} 或 #{0,1.。。}下标 来使用参数的数据)

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">粉色:resultType = 返回类型 )

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">然后这里有一个多表查询,返回list<map>d 数据;像这样
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><select id="findPhotoName" resultType="map"><code class="language-xml">		select p.name as pn,a.name as an from t_photo as p join t_album a on p.aid=a.id<code class="language-xml">	</select><code class="language-xml">public List<Photo> findPhotos();</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">还有<strong>高级的动态SQL(DynamicSQL)的使用</strong>,也是在这个,xml里面: 这些的东东; 例子: <u><em>Foreach的:</em></u> foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">public List<Album> findAlbum(List<Integer> ids); <code class="language-xml">//list<?> 接口里面的 因为查找包含条件不同,所以需要动态	<code class="language-xml">     <select id="findAlbum" resultType="Album"><code class="language-xml">		select * from t_album where id in <code class="language-xml">		<foreach collection="list" item="id" open="(" close=")" separator=","><code class="language-xml">			#{id}<code class="language-xml">		</foreach>		<code class="language-xml">	</select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">If 的例子就不用了; where的例子有一个:
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">	<select id="findAlbum2" resultType="Album"><!-- 整数不能当成int,而是Integer --><code class="language-xml">		select * from t_album <code class="language-xml">			<where><code class="language-xml">				<if test="name!=null"><code class="language-xml">					name=#{name} <code class="language-xml">				</if><code class="language-xml">				<if test="name !=null and uid!=null"><code class="language-xml">					and <code class="language-xml">				</if><code class="language-xml">				<if test="uid!=null"><code class="language-xml">					uid=#{uid}<code class="language-xml">				</if><code class="language-xml">			</where><code class="language-xml">	</select><code class="language-xml">就是如果where 后面有东东的话 就会输出where + 后面的东东  否则 就不输出</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">choose choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的,通常都是与when和otherwise搭配的。看如下一个例子:
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">	<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"><code class="language-xml">		select * from t_blog where 1 = 1 <code class="language-xml">		<choose><code class="language-xml">			<when test="title != null"><code class="language-xml">				and title = #{title}<code class="language-xml">			</when><code class="language-xml">			<when test="content != null"><code class="language-xml">				and content = #{content}<code class="language-xml">			</when><code class="language-xml">			<otherwise><code class="language-xml">				and owner = "owner1"<code class="language-xml">			</otherwise><code class="language-xml">		</choose><code class="language-xml">	</select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">trim: trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,示例代码如下:
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">	<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"><code class="language-xml">		select * from t_blog <code class="language-xml">		<trim prefix="where" prefixOverrides="and |or"><code class="language-xml">			<if test="title != null"><code class="language-xml">				title = #{title}<code class="language-xml">			</if><code class="language-xml">			<if test="content != null"><code class="language-xml">				and content = #{content}<code class="language-xml">			</if><code class="language-xml">			<if test="owner != null"><code class="language-xml">				or owner = #{owner}<code class="language-xml">			</if><code class="language-xml">		</trim><code class="language-xml">	</select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
登入後複製
<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml">set: set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。(如果set中一个条件都不满足,即set中包含的内容为空的时候就会报错。)<br> 这个的例子不copy了,感觉没什么用的;

<code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code class="language-xml"><code cl>
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板