动态Sql语句中标签的使用
(一)常用标签
1.
if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段,还可以在INSERT语句中用来判断是否插入某个字段的值
2.
where元素的作用是给SQL语句添加一个条件判断. 如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上
3.
主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有item,index,collection,open,separator,close
collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合
item : 表示在迭代过程中每一个元素的别名
index :表示在迭代过程中每次迭代到的位置(下标)
open :前缀
close :后缀
separator :分隔符,表示迭代时每个元素之间以什么分隔
(二)应用
1.pom.xml
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.41version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.6version> dependency> dependencies>
2.SqlMapperConfig.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC">transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver">property> <property name="url" value="jdbc:mysql://localhost:3306/zml?useUnicode=true&characterEncoding=utf-8">property> <property name="username" value="root">property> <property name="password" value="root">property> dataSource> environment> environments> <mappers> <mapper resource="dao/UserDao.xml">mapper> mappers> configuration>
3.Users.java
package entity; public class Users { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Users{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
4.UserVo.java
package entity; import java.util.List; public class UserVo { private Users users; private Listids; public Users getUsers() { return users; } public void setUsers(Users users) { this.users = users; } public List getIds() { return ids; } public void setIds(List ids) { this.ids = ids; } }
5.UserDao.java
package dao; import entity.UserVo; import entity.Users; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserDao { //查询所有用户信息 ListfindAll(); //根据ID进行查询 Users find(int id); //模糊查询 List findVague(String name); //查询用户总数 int count(); //将实体类封装到另一个实体类中的模糊查询 List findVo(UserVo vo); //根据传入参数进行查询 Users findby(Users users); //根据UserVo中的ids集合查询用户信息 List findids(UserVo vo); }
6.UserDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.UserDao"> <select id="findAll" resultType="entity.Users"> select * from users select> <select id="find" resultType="entity.Users"> select * from users where id=#{id} select> <select id="findVague" resultType="entity.Users"> -- select * from users where username like '%${value}%' select * from users where username like #{name} select> <select id="count" resultType="int"> select count(id) from users select> <select id="findVo" resultType="entity.Users"> select * from users where username like #{users.username} select> <select id="findby" resultType="entity.Users" > select * from users <where> <if test="id != null"> and id=#{id} if> <if test="username != null"> and username=#{username} if> where> select> <select id="findids" resultType="entity.Users" parameterType="entity.UserVo"> select * from users <where> <if test="ids != null and ids.size()>0"> <foreach collection="ids" open="and id in (" close=")" item="id" separator=","> #{id} foreach> if> where> select> mapper>
7.UserTest.java
package test; import dao.UserDao; import entity.UserVo; import entity.Users; 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 org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class UserTest { private InputStream in; private SqlSession sqlSession; private UserDao UserDaoImpl; @Before public void init() throws IOException { //1.读取配置文件 in= Resources.getResourceAsStream("SqlMapperConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂生产SqlSession对象 sqlSession=factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 UserDaoImpl=sqlSession.getMapper(UserDao.class); } @After public void destroy() throws IOException { //提交事务 sqlSession.commit(); //6.释放资源 sqlSession.close(); in.close(); } /** * 查询所有 * @throws IOException */ @Test public void findall() throws IOException { //5.使用代理对象执行方法 Listusers=UserDaoImpl.findAll(); for(Users u:users){ System.out.println(u); } } /** * 根据ID进行查询 */ @Test public void find(){ Users u=UserDaoImpl.find(15); System.out.println(u); } /** * 模糊查询,查询所有名字中有小的用户 * @throws IOException */ @Test public void findVague() throws IOException { //5.使用代理对象执行方法 List users=UserDaoImpl.findVague("%小%"); for(Users u:users){ System.out.println(u); } } /** * 查询用户总数 */ @Test public void count(){ int c=UserDaoImpl.count(); System.out.println(c); } /** * 将实体类封装到另一个实体类中的模糊查询 */ @Test public void findVo(){ UserVo vo=new UserVo(); Users users=new Users(); users.setUsername("%小%"); vo.setUsers(users); List list=UserDaoImpl.findVo(vo); for(Users u:list){ System.out.println(u); } } /** * if标签查询,查询用户名为小花,id为25的用户信息 */ @Test public void findby(){ Users users=new Users(); users.setUsername("小花"); users.setId(25); Users u=UserDaoImpl.findby(users); System.out.println(u); } /** * 根据UserVo中的ids集合查询用户信息 */ @Test public void findids(){ UserVo vo=new UserVo(); List list=new ArrayList (); list.add(13); list.add(14); list.add(15); vo.setIds(list); List li=UserDaoImpl.findids(vo); for(Users u:li){ System.out.println(u); } } }