- /**
- * 根据用户名进行模糊查询
- * @param username
- * @return java.util.List
- */
- List<User> getUserByLike(@Param("username") String username);
- <!--List<User> getUserByLike(@Param("username") String username);-->
- <select id="getUserByLike" resultType="User">
- select * from t_user where username like '%${username}%'
- </select>
like '%${xxx}%' 本质上是字符串的拼接,在数据库中单引号表示字符串。此处不能使用#{xxx},#{xxx}的本质是占位符赋值,'%#{xxx}%'会被数据库解析为'%?%',即会去查找字段中是否包含?.
- /**
- * 根据用户名进行模糊查询
- * @param username
- * @return java.util.List
- */
- List<User> getUserByLike(@Param("username") String username);
- <!--List<User> getUserByLike(@Param("username") String username);-->
- <select id="getUserByLike" resultType="User">
- select * from t_user where username like concat('%',#{username},'%')
- </select>
#{}的本质就是占位符赋值,再使用concat()函数进行拼接。
此种写法Mysql中有效,Oracle中无效。
- /**
- * 根据用户名进行模糊查询
- * @param username
- * @return java.util.List
- */
- List<User> getUserByLike(@Param("username") String username);
- <!--List<User> getUserByLike(@Param("username") String username);-->
- <select id="getUserByLike" resultType="User">
- select * from t_user where username like "%"#{username}"%"
- </select>
在Mysql数据库中,可以使用双引号进行字符串的拼接,如上:"%"#{username}"%"
select * from t_user where username like "%"#{xxx}"%"是最常用的此种写法Oracle有效,Mysql中无效。
- <select id="selectByCondition" resultType="com.chinamobile.zj.modules.aqtz.entity.AqtzTzxxEntity">
- select ID,NAME,DEPT_ID,DEPT_NAME,CREAT_TIME,USER_ID,USER_NAME,FILE_ID,PARENT_ID,FILE_NAME,CONTENT from AQTZ_TZXX
- <where>
- <if test="id != null and id != ''"> and ID = #{id} </if>
- <if test="name != null and name != ''"> and NAME like '%'||#{name}||'%' </if>
- <if test="deptId != null and deptId != ''"> and DEPT_ID = #{deptId} </if>
- <if test="deptName != null and deptName != ''"> and DEPT_NAME like '%'||#{deptName}||'%' </if>
- <if test="creatTime != null and creatTime != ''"> and CREAT_TIME like '%'||#{creatTime}||'%' </if>
- <if test="userId != null and userId != ''"> and USER_ID = #{userId} </if>
- <if test="userName != null and userName != ''"> and USER_NAME like '%'||#{userName}||'%' </if>
- <if test="fileId != null and fileId != ''"> and FILE_ID = #{fileId} </if>
- <if test="parentId != null and parentId != ''"> and PARENT_ID = #{parentId} </if>
- <if test="fileName != null and fileName != ''"> and FILE_NAME like '%'||#{fileName}||'%' </if>
- <if test="content != null and content != ''"> and CONTENT like '%'||#{content}||'%' </if>
- </where>
- </select>
在oracle数据库中,||相当于字符串的拼接。
只能使用${},如果使用#{},则解析后的sql语句为delete from t_user where id in ('1,2,3'),这样是将1,2,3看做是一个整体,只有id为1,2,3的数据会被删除。正确的语句应该是delete from t_user where id in (1,2,3),或者delete from t_user where id in ('1','2','3')
其中#{xxx}会被解析成 'xxx'.
- /**
- * 根据id批量删除
- * @param ids
- * @return int
- */
- int deleteMore(@Param("ids") String ids);
- <delete id="deleteMore">
- delete from t_user where id in (${ids})
- </delete>
- //测试类
- @Test
- public void deleteMore() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- int result = mapper.deleteMore("1,2,3");
- System.out.println(result);
- }
其中#{xxx}会被解析成 'xxx'.
- /**
- * 查询指定表中的数据
- * @param tableName
- * @return java.util.List
- */
- List<User> getUserByTable(@Param("tableName") String tableName);
- <!--List<User> getUserByTable(@Param("tableName") String tableName);-->
- <select id="getUserByTable" resultType="User">
- select * from ${tableName}
- </select>
使用场景
t_clazz(clazz_id,clazz_name)
t_student(student_id,student_name,clazz_id)
1.添加班级信息
2.获取新添加的班级的id
3.为班级分配学生,即将某学的班级id修改为新添加的班级的id
在mapper.xml中设置两个属性
useGeneratedKeys:设置使用自增的主键
keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
- /**
- * 添加用户信息
- * @param user
- */
- void insertUser(User user);
- <!--void insertUser(User user);-->
- <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
- insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})
- </insert>
- //测试类
- @Test
- public void insertUser() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- User user = new User(null, "ton", "123", 23, "男", "123@321.com");
- mapper.insertUser(user);
- System.out.println(user);
- //输出:user{id=10, username='ton', password='123', age=23, sex='男', email='123@321.com'},自增主键存放到了user的id属性中
- }