动态SQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #{} 占位符:用于指定 SQL 参数的占位符,从而实现参数绑定和防止 SQL 注入攻击。#{}具有更高的安全性和更好的可读性,但是在某些情况下,${}可能更加方便。 ${}是文本替换,${}可以用于动态指定列名、表名、SQL函数等,但是也会存在SQL注入的风险。因此,${}应该谨慎使用,只用于不需要通过预编译来防止SQL注入攻击的情况。 <if > 标签:用于条件判断,当满足条件时,生成相应的SQL语句,通常与<where > 或<set > 标签配合使用。<where > 标签:用于生成SQL语句中的WHERE子句,可以动态地添加条件语句,并自动去除多余的AND和OR。<set > 标签:用于生成SQL语句中的SET子句,可以动态地更新多个列的值。<foreach > 标签:用于循环遍历集合或数组,并将集合中的元素作为参数传递到SQL语句中的IN子句中。<choose > 标签:相当于Java中的switch语句,根据条件生成不同的SQL语句。<when > 标签:配合<choose > 标签使用,表示不同的分支条件。<otherwise > 标签:配合<choose > 标签使用,表示没有任何条件匹配时的默认分支。
增删改 插入 1 2 3 int insertUser (User user) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <insert id ="insertUser" > INSERT INTO t_sys_user <trim prefix ="(" suffix =")" suffixOverrides ="," > <if test ="account != null and account != ''" > account,</if > <if test ="realName != null and realName != ''" > realName,</if > <if test ="sex != null and sex != ''" > sex,</if > <if test ="birthday != null and birthday != ''" > birthday,</if > <if test ="phone != null and phone != ''" > phone,</if > <if test ="address != null and address != ''" > address,</if > </trim > <trim prefix ="VALUES (" suffix =")" suffixOverrides ="," > <if test ="account != null and account != ''" > #{account},</if > <if test ="realName != null and realName != ''" > #{realName},</if > <if test ="sex != null and sex != ''" > #{sex},</if > <if test ="birthday != null and birthday != ''" > #{birthday},</if > <if test ="phone != null and phone != ''" > #{phone},</if > <if test ="address != null and address != ''" > #{address},</if > </trim > </insert >
在测试类
测试是否插入成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void testInsert () throws Exception { User user = new User (); user.setRealName("test" ); user.setSex(1 ); user.setAccount("test" ); UserMapper userMapper = session.getMapper(UserMapper.class); int rows = userMapper.insertUser(user); System.out.println("插入了" + rows + "行" ); }
修改 1 2 3 int updateUser (User user) ;
1 2 3 4 5 6 7 8 9 10 11 12 <update id ="updateUser" > UPDATE t_sys_user <set > <if test ="realName != null" > realName = #{realName},</if > <if test ="account != null" > account = #{account},</if > <if test ="sex != null" > sex = #{sex},</if > <if test ="birthday != null" > birthday = #{birthday},</if > <if test ="phone != null" > phone = #{phone},</if > <if test ="address != null" > address = #{address},</if > </set > WHERE id = #{id} </update >
在测试类
测试是否修改成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Test public void testUpdate () throws Exception { User user = new User (); user.setRealName("阿达是几号来的kj" ); user.setSex(1 ); user.setAccount("奥术大师大所大所" ); user.setId(10 ); UserMapper userMapper = session.getMapper(UserMapper.class); int rows = userMapper.updateUser(user); System.out.println("修改了" + rows + "行" ); }
删除 1 2 int DeleteUserByIds (List<Integer> ids) ;
1 2 3 4 5 6 <delete id ="DeleteUserByIds" > DELETE FROM t_sys_user WHERE id IN <foreach collection ="list" item ="id" open ="(" close =")" separator ="," > #{id} </foreach > </delete >
在测试类
测试是否删除成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Test public void testDeleteUserByIds () { List<Integer> ids = new ArrayList <Integer>(); ids.add(11 ); ids.add(12 ); ids.add(13 ); UserMapper userMapper = session.getMapper(UserMapper.class); int rows = userMapper.DeleteUserByIds(ids); System.out.println("删除了" + rows + "行" ); }
查询 1 2 List<User> findUserByWhere (@Param("name") String name,@Param("id") Integer id,@Param("sex") Integer sex,@Param("address") String address) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <select id ="findUserByWhere" resultType ="com.Miku_39.pojo.User" > SELECT * FROM t_sys_user <where > <if test ="name != null and name != ''" > AND realName LIKE "%"#{name}"%" </if > <if test ="sex != null" > AND sex = #{sex} </if > <if test ="id != null" > AND id=#{id} </if > <if test ="address != null" > AND address LIKE "%"#{address}"%" </if > </where > </select >
在测试类
测试
1 2 3 4 5 6 7 8 @Test public void testFindUserByCondition () throws Exception { System.out.println( session.getMapper(UserMapper.class).findUserByWhere("号" ,null ,null ,null ) ); }
分页查询 在maven
导入分页插件jar包 1 2 3 4 5 6 <dependency > <groupId > com.github.pagehelper</groupId > <artifactId > pagehelper</artifactId > <version > 5.2.1</version > </dependency >
在mybatis-config.xml
下导入设置分页插件
1 2 3 4 <plugins > <plugin interceptor ="com.github.pagehelper.PageInterceptor" > </plugin > </plugins >
在UserMapper.java
接口查询所有
在UserMapper.xml
中写Sql语句
1 2 3 4 <select id ="findAll" resultType ="com.Miku_39.pojo.User" > select * from t_sys_user </select >
在测试类中试验是否成功
1 2 3 4 5 6 @Test public void testfindAll () throws Exception { UserMapper mapper = session.getMapper(UserMapper.class); List<User> users = mapper.findAll(); System.out.println(users); }
然后在刚刚测试类方法中添加分页对象并且传入 pageNo 和 pageSize 的值
1 PageHelper.startPage(1 ,1 );
然后创建新的分页对象 1 PageInfo<User> pageInfo = new PageInfo <>(users,2 );
小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 UserMapper mapper = session.getMapper(UserMapper.class);PageHelper.startPage(1 , 1 ); List<User> users = mapper.findAll(); PageInfo<User> pageInfo = new PageInfo <>(users, 2 ); System.out.println(pageInfo);