JavaWeb_(Mybatis框架)动态sql_七
系列博文:
JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二 传送门
JavaWeb_(Mybatis框架)Mapper动态代理开发_三 传送门
JavaWeb_(Mybatis框架)主配置文件介绍_四 传送门
JavaWeb_(Mybatis框架)输入和输出参数_五 传送门
JavaWeb_(Mybatis框架)关联查询_六传送门 传送门
JavaWeb_(Mybatis框架)动态sql_七传送门 传送门
1、if标签 - 多条件查询,获取用户列表;
2、where标签 - 解决if标签拼接字符串AND符号问题;
3、trim标签 - 定制where标签的规则
4、set标签 - 解决更新数据表时字符串拼接逗号”,”问题;
5、foreach标签 – 如果需要使用IN查询多条相同数据,可以使用foreach遍历;
6、sql标签 – 可以提取重复sql语句片段;
1、if标签 - 多条件查询,获取用户列表
模糊查询用户名中带有"王"字的用户
UserMapper.java中编写通过用户模糊查询用户列表接口selectUserListByUser()
package com.Gary.mapper; import java.util.List; import com.Gary.bean.User;
import com.Gary.bean.UserVo; public interface UserMapper { //通过id查询一个用户
public User selectUserById(Integer id); //通过用户名模糊查询 获取用户列表
public List<User> selectUserByName(String name); //通过UserVo id 查询一个用户
public User selectUserByVoId(UserVo id); //查询用户总条数
public Integer selectUserCount(); //查询所有用户的包装类
public List<UserVo> selectAllUserVo(); //多条件查询 通过用户对象中的条件查询用户列表
public List<User> selectUserListByUser(User u); }
UserMapper.java
UserMapper.xml中实现这个模糊查询的方法
<select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user WHERE u_sex = #{u_sex}
AND u_username like "%"#{u_username}"%"
AND u_cid = #{u_cid}
</select>
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user WHERE u_sex = #{u_sex}
AND u_username like "%"#{u_username}"%"
AND u_cid = #{u_cid}
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest8 { @Test
public void Test8() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
u.setU_sex("1");
u.setU_username("王");
u.setU_cid(1); List<User> list = mapper.selectUserListByUser(u); for(User user : list){
System.out.println(user);
} } }
MapperTest8.java
但此时,如果用户未输入三个参数,只输入了sex、username两个参数
UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
u.setU_sex("1");
u.setU_username("王");
//u.setU_cid(1); List<User> list = mapper.selectUserListByUser(u); for(User user : list){
System.out.println(user);
}
此时会无法准确模糊查询到数据库中的值
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest8 { @Test
public void Test8() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
u.setU_sex("1");
u.setU_username("王");
//u.setU_cid(1); List<User> list = mapper.selectUserListByUser(u); for(User user : list){
System.out.println(user);
} } }
MapperTest8.java
为防止照成这种情况,可以使用在查询时使用<if>标签【也可以防止用户添加空串】
<select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user WHERE <if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</select>
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user WHERE <if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest8 { @Test
public void Test8() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
u.setU_sex("1");
u.setU_username("王");
//u.setU_cid(1); List<User> list = mapper.selectUserListByUser(u); for(User user : list){
System.out.println(user);
} } }
MapperTest8.java
此时如果是未输入第一个u_sex值,会产生bug,数据库执行语句就会变成 SELECT * FROM user WHERE AND u_username like "%"#{u_username}"%"
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user WHERE <if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</select> </mapper>
UserMapper.xml
所以,为了解决上面这种情况,我们可以where标签
2、where标签 - 解决if标签拼接字符串AND符号问题;
在原有的selectUserListByUser查询用户性别 模糊用户名 查询用户cid 国际ID上添加<where>标签 <where>标签可以去掉前边的AND符号
<select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<where>
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</where>
</select>
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<where>
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</where>
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest8 { @Test
public void Test8() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
//u.setU_sex("1");
u.setU_username("王");
//u.setU_cid(1); List<User> list = mapper.selectUserListByUser(u); for(User user : list){
System.out.println(user);
} } }
MapperTest8.java
3、trim标签 - 定制where标签的规则
在<trim>标签中默认使用prefixOverrides去掉前AND,也可以使用suffixOverrides去掉后AND
<select id="selectUserListByUserTrim" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<!-- 默认使用prefixOverrides去掉前AND 也可以使用suffixOverrides去掉后AND -->
<trim prefix="where" prefixOverrides="">
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</trim>
</select>
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<where>
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</where>
</select> <select id="selectUserListByUserTrim" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<!-- 默认使用prefixOverrides去掉前AND 也可以使用suffixOverrides去掉后AND -->
<trim prefix="where" suffixOverrides="AND|OR">
<if test="u_sex != null">
u_sex = #{u_sex} AND
</if> <if test="u_username != null">
u_username like "%"#{u_username}"%" AND
</if> <if test="u_cid != null">
u_cid = #{u_cid} AND
</if>
</trim>
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest9 { @Test
public void Test8() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User();
//u.setU_sex("1");
u.setU_username("王");
//u.setU_cid(1); List<User> list = mapper.selectUserListByUserTrim(u); for(User user : list){
System.out.println(user);
} } }
MapperTest9.java
4、set标签 - 解决更新数据表时字符串拼接逗号”,”问题;
编写一个更新用户updateSetUser的SQL语句
<update id="updateSetUser" parameterType="User">
<!-- 用户名 用户密码 性别 用id来限制 -->
update user
Set
<if test="u_username != null and u_username != '' ">
u_username = #{u_username},
</if>
<if test="u_password != null and u_password != '' ">
u_password = #{u_password},
</if>
<if test="u_sex != null and u_sex != '' ">
u_sex = #{u_sex},
</if> Where u_id= #{u_id} </update>
发现如果最后一个u_sex条件不成立的时候,会把#{u_password}后边的逗号,遗留下
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest10 { @Test
public void Test10() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User(); u.setU_id(1);
u.setU_username("Gary王");
u.setU_password("aaa");
//u.setU_sex("1"); 主拼其那两个,会录下一个逗号
//未提交事务 不会改变数据库
mapper.updateSetUser(u); } }
MapperTest10.java
5、foreach标签 – 如果需要使用IN查询多条相同数据,可以使用foreach遍历;
数据库中使用in标签查询u_id为1,3,5的数据
select * from user where u_id in (1,3,5)
Gary.sql
编写一个使用id更新用户列表selectUserListByIds的SQL语句
<!-- 使用多个id获得用户列表 (1,3,5) -->
<select id="selectUserListByIds" resultType="User">
select *
from user
where u_id
in
<!-- (1,3,5) separator可以组拼逗号 -->
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<where>
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</where>
</select> <select id="selectUserListByUserTrim" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<!-- 默认使用prefixOverrides去掉前AND 也可以使用suffixOverrides去掉后AND -->
<trim prefix="where" suffixOverrides="AND|OR">
<if test="u_sex != null">
u_sex = #{u_sex} AND
</if> <if test="u_username != null">
u_username like "%"#{u_username}"%" AND
</if> <if test="u_cid != null">
u_cid = #{u_cid} AND
</if>
</trim>
</select> <update id="updateSetUser" parameterType="User">
<!-- 用户名 用户密码 性别 用id来限制 -->
update user
<set>
<if test="u_username != null and u_username != '' ">
u_username = #{u_username},
</if>
<if test="u_password != null and u_password != '' ">
u_password = #{u_password},
</if>
<if test="u_sex != null and u_sex != '' ">
u_sex = #{u_sex},
</if>
</set>
Where u_id= #{u_id} </update> <!-- 使用多个id获得用户列表 (1,3,5) -->
<select id="selectUserListByIds" resultType="User">
select *
from user
where u_id
in
<!-- (1,3,5) separator可以组拼逗号 -->
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest11 { @Test
public void Test11() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); Integer[] ids = {1,3,5,7};
List<User> list = mapper.selectUserListByIds(ids); for(User user:list) {
System.out.println(user);
} } }
MapperTest11.java
6、sql标签 – 可以提取重复sql语句片段;
在Userapper.xml中实现<sql>标签
<sql id="myselect">
select * from user
</sql>
之后在UserMapper.xml中可以用<include>把重复引用的sql语句片段加入进去
<!-- 使用多个id获取用户列表 by userVO -->
<select id="selectUserListByUserVo" parameterType="UserVo" resultType="User">
<include refid="myselect"/>
where u_id
in
<!-- (1,3,5) separator可以组拼逗号 -->
<foreach collection="idList" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<?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.Gary.mapper.UserMapper"> <sql id="myselect">
select * from user
</sql> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> <select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<where>
<if test="u_sex != null">
u_sex = #{u_sex}
</if> <if test="u_username != null">
AND u_username like "%"#{u_username}"%"
</if> <if test="u_cid != null">
AND u_cid = #{u_cid}
</if>
</where>
</select> <select id="selectUserListByUserTrim" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊用户名 查询用户cid 国际ID -->
SELECT * FROM user
<!-- 默认使用prefixOverrides去掉前AND 也可以使用suffixOverrides去掉后AND -->
<trim prefix="where" suffixOverrides="AND|OR">
<if test="u_sex != null">
u_sex = #{u_sex} AND
</if> <if test="u_username != null">
u_username like "%"#{u_username}"%" AND
</if> <if test="u_cid != null">
u_cid = #{u_cid} AND
</if>
</trim>
</select> <update id="updateSetUser" parameterType="User">
<!-- 用户名 用户密码 性别 用id来限制 -->
update user
<set>
<if test="u_username != null and u_username != '' ">
u_username = #{u_username},
</if>
<if test="u_password != null and u_password != '' ">
u_password = #{u_password},
</if>
<if test="u_sex != null and u_sex != '' ">
u_sex = #{u_sex},
</if>
</set>
Where u_id= #{u_id} </update> <!-- 使用多个id获得用户列表 (1,3,5) -->
<select id="selectUserListByIds" resultType="User">
select *
from user
where u_id
in
<!-- (1,3,5) separator可以组拼逗号 -->
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select> <!-- 使用多个id获取用户列表 by userVO -->
<select id="selectUserListByUserVo" parameterType="UserVo" resultType="User">
<include refid="myselect"/>
where u_id
in
<!-- (1,3,5) separator可以组拼逗号 -->
<foreach collection="idList" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; 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.Test; import com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest12 { @Test
public void Test12() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession();
UserMapper mapper = session.getMapper(UserMapper.class); List<Integer> idList = new ArrayList<Integer>();
idList.add(1);
idList.add(4);
idList.add(8); UserVo vo = new UserVo();
vo.setIdList(idList); List<User> list =mapper.selectUserListByUserVo(vo); for(User user:list) {
System.out.println(user);
} } }
MapperTest12.java
JavaWeb_(Mybatis框架)动态sql_七的更多相关文章
- JavaWeb_(Mybatis框架)Mapper动态代理开发_三
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)关联查询_六
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)输入和输出参数_五
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)主配置文件介绍_四
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
- MyBatis框架——动态SQL
MyBatis 作为⼀个“半⾃动化”的 ORM 框架,需要开发者⼿动定义 SQL 语句. 在业务需求⽐较复杂的情 况下,⼿动拼接 SQL 语句的⼯作量会⾮常⼤,为了适⽤于不同的业务需求,往往需要做很多 ...
随机推荐
- Html5+Mui前端框架,开发记录(四):下拉菜单绑定数据、搜索、时间控件
1.下拉菜单绑定数据,选择后回传值 1)html: <div class="mui-input-row"> <label>xxx:</label> ...
- .NET Core 发布部署问题
运行环境 操作系统 开发工具 frameworks .Net Core SDK 版本 托管运行 本地 ...
- Vue命名路由
Vue命名路由 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- MYSQL日期相关操作
*******MYSQL中取当前周/月/季/年的第一天与最后一天******* 当年第一天: SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 ...
- centos7安装google浏览器
1. 配置yum源 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo cd /ect/yum.repos.d/ vim google-chrome.repo ...
- laravel中引入composer安装在vendor中的第三方组件
一.安装第三方组件 方法一:使用命令行安装第三方(已phpword为例): composer require phpoffce/phpword ^v0..* 方法二: 修改主项目composer.js ...
- goroutine的设计与实现
goroutine背后的系统知识 http://www.sizeofvoid.net/goroutine-under-the-hood/ 下周写完
- FPGA学习笔记——点亮LED
软件平台:win7(64bit) + Quartus II 9.1 (64-Bit) 硬件平台:东理电子Easy-FPGA Cyclone II EP2C5T114C8N 这个开发板买了很长时间了,买 ...
- 【问题】redhat安装dig
转自:https://blog.csdn.net/ricky_hust/article/details/8868131 最终可行的方法 在linux下没有单独的dig和nslookup的包,而是以bi ...
- python-----将图片与标注的xml坐标水平翻转
我们做机器学习的时候,总会用到很多训练集,然后我们的数据比较少的时候,就可以将图片翻转标注.代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # ...