当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型

idea设置识别.sql文件类型以及其他文件类型

How to simplify the automation of modern e-procurement?

System design. How to design a spike system (full version transfer)

There is an 8 hour difference between the docker installation of mysql and the host.

Turn: Management is the love of possibility, and managers must have the courage to break into the unknown

Teenage Achievement Hackers Need These Skills

C语言 -- 操作符详解

px、em、rem的区别

败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
随机推荐
路网编辑器技术预研
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
LCP 17. 速算机器人
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
FPGA学习笔记——知识点总结
高性能高可靠性高扩展性分布式防火墙架构
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
少年成就黑客,需要这些技能
编程大杂烩(四)
触觉智能分享-SSD20X实现升级显示进度条
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
8款最佳实践,保护你的 IaC 安全!
Resolved error: npm WARN config global `--global`, `--local` are deprecated
CentOS7 —— yum安装mysql
C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
有趣的 Kotlin 0x0E:DeepRecursiveFunction
MySQL数据库(基础)