当前位置:网站首页>7、特殊SQL的执行
7、特殊SQL的执行
2022-08-04 05:24:00 【猿 白】
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}%' 字符串拼接报错-->
<!--正确方式如下:-->
<!--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:表示当前添加功能使用自增的主键
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
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
- 代码重构:面向单元测试
- C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
- As soon as flink cdc is started, the CPU of the source Oracle server soars to more than 80%. What is the reason?
- 【一步到位】Jenkins的安装、部署、启动(完整教程)
- Write golang simple C2 remote control based on gRPC
- static在不同位置定义变量居然还有不同的含义?
猜你喜欢
MySQL日志篇,MySQL日志之binlog日志,binlog日志详解
深度学习环境配置
[Cocos 3.5.2]开启模型合批
【一步到位】Jenkins的安装、部署、启动(完整教程)
代码重构:面向单元测试
el-Select selector bottom fixed
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
day13--postman接口测试
day13--postman interface test
如何将 DevSecOps 引入企业?
随机推荐
Redis common interview questions
处理List<Map<String, String>>类型
附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;
21 days learning challenge 】 【 sequential search
Introduction and application of go module
The difference between px, em, and rem
商城App开发都有哪些功能呢
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
【一步到位】Jenkins的安装、部署、启动(完整教程)
MySQL日志篇,MySQL日志之binlog日志,binlog日志详解
Get the selected content of the radio box
《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
心余力绌:企业面临的软件供应链安全困境
一个对象引用的思考
What is the salary of a software testing student?
day13--postman interface test
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
如何将 DevSecOps 引入企业?
Programming hodgepodge (4)
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking