当前位置:网站首页>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);
}
边栏推荐
- About yolo7 and gpu
- C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
- 渗透测试(PenTest)基础指南
- Programming hodgepodge (4)
- SLSA 框架与软件供应链安全防护
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- [Cocos 3.5.2]开启模型合批
- 力扣:62.不同路径
- 在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
- OpenGL绘制圆
猜你喜欢
随机推荐
QT 如何识别文件的编码格式
Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
离线采集怎么看sql执行计划
idea设置识别.sql文件类型以及其他文件类型
【评价类模型】Topsis法(优劣解距离法)
el-Select selector bottom fixed
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
CentOS7 —— yum安装mysql
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
谷粒商城-基础篇(项目简介&项目搭建)
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
There is an 8 hour difference between the docker installation of mysql and the host.
8大软件供应链攻击事件概述
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
使用Loadrunner进行性能测试
LCP 17. 速算机器人
static在不同位置定义变量居然还有不同的含义?
你以为border-radius只是圆角吗?【各种角度】
力扣题解8/3





![[Cocos 3.5.2]开启模型合批](/img/d9/9e8f71c9a26c8052b11291fe3ba7ac.png)



