Mybatis 其他
# Mybatis其他
# 自增id
获取自增id
<insert id="insert" useGeneratedKeys="true" keyProperty="empId">
insert into student(name,no) values(#{name},#{no})
</insert>
1
2
3
2
3
# 参数设置方式
#{}:底层使用 PreparedStatement 方式,SQL预编译后设置参数,无SQL注入攻击风险
${}:底层使用 Statement 方式,SQL无预编译,直接拼接参数,有SQL注入攻击风险 所有参数位置,都应该用 #{},需要动态表名等,才用 ${}
Connection connection =dataSource.getConnection();
String sql="select*from user where username =? and password = ?" //#仆底层预编译方式,
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"admin");preparedStatement.setString(2,"123456");
//${}底层拼接方式:SQL注入问题
String sql2="select * from user where username ='admin'and password = '123456'"
Statement statement=connection.createStatement();
statement.execute(sql2);
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 特殊字符
在xml中,以下字符需要用转义字符,不能直接写
| 原始字符 | 转义字符 |
|---|---|
| & | & |
| < | < |
| > | > |
| " | " |
| ' | ' |
Last Updated: 2025/12/02, 11:22:00