当前位置:网站首页>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);
}
边栏推荐
- 一个对象引用的思考
- Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
- 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
- DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
- Introduction and application of go module
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- Teenage Achievement Hackers Need These Skills
- Delphi-C端有趣的菜单操作界面设计
- MySQL日志篇,MySQL日志之binlog日志,binlog日志详解
- C专家编程 第5章 对链接的思考 5.2 动态链接的优点
猜你喜欢
Cannot read properties of null (reading ‘insertBefore‘)
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
编程大杂烩(三)
一个对象引用的思考
震惊,99.9% 的同学没有真正理解字符串的不可变性
MySQL数据库(基础)
The symbol table
System design. How to design a spike system (full version transfer)
Programming hodgepodge (4)
Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
随机推荐
注意!软件供应链安全挑战持续升级
使用Loadrunner进行性能测试
[Cocos] cc.sys.browserType可能的属性
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
QT 如何识别文件的编码格式
day13--postman接口测试
8大软件供应链攻击事件概述
Bolb analysis of image processing (1)
flink cdc一启动,源端Oracle那台服务器的CPU就飙升到80%以上,会是啥原因呢?
编程大杂烩(三)
Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
2022年PMP考试延迟了,该喜该忧?
day13--postman interface test
Interesting Kotlin 0x0E: DeepRecursiveFunction
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
[SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
读者让我总结一波 redis 面试题,现在肝出来了
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
Dynamic programming of the division of numbers