当前位置:网站首页>7. Execution of special SQL
7. Execution of special SQL
2022-08-04 05:30:00 【ape white】
7、特殊SQL的执行
7.1、模糊查询
//模糊查询
List<User> getUserByLike(@Param("mohu") String mohu);
<!--List<User> getUserByLike(@Param("username") String mohu);-->
<select id="getUserByLike" resultType="user">
<!--select * from t_user where username like '%#{
mohu}%' String concatenation error-->
<!--正确方式如下:-->
<!--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>
测试:
@Test
public void GetUserByLike(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
SpecialSQLMapper mapper = sqlSession.getMapper(SpecialSQLMapper.class);
// for (User user : mapper.getUserByLike("a")) {
// System.out.println(user);
// }
List<User> a = mapper.getUserByLike("a");
a.forEach(System.out::println);
}
7.2、批量删除
//批量删除
void deleteMoreUser(@Param("ids") String ids);
<!--void deleteMoreUser(@Param("ids") String ids);-->
<delete id="deleteMoreUser">
delete from t_user where id in (${
ids})
</delete>
@Test
public void DeleteMoreUser(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
SpecialSQLMapper mapper = sqlSession.getMapper(SpecialSQLMapper.class);
mapper.deleteMoreUser("5,6");
}
7.3、动态设置表名
//动态设置表名
List<User> getUserList(@Param("tableName") String tableName);
<!--List<User> getUserList(@Param("tableName") String tableName);-->
<select id="getUserList" resultType="user">
select * from ${
tableName}
</select>
@Test
public void testGetUserList(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
SpecialSQLMapper mapper = sqlSession.getMapper(SpecialSQLMapper.class);
// for (User t_user : mapper.getUserList("t_user")) {
// System.out.println(t_user);
// }
List<User> t = mapper.getUserList("t_user");
t.forEach(System.out::println);
}
7.4、添加功能获取自增的主键
JDBC的功能:
@Test
public void testJDBC(){
try {
Class.forName("");
Connection connection = DriverManager.getConnection("", "", "");
// String sql = "";
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setString(1,"a");
String sql = "insert into t_user values()";
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ResultSet resultSet = ps.getGeneratedKeys();
resultSet.next();
int id = resultSet.getInt(1);
} catch (Exception e) {
e.printStackTrace();
}
}
//添加用户信息
void InsertUser(User user);
<!--void InsertUser(User user);-->
<!--
useGeneratedKeys:Indicates that the currently added function uses an auto-incrementing primary key
keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参
数user对象的某个属性中
-->
<insert id="InsertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{
username},#{
password},#{
age},#{
gender},#{
email})
</insert>
@Test
public void testInsertUser(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
SpecialSQLMapper mapper = sqlSession.getMapper(SpecialSQLMapper.class);
User user = new User(null, "张三", "2222", 18, "男", "[email protected]");
mapper.InsertUser(user);
System.out.println(user);
}
边栏推荐
- 入坑软件测试的经验与建议
- npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
- Resolved error: npm WARN config global `--global`, `--local` are deprecated
- Bolb analysis of image processing (1)
- el-Select 选择器 底部固定
- 深度学习环境配置
- 如何将 DevSecOps 引入企业?
- MySQL数据库面试题总结(2022最新版)
- 触觉智能分享-SSD20X实现升级显示进度条
- Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
猜你喜欢
随机推荐
Introduction and application of go module
动态规划总括
力扣题解8/3
Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
string类简介
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
MySQL日期函数
Shocked, 99.9% of the students didn't really understand the immutability of strings
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
The symbol table
使用Patroni回调脚本绑定VIP的坑
QT 如何识别文件的编码格式
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
如何低成本修bug?测试左移给你答案
LCP 17. 速算机器人
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
离线采集怎么看sql执行计划
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer








