1、模糊查询
/** * 测试模糊查询 * @param mohu * @return */ List<User> testMohu(@Param("mohu") String mohu);
<!--List<User> testMohu(@Param("mohu") String mohu);--> <select id="testMohu" resultType="User"> <!--select * from t_user where username like '%${mohu}%'--> <!--select * from t_user where username like concat('%',#{mohu},'%')--> select * from t_user where username like "%"#{mohu}"%" </select>
2、批量删除
/** * 批量删除 * @param ids * @return */ int deleteMore(@Param("ids") String ids);
<!--int deleteMore(@Param("ids") String ids);--> <delete id="deleteMore"> delete from t_user where id in (${ids}) </delete>
3、动态设置表名
/** * 动态设置表名,查询所有的用户信息 * @param tableName * @return */ List<User> getAllUser(@Param("tableName") String tableName);
<!--List<User> getAllUser(@Param("tableName") String tableName);--> <select id="getAllUser" resultType="User"> select * from ${tableName} </select>
4、添加功能获取自增的主键
t_clazz(clazz_id,clazz_name) t_student(student_id,student_name,clazz_id)
1、添加班级信息
2、获取新添加的班级的id
3、为班级分配学生,即将某学的班级id修改为新添加的班级的id
/** * 添加用户信息 * @param user * @return * useGeneratedKeys:设置使用自增的主键 * keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参 数user对象的某个属性中 */ int insertUser(User user);
<!--int insertUser(User user);--> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values(null,#{username},#{password},#{age},#{sex}) </insert>
这里的意思是将新增的数据的ID放入实体类,
因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
在这里就遇到了不能加引号的情况,所以只能使用${}
实例
public interface SQLMapper { /** * 根据用户名模糊查询用户信息 */ List<User> getUserByLike(@Param("username") String username); /** * 批量删除 */ int deleteMore(@Param("ids") String ids); /** * 查询指定表中的数据 */ List<User> getUserByTableName(@Param("tableName") String tableName); /** * 添加用户信息 */ void insertUser(User user); }
<?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.atguigu.mybatis.mapper.SQLMapper"> <!--List<User> getUserByLike(@Param("username") String username);--> <select id="getUserByLike" resultType="User"> <!--select * from t_user where username like '%${username}%'--> <!--select * from t_user where username like concat('%',#{username},'%')--> select * from t_user where username like "%"#{username}"%" </select> <!--int deleteMore(@Param("ids") String ids);--> <delete id="deleteMore"> delete from t_user where id in (${ids}) </delete> <!--List<User> getUserByTableName(@Param("tableName") String tableName);--> <select id="getUserByTableName" resultType="User"> select * from ${tableName} </select> <!-- void insertUser(User user); useGeneratedKeys:设置当前标签中的sql使用了自增的主键 keyProperty:将自增的主键的值赋值给传输到映射文件中参数的某个属性 --> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email}) </insert> </mapper>
public class SQLMapperTest { @Test public void testGetUserByLike(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SQLMapper mapper = sqlSession.getMapper(SQLMapper.class); List<User> list = mapper.getUserByLike("a"); System.out.println(list); } @Test public void testDeleteMore(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SQLMapper mapper = sqlSession.getMapper(SQLMapper.class); int result = mapper.deleteMore("1,2,3"); System.out.println(result); } @Test public void testGetUserByTableName(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SQLMapper mapper = sqlSession.getMapper(SQLMapper.class); List<User> list = mapper.getUserByTableName("t_user"); System.out.println(list); } @Test public void testInsertUser(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SQLMapper mapper = sqlSession.getMapper(SQLMapper.class); User user = new User(null, "王五", "123", 23, "男", "[email protected]"); mapper.insertUser(user); System.out.println(user); } @Test public void testJDBC() throws Exception { Class.forName(""); Connection connection = DriverManager.getConnection("", "", ""); PreparedStatement ps = connection.prepareStatement("insert", Statement.RETURN_GENERATED_KEYS); ps.executeUpdate(); ResultSet resultSet = ps.getGeneratedKeys(); } }
标签:username,特殊,String,tableName,Param,SQLMapper,user,SQL,执行 From: https://www.cnblogs.com/dzs894330350/p/16727127.html